【发布时间】:2015-09-16 21:57:58
【问题描述】:
我想加密用户输入的密码字符串,然后在屏幕上打印。还可以恢复原始密码,然后也将其打印在屏幕上。但是 XOR 运算符不适用于字符串。我该如何操作它?
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string pass;
string enc="akdhigfohre";
string x;
cout<<"Enter new password: ";
cin>>pass;
cout<<"\n\nYour New Password is:" << pass<<endl;
x=pass^enc;
cout<<"\n\nEncrypted Version: "<<x;
x=x^enc;
cout<<"\n\nRecovred Password: "<<x;
system("pause");
}
【问题讨论】:
-
您可以将字符串拆分为字符,对每个单独的字符进行异或,然后将它们组合成新字符串
-
谢谢先生。如何拆分字符?请告诉代码。
-
就
for(char c: pass) { ... } -
类似于
for (int i = 0; i<x.length;i++) {newPass+= x[i]^enc[i%enc.length]; }
标签: c++ string operator-keyword xor