C++ 扩展凯撒密码C++;
我正在尝试制作和扩展Caesar Cipher版本,包括ASCII值32(一个空格)到126(一个波浪形)的所有7位ASCII字符,但问题是如何确保我的程序只使用这些ASCII字符,而不使用奇怪的DEL符号或扩展ASCII图表 例如,如果我键入小写字母“u”并输入10作为键,我将得到del符号,但我想在键入小写字母“u”时得到一个空格 这是我目前掌握的代码C++ 扩展凯撒密码C++;,c++,C++,我正在尝试制作和扩展Caesar Cipher版本,包括ASCII值32(一个空格)到126(一个波浪形)的所有7位ASCII字符,但问题是如何确保我的程序只使用这些ASCII字符,而不使用奇怪的DEL符号或扩展ASCII图表 例如,如果我键入小写字母“u”并输入10作为键,我将得到del符号,但我想在键入小写字母“u”时得到一个空格 这是我目前掌握的代码 #include <iostream> #include <string> #include <algorit
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
string Encrypt(string, int);
int main(int argc, char *argv[])
{
string Source;
int Key;
cout << "Source: ";
getline(cin, Source);
cout << "Key: ";
cin >> Key;
cout << "Encrypted: " << Encrypt(Source, Key) << endl;
system("pause");
}
string Encrypt(string Source, int Key)
{
string Crypted = Source;
for (int Current = 0; Current < Source.length(); Current++)
Crypted[Current] += Key;
return Crypted;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串加密(字符串,int);
int main(int argc,char*argv[])
{
字符串源;
int键;
cout键;
cout简单:只需复制受祝福范围之外的任何输入值而不进行修改,您只需进行两次比较:
if(x<' ' || x>126)
copy();
else
encode();
if(x126)
复制();
其他的
编码();
caesar密码的算法为:
将所有有效符号映射到从0开始的连续非负数
添加符号计数的键模
向后映射
旁白:您可能也必须在应用程序之前映射键。好的,我找到了它。我所做的只是修改公式,强制程序循环使用ASCII值32-126
Crypted[Current] = ((Crypted[Current] + key) - 32) % 95 + 32;(Encrypting)
Crypted[Current] = ((Crypted[Current] - key) - 32 + 3 * 95) % 95 + 32; (Decrypting)
谢谢格雷格的主意
这是完整的代码完美地工作
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;
string encrypt(string, int);
string decrypt(string source, int key);
int main(int argc, char *argv[])
{
string Source;
int Key;
cout << "Source: ";
getline(cin, Source);
cout << "Key: ";
cin >> Key;
cout << "Encrypted: " << decrypt(Source, Key) << endl;
system("pause");
}
string encrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] + key) - 32) % 95 + 32;
return Crypted;
}
string decrypt(string source, int key)
{
string Crypted = source;
for (int Current = 0; Current < source.length(); Current++)
Crypted[Current] = ((Crypted[Current] - key) - 32 + 3 * 95) % 95 + 32;
return Crypted;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串加密(字符串,int);
字符串解密(字符串源,int密钥);
int main(int argc,char*argv[])
{
字符串源;
int键;
cout键;
你没试过模吗?Crypted[Current]=(Crypted[Current]+Key)%127;
@Greg:小心负数。(char
可能有符号)