为什么我的字符串不会异或?
Why my strings are not going to be XOR?
我想对用户输入的密码字符串进行加密,然后打印在屏幕上。还可以恢复原始密码,然后也将其打印在屏幕上。但是 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");
}
好的,我有办法解决你的问题。希望对你有所帮助。
#include <iostream>
using namespace std;
#include<iostream>
using std::string;
string XOR(string value,string key)
{
string retval(value);
short unsigned int klen=key.length();
short unsigned int vlen=value.length();
short unsigned int k=0;
short unsigned int v=0;
for(v;v<vlen;v++)
{
retval[v]=value[v]^key[k];
k=(++k<klen?k:0);
}
return retval;
}
int main()
{
std::string value("Phuc Nguyen");
std::string key("akdhigfohre");
std::cout<<"Plain text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Cipher text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Decrypted text: "<<value<<std::endl;
std::cin.get();
return 0;
}
再次尝试问题的代码库,
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string pass;
string enc="akdhigfohre";
string x = "";
string y = "";
cout<<"Enter new password: ";
cin>>pass;
cout<<"\n\nYour New Password is:" << pass<<endl;
for(size_t i = 0; i < pass.size(); ++i){
x += pass.at(i)^enc.at(i%enc.size());
}
cout<<"\n\nEncrypted Version: "<<x;
for(size_t i = 0; i < x.size(); ++i){
y += x.at(i)^enc.at(i%enc.size());
}
cout<<"\n\nRecovred Password: "<<y;
system("pause");
}
我想对用户输入的密码字符串进行加密,然后打印在屏幕上。还可以恢复原始密码,然后也将其打印在屏幕上。但是 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");
}
好的,我有办法解决你的问题。希望对你有所帮助。
#include <iostream>
using namespace std;
#include<iostream>
using std::string;
string XOR(string value,string key)
{
string retval(value);
short unsigned int klen=key.length();
short unsigned int vlen=value.length();
short unsigned int k=0;
short unsigned int v=0;
for(v;v<vlen;v++)
{
retval[v]=value[v]^key[k];
k=(++k<klen?k:0);
}
return retval;
}
int main()
{
std::string value("Phuc Nguyen");
std::string key("akdhigfohre");
std::cout<<"Plain text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Cipher text: "<<value<<"\n\n";
value=XOR(value,key);
std::cout<<"Decrypted text: "<<value<<std::endl;
std::cin.get();
return 0;
}
再次尝试问题的代码库,
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string pass;
string enc="akdhigfohre";
string x = "";
string y = "";
cout<<"Enter new password: ";
cin>>pass;
cout<<"\n\nYour New Password is:" << pass<<endl;
for(size_t i = 0; i < pass.size(); ++i){
x += pass.at(i)^enc.at(i%enc.size());
}
cout<<"\n\nEncrypted Version: "<<x;
for(size_t i = 0; i < x.size(); ++i){
y += x.at(i)^enc.at(i%enc.size());
}
cout<<"\n\nRecovred Password: "<<y;
system("pause");
}