概述
用C++代码编写基于异或(XOR)的加密和解密函数,以及将字符串转换为十六进制表示和从十六进制表示转换回字符串的函数的实践。
函数
-
getXorEncryptDecrypt
- 目的:使用单个字符密钥对字符串进行异或加密/解密。
- 参数:
const string &str
:要加密/解密的输入字符串。char key
:用于异或操作的密钥。
- 返回值:加密/解密后的字符串。
-
encrypt
- 目的:使用多字符密钥对字符串进行加密,通过迭代调用
getXorEncryptDecrypt
。 - 参数:
const string &str
:要加密的输入字符串。const string &key
:多字符密钥。
- 返回值:加密后的字符串。
- 目的:使用多字符密钥对字符串进行加密,通过迭代调用
-
decrypt
- 目的:使用多字符密钥对字符串进行解密,通过反向迭代调用
getXorEncryptDecrypt
。 - 参数:
const string &str
:要解密的输入字符串。const string &key
:多字符密钥。
- 返回值:解密后的字符串。
- 目的:使用多字符密钥对字符串进行解密,通过反向迭代调用
-
toHex
- 目的:将字符串转换为其十六进制表示。
- 参数:
const string &str
:输入字符串。
- 返回值:字符串的十六进制表示。
-
fromHex
- 目的:将十六进制字符串转换回其原始字符串形式。
- 参数:
const string &str
:输入的十六进制字符串。
- 返回值:原始字符串。
主函数
- 目的:演示加密、解密和十六进制转换函数。
- 步骤:
- 初始化字符串
str
和密钥key
。 - 使用
key
对str
进行加密。 - 将
str
转换为其十六进制表示。 - 打印原始字符串、其十六进制表示以及将十六进制转换回字符串的结果。
- 打印加密后的十六进制字符串和解密后的字符串。
- 初始化字符串
示例输出
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;
}