gpt4 book ai didi

c++ - 如何在 C++ 中正确创建加密?

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:22 25 4
gpt4 key购买 nike

我已经完成了有关加密的程序。问题是,我需要一条捷径。我编写的程序太长,不太适合。我只是 c++ 编程的新手。有人可以帮我吗?谢谢! :) 这是我的程序:

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
c:char crypt[25], cons;
int ctr;
cout<<"Input your 26-character cipherstring below.\n\n";
cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";cin>>crypt;
e:char string[65535];
cout<<"\nInput your string (input your spaces as any non-alphabet character).\n";cin>>string;
cout<<"\n\nEncrypted string: "; //std::string
for(ctr=0;ctr<=strlen(string);ctr++)
{
switch(string[ctr])
{
case('a'): cout<<crypt[0]; break;
case('b'): cout<<crypt[1]; break;
case('c'): cout<<crypt[2]; break;
case('d'): cout<<crypt[3]; break;
case('e'): cout<<crypt[4]; break;
case('f'): cout<<crypt[5]; break;
case('g'): cout<<crypt[6]; break;
case('h'): cout<<crypt[7]; break;
case('i'): cout<<crypt[8]; break;
case('j'): cout<<crypt[9]; break;
case('k'): cout<<crypt[10]; break;
case('l'): cout<<crypt[11]; break;
case('m'): cout<<crypt[12]; break;
case('n'): cout<<crypt[13]; break;
case('o'): cout<<crypt[14]; break;
case('p'): cout<<crypt[15]; break;
case('q'): cout<<crypt[16]; break;
case('r'): cout<<crypt[17]; break;
case('s'): cout<<crypt[18]; break;
case('t'): cout<<crypt[19]; break;
case('u'): cout<<crypt[20]; break;
case('v'): cout<<crypt[21]; break;
case('w'): cout<<crypt[22]; break;
case('x'): cout<<crypt[23]; break;
case('y'): cout<<crypt[24]; break;
case('z'): cout<<crypt[25]; break;
case('A'): cout<<crypt[0]; break;
case('B'): cout<<crypt[1]; break;
case('C'): cout<<crypt[2]; break;
case('D'): cout<<crypt[3]; break;
case('E'): cout<<crypt[4]; break;
case('F'): cout<<crypt[5]; break;
case('G'): cout<<crypt[6]; break;
case('H'): cout<<crypt[7]; break;
case('I'): cout<<crypt[8]; break;
case('J'): cout<<crypt[9]; break;
case('K'): cout<<crypt[10]; break;
case('L'): cout<<crypt[11]; break;
case('M'): cout<<crypt[12]; break;
case('N'): cout<<crypt[13]; break;
case('O'): cout<<crypt[14]; break;
case('P'): cout<<crypt[15]; break;
case('Q'): cout<<crypt[16]; break;
case('R'): cout<<crypt[17]; break;
case('S'): cout<<crypt[18]; break;
case('T'): cout<<crypt[19]; break;
case('U'): cout<<crypt[20]; break;
case('V'): cout<<crypt[21]; break;
case('W'): cout<<crypt[22]; break;
case('X'): cout<<crypt[23]; break;
case('Y'): cout<<crypt[24]; break;
case('Z'): cout<<crypt[25]; break;
default: cout<<" "; break;
}
}
cout<<"\n\n";
/*cout<<"Input 'c' to re-input your cipherstring.\n 'e' to reuse your cipherstring.\n 'q' to quit. ";
comm:cout<<"\nCommand: "; cin>>cons;
switch (cons)
{
case('c'): cout<<endl; goto c; break;
case('C'): goto c; break;
case('e'): goto e; break;
case('E'): goto e; break;
case('q'): break;
case('Q'): break;
default: cout<<"Invalid command. Please refer to the command list above.\n";goto comm;
}*/
system("PAUSE"); return 0;
}

最佳答案

你可以用这个替换那个巨大的 switch 语句:

if (isalpha(string[ctr]))
{
int index = toupper(string[ctr]) - 'A';
cout << crypt[index];
}
else
cout << " ";

此外,不要在 for 循环的每次迭代中都调用 strlen,只调用一次:

for(int ctr=0, len=strlen(string); ctr<len; ctr++)
{
....
}

其他一些注意事项:

  • crypt 太小,它的大小应该是 27
  • 如果您使用 getline 而不是 operator>>,您可以让用户正确输入空格
  • 您的代码容易出现缓冲区溢出。使用 std::string,您几乎可以忘记这些。

请注意,正如 Kerrek 所说,这假设在您使用的字符集中,大写字母 A-Z 是按顺序排列的。但我认为情况并非如此,您可以放心地忽略它们。

关于c++ - 如何在 C++ 中正确创建加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7704660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com