XOR错误的结果,转义字符
XOR wrong result , escaping char
我正在尝试对一个小字符串进行异或,它可以工作。当我尝试使用XOR字符串时,我甚至无法编译它
string str = "MyNameIsMila";
string str_xored = "2*&.8"'*"; //you can't escape this or the result be be different
//Enc:2*&.8"'*:
//Dec:MyNameIsMila:
我试图逃脱这根绳子,但最后我得到了另一个结果。有什么好的方向吗?退出后输出:
//Enc:yamesila:
//Dec:2*&.8"'*:
希望能让MyNameIsMila回来。
功能看起来像:
string encryptDecrypt(string toEncrypt) {
char key = 'K'; //Any char will work
string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++)
output[i] = toEncrypt[i] ^ key;
return output;
}
我有两件事要说:
1:字符串的值需要在2->"之间
string str_xored = 2*&.8"'*; //this is not a valid syntax = error
//valid
string str_xored = "2*&.8";
str += '"';
str += "'*";
2:在你的情况下,我会使用迭代器:
#include <iostream>
#include <string>
//please don't use "using namespace std;"
std::string encryptDecrypt(std::string toEncrypt) {
char key = 'K'; //Any char will work
std::string output = ""; //needs to be empty
for (auto i = toEncrypt.begin(); i != toEncrypt.end(); i++) {
output += *i ^ key; //*i holds the current character to see how
//an iterator works have a look at my link
}
return output;
}
int main() {
std::string str = encryptDecrypt("Hello...!");
std::cout << str << std::endl;
return 0;
}
下面来看一下(字符串)迭代器:
链路1
链路2
如果你认为迭代器太难了,那么就使用
for(int i = 0; i < str.size(); i++){
//CODE
}
for()-循环
不能像对待普通字符串那样对待xored字符串!
value ^ same_value == 0
把它们当作普通的容器。
示例:
#include <iostream>
#include <iterator>
#include <algorithm>
template<typename InputIterator, typename OutputIterator, typename Key>
void perform_xor(InputIterator begin, InputIterator end, OutputIterator out, Key const &key) {
std::transform(begin, end, out, [key](auto &&value) {
return value ^ key;
});
}
using namespace std;
int main() {
char test[] = "(2*&.8"'*";
perform_xor(begin(test), end(test), begin(test), '&');
copy(begin(test), end(test), ostream_iterator<int>(cout, " "));
cout << endl;
perform_xor(begin(test), end(test), begin(test), '&');
copy(begin(test), end(test), ostream_iterator<char>(cout));
return 0;
}
请参阅:http://ideone.com/ryoNp5
相关文章:
- 为什么当我从 cin 阅读时转义字符不起作用?
- 在QLineEdit中显示和编辑转义字符
- std::regex_error语言 - 意外的转义字符
- LPCWSTR 查找和替换为转义字符
- 修改正则表达式以包含反斜杠和单引号(以便反斜杠表现为转义字符)
- 有人可以解释一下与 Windows 文件系统相关的C++转义字符" "吗?
- 将带有转义字符的大字符串转换为字节数组
- 由转义字符组成的字符串文字的大小
- XOR错误的结果,转义字符
- 在字符串行中查找转义字符
- 以十进制、八进制和十六进制表示法标识整数文本是否与转义字符中的整数文本相同
- C++转义字符和引用变量输出混淆
- 定义 std::字符串 C++不带转义字符
- 字符结果和整数指针之间的差异
- 正在读取(?)转义字符
- 如何提供转义字符作为命令行参数
- 打印不带转义字符的字符串
- Linux中的转义字符不起作用
- 关于转义字符的帮助
- cin.Get带有转义字符