【发布时间】:2016-07-15 03:39:33
【问题描述】:
程序应该询问用户 3 个选项:
1 表示加密 2 用于解密 3 退出
对于加密,第一步是要求对纯文本进行加密,程序通过它。
下一步是询问加密/解密代码,当我输入出现错误的地方时
这是完整的代码:
#include bits/stdc++.h
#include stdio.h
using namespace std;
void encrypt001();
void decrypt001();
string yes;
string inname;
string key;
string alphabets="abcdefghijklmnopqrstuvwxyz";
int size=0;
int y;
int main()
{
int useroption;
do
{
cout<<"*****ENIGMA****"<<endl;
cout<<"[1] - Encrypt"<<endl;
cout<<"[2] - Decrypt"<<endl;
cout<<"[3] - Exit"<<endl;
cout<<"Enter Choice:"<<endl;
cin>>useroption;
switch (useroption)
{
case 1:
encrypt001();
break;
case 2:
decrypt001();
break;
default:
cout<<"Exit"<<endl;
break;
}
}
while (useroption!=3);
}
void encrypt001()
{
cout<<"*****ENCRYPTION******"<<endl;
string encrypt;
string ekey;
cin.ignore();
cout<<"Enter Plain Text To Encrypt:"<<endl;
getline(cin,encrypt);
cout<<"Enter Encryption Key:"<<endl;
cin>>ekey;
int elen=encrypt.length();
int ekeylen=ekey.length();
int letterslen=alphabets.length();
int num;
int y=0;
for (int x=0; x<elen; x++)
{
for (int h=0; h<letterslen; h++)
{
if (ekey.at(y)==alphabets[h])
{
num=h;
}
}
int num1=0;
int num2=0;
string space;
space=encrypt.at(x);
if (space==" ")
{
continue;
}
for (int j=0; j<letterslen; j++)
{
if (encrypt[x]==alphabets[j])
{
num1=j;
}
}
num2=num+num1;
if (num2>25)
{
num2=num2-26;
}
string letterrep;
letterrep=alphabets.at(num2);
encrypt.replace(x, 1, letterrep);
y++;
if (y>ekeylen)
{
y=0;
}
}
cout<<"Encrypted Cipher Text:"<<encrypt<<endl;
cout<<"Save To File? (y/n)"<<endl;
cin>>yes;
if (yes=="y")
{
cout<<"Enter File Name:"<<endl;
cin>>inname;
cout<<inname<<".txt saved succcessfully."<<endl;
}
else
{
cout<<"Proceed"<<endl;
}
}
void decrypt001()
{
cout<<"*****DECRYPTION*****"<<endl;
string decrypt;
string dkey;
string output;
cin.ignore();
cout<<"Enter Cipher Text to Decrypt:"<<endl;
getline(cin, decrypt);
cout<<"Enter Decryption Key:"<<endl;
cin>>dkey;
int dlen=0;
int dkeylen=0;
for (int k=0; k<decrypt.length(); k++)
{
if (decrypt[dlen]!=' ')
{
output+=((((decrypt[dlen]-97)+26)-(dkey[dkeylen]-97))%26)+97;
dkeylen++;
dlen++;
if (dkeylen==dkey.length())
{
dkeylen=0;
}
}
else if (decrypt[dlen]==' ')
{
output+=" ";
dlen++;
}
}
cout<<"Decrypted Plain Text:"<<output<<endl;
}
抱歉第一部分的格式,我没有足够的声誉来发布多个链接。
解密部分工作正常。
错误是:
终止调用 std::out_of_range
【问题讨论】:
-
对不起,你不会得到这份工作。
-
他们在 stackoverflow.com 上找到了这篇文章。
标签: c++ c++11 encryption crash