思路:原理是用字节b对字节A异或两次结果还是A;
#include <iostream>
void encryp(char *pContents, std::string key);
void decryp(char *pContents, std::string key);
int main()
{
//test
std::string key = "123456";
char contents[] = "Hello World!\n";
encryp(contents, key);
std::cout << "encryp:" << std::string(contents) << std::endl;
decryp(contents, key);
std::cout << "decryp:" << std::string(contents) << std::endl;
//
return 0;
}
//加密
void encryp(char *pContents, std::string key){
char *pCur = pContents;
int i = 0;
int len = strlen(pContents);
while(len--){
*pCur ^= key.at(i);
i = (i+1)%key.size();
pCur++;
}
}
//解密
void decryp(char *pContents, std::string key){
char *pCur = pContents;
int i = 0;
int len = strlen(pContents);
while(len--){
*pCur ^= key.at(i);
i = (i+1)%key.size();
pCur++;
}
}
好处:
只要保证key不泄露,暴力破解其实也不好破解(有懂的欢迎评论区讨论);
对于普通业务的文本资料混淆特别好用;
对于exe、图片、视频加密很好用,可以只解密其中的某一部分字节,这样资料就损坏了;
加密效率高;
实现简单;
问题:
对同一文本加密两次会无效,实际项目需要添加已加密的标识