我到处寻找一个C ++代码,它接收来自用户的消息,并通过增加每个字符的ASCII值对其进行编码(显然不是很安全,但很简单)。我已经设法组建了一个程序,它返回一些更高值的字符,但无法弄清楚如何使用包含空格的完整消息来完成它。我打算制作一个后来反其道而行的解码器。任何帮助将非常感激。提前谢谢。
单值C ++程序 -
#include <iostream>
using namespace std;
int main(){
char ascii;
cout << "Enter a character: ";
cin >> ascii;
cout << "Its ascii value is: " << (int) ascii << endl;
return 0;
}
VBS中的工作编码器示例 -
set x = WScript.CreateObject("WScript.Shell")
entxtde = inputbox("Enter text to be encoded")
entxtde = StrReverse(entxtde)
x.Run "%windir%\notepad"
wscript.sleep 1000
x.sendkeys encode(entxtde)
function encode(s)
For i = 1 To Len(s)
newtxt = Mid(s, i, 1)
newtxt = Chr(Asc(newtxt)+3)
coded = coded & newtxt
Next
encode = coded
End Function
答案 0 :(得分:0)
std::string originalString = "";
std::string newString = "";
int incrementValue = 1;
std::cout << "Input a string to encode: ";
std::cin >> originalString;
for(int i = 0; i < originalString.length(); i++) {
newString += (originalString.at(i) + incrementValue);
}
std::cout >> "New string is " + newString
只需更改incrementValue
即可更改其编码方式。
如果incrementValue = 1,则“Hello”=“Ifmmp”
要反转它,只需将其更改为减去incrementValue
,而不是添加相同类型的for
循环。我觉得很简单
答案 1 :(得分:0)
如果您希望保持在80列以下,可以在一行中完成,或者两行。其中value
是您要加密的字符串,offset
是偏移值:
auto caesar = [offset](CharT c){return c + offset;};
std::transform(value.begin(), value.end(), value.begin(), caesar);
对于奖励积分,您可以通过对字符类型进行模板化来使其适用于任何类型的字符串:
template <typename CharT>
std::basic_string<CharT> caesarEncode(std::basic_string<CharT> value, CharT offset){
auto caesar = [offset](CharT c){return c + offset;};
std::transform(value.begin(), value.end(), value.begin(), caesar);
return value;
}
由于您可能在实际获取具有空格的字符串时遇到困难,因此您可以使用标准库的getline
函数获取一个,默认情况下会获得源流的一整行。
// narrow (CharT = char)
std::string value;
std::getline(std::cin, value);
// wide (CharT = wchar_t)
std::wstring wvalue;
std::getline(std::wcin, wvalue);
实际编码字符串的将按如下方式进行:
char offset = 12;
auto encoded = caesarEncode(value, offset);
wchar_t woffset = 12;
auto wencoded = caesarEncode(wvalue, woffset);
您可以在实践中看到一个示例here on coliru。
答案 2 :(得分:-1)
真的很简单。首先,您将输入作为字符串。然后,按照您想要的方式迭代每个字符。为确保值保持有效且易于反转,您可以使用char的最大值(即255)修改该值。
didSelectRowAtIndexPath
注意: int main () {
std::string input; // to store the text
std::getline(std::cin, input); // get the text
const int _add = 12; // value added
const int max_size = 255; // max value of a char
for (int i = 0; i < input.size(); ++i)
input[i] = (input[i] + _add) % max_size;
/* now the input is properly modified */
}
是一个防止溢出错误的int。