XOR加密实践与字符串toHex(字符串转16进制字符的字符串)

在这里插入图片描述

概述

用C++代码编写基于异或(XOR)的加密和解密函数,以及将字符串转换为十六进制表示和从十六进制表示转换回字符串的函数的实践。

函数

  1. getXorEncryptDecrypt

    • 目的:使用单个字符密钥对字符串进行异或加密/解密。
    • 参数
      • const string &str:要加密/解密的输入字符串。
      • char key:用于异或操作的密钥。
    • 返回值:加密/解密后的字符串。
  2. encrypt

    • 目的:使用多字符密钥对字符串进行加密,通过迭代调用getXorEncryptDecrypt
    • 参数
      • const string &str:要加密的输入字符串。
      • const string &key:多字符密钥。
    • 返回值:加密后的字符串。
  3. decrypt

    • 目的:使用多字符密钥对字符串进行解密,通过反向迭代调用getXorEncryptDecrypt
    • 参数
      • const string &str:要解密的输入字符串。
      • const string &key:多字符密钥。
    • 返回值:解密后的字符串。
  4. toHex

    • 目的:将字符串转换为其十六进制表示。
    • 参数
      • const string &str:输入字符串。
    • 返回值:字符串的十六进制表示。
  5. fromHex

    • 目的:将十六进制字符串转换回其原始字符串形式。
    • 参数
      • const string &str:输入的十六进制字符串。
    • 返回值:原始字符串。

主函数

  • 目的:演示加密、解密和十六进制转换函数。
  • 步骤
    1. 初始化字符串str和密钥key
    2. 使用keystr进行加密。
    3. str转换为其十六进制表示。
    4. 打印原始字符串、其十六进制表示以及将十六进制转换回字符串的结果。
    5. 打印加密后的十六进制字符串和解密后的字符串。

示例输出

main函数将输出原始字符串、其十六进制表示、加密后的十六进制字符串和解密后的字符串。

代码

#include <iostream>
#include <string>

using namespace std;

// XOR encryption/decryption algorithm
string getXorEncryptDecrypt(const string & str, char key)
{
    string res = str; // Initialize result with the input string
#if __cplusplus >= 201103L
    for (char &c : res)
    {
        c ^= key; // XOR each character with the key
    }
#else
    size_t size = res.size();
    for(size_t i = 0 ; i < size ; i++)
    {
        res[i] ^= key;
    }
#endif
    return res;
}


//加密
string encrypt(const string &str, const string &key)
{
    string res = str;
#if __cplusplus >= 201103L
    for (char k : key)
    {
        res = getXorEncryptDecrypt(res, k);
    }
#else
    size_t size = key.size();
    for(size_t i = 0 ; i < size ; i++)
    {
        res = getXorEncryptDecrypt(res, key[i]);
    }
#endif
    return res;
}

//解密
string decrypt(const string &str , const string &key)
{
    string res = str;
#if __cplusplus >= 201103L
    for (auto it = key.rbegin(); it != key.rend(); ++it)
    {
        res = getXorEncryptDecrypt(res, *it);
    }
#else
    for (int i = key.size() - 1; i >= 0; --i)
    {
        res = getXorEncryptDecrypt(res, key[i]);
    }
#endif
    return res;
}

//将字符串内容转化为16进制字符的字符串
string toHex(const string &str)
{
    static const char hexChars[] = "0123456789abcdef";
    size_t size = str.size();
    string res;
    res.reserve(size * 2); // 预分配结果字符串的大小

    for (size_t i = 0; i < size; ++i)
    {
        unsigned char byte = static_cast<unsigned char>(str[i]);
        res += hexChars[byte >> 4]; // 高位半字节
        res += hexChars[byte & 0x0F]; // 低位半字节
    }

    return res;
}

inline unsigned char hexCharToByte(char c)
{
    return (c >= '0' && c <= '9') ? (c - '0') :
           (c >= 'a' && c <= 'f') ? (c - 'a' + 10) :
           (c >= 'A' && c <= 'F') ? (c - 'A' + 10) :
           0; // Invalid character
}

//从一个hex字符串即16进制字符的字符串转化为字符串
string fromHex(const string &str)
{
    string res;
    int size = str.size();
    if (size % 2 != 0) 
        return ""; // Invalid hex string

    for (int i = 0; i < size; i += 2)
    {
        char highNibble = str[i];
        char lowNibble = str[i + 1];
        unsigned char byte = (hexCharToByte(highNibble) << 4) | hexCharToByte(lowNibble);
        res += byte;
    }

    return res;
}


int main()
{                 /* 有内鬼终止交易! */
    string str = "A mole pulled the plug on the deal !";
    string key = "this is a key";//密钥

    string dstr = encrypt(str,key);//对str进行加密;
    string hex = toHex(str);

    cout <<"self: " << str  <<endl;
    cout <<"toHex: "<< hex << endl;
    cout <<"fromHex: "<< fromHex(hex) << endl;

    //输出加密后的hex
    cout <<"encrypt: " << toHex(dstr) << endl;
    //输出解密后的内容
    cout <<"decrypt: " << decrypt(dstr,key) << endl <<endl;
   
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boonion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值