C++ 使用异或对字符串进行简单加密
https://blog.csdn.net/fwb330198372/article/details/84637137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| int key[] = { 1,2,3,4,5,6,7}; void encryption(string& c, int key[]) { int len = c.size(); for (int i = 0; i < len; i++) { c[i] = c[i] ^ key[i % 7]; } } void decode(string& c, int key[]) { int len = c.size(); for (int i = 0; i < len; i++) { c[i] = c[i] ^ key[i % 7]; } } int main(int argc, char* argv[]) { std::string str = "hello world!"; std::cout << "原文:" << str << std::endl; encryption(str, key); std::cout << "加密后密文:" << str << std::endl; decode(str, key); std::cout << "解密后密文:" << str << std::endl; return 0; }
|