Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的字符串不是XOR?_C++_String_Operator Keyword_Xor - Fatal编程技术网

C++ 为什么我的字符串不是XOR?

C++ 为什么我的字符串不是XOR?,c++,string,operator-keyword,xor,C++,String,Operator Keyword,Xor,我想加密用户输入的密码字符串,然后在屏幕上打印它。同时恢复原始密码,然后在屏幕上打印。但XOR运算符不能处理字符串。我怎样才能操纵它 #include<iostream> #include<string.h> using namespace std; int main() { string pass; string enc="akdhigfohre"; string x; cout<<"Enter new password: ";

我想加密用户输入的密码字符串,然后在屏幕上打印它。同时恢复原始密码,然后在屏幕上打印。但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");
}

您可以将字符串拆分为字符,对每个字符进行异或运算,然后将它们组合成新的字符串。我如何分割角色?请告诉代码。只需forchar c:pass{…}它将类似于int i=0;我会在将来。但现在我有了名声