Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数之间共享变量-rand()和_C++_Variables_Encryption_Random_Srand - Fatal编程技术网

C++ 函数之间共享变量-rand()和

C++ 函数之间共享变量-rand()和,c++,variables,encryption,random,srand,C++,Variables,Encryption,Random,Srand,我必须创建一个程序来收集用户的输入,然后对这些字符进行加密和解密 我对编码不是很有信心,因此我确信我的代码的许多部分写得很差,没有遵循最佳实践,因此我编写了一个简单的算法版本,程序只需在ASCII值上加/减2,但我发现了rand()的用法和srand函数,但我不确定如何在我的encrypt和decrypt函数中将它们作为单个值(静态变量?)使用 这是我目前的全部代码 #include <iostream > #include <iomanip> #include <

我必须创建一个程序来收集用户的输入,然后对这些字符进行加密和解密

我对编码不是很有信心,因此我确信我的代码的许多部分写得很差,没有遵循最佳实践,因此我编写了一个简单的算法版本,程序只需在ASCII值上加/减2,但我发现了rand()的用法和srand函数,但我不确定如何在我的encrypt和decrypt函数中将它们作为单个值(静态变量?)使用

这是我目前的全部代码

#include <iostream >
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

char getMenuSelection (char m);
void encrypt (char key[]);
void decrypt (char code[]);

int main ()
{
    ifstream input;
    ifstream in;
    ofstream out;
    char menu;
    char message[80];
    //char codekey;
    //char myname[128];
     menu = 0;

    menu=getMenuSelection(menu);
    if (menu == 'e' || menu == 'E') //Option to Encrypt
    {
        cout << "Enter Message to be encrypted \n";
        cin >> message;
        input.open (message);
        encrypt (message);
        input.close();
         cout << "The Ciphertext is: " << message << endl;
        return main();
    }
     if (menu == 'd' || menu == 'D')
    {
         cout << "Enter Ciphertext to decrypt \n";
         cin >> message;
         input.open (message);
         decrypt (message);
         input.close();
         cout << "The Decrypted message is: " << message << endl;
        return main();
     }
    if (menu == 'q' || menu == 'Q')
     {
         cout << "Program exiting.";
         cin.ignore();
         return 0;
    }

    cin.ignore();
    return 0;
     }

 char getMenuSelection (char m) // Get the menu function
 {
     cout << "|||_______Welcome To Elliotts Encryption_______|||\n";
     cout << "|||__________and Deciphering Program___________|||\n";
     cout << "|||____________________________________________|||\n";
     cout << "|||______Please select 'E' to encrypt a________|||\n";
     cout << "|||____message, 'D' to decipher a message______|||\n";
     cout << "|||______________Or 'Q' to Quit________________|||\n";
     cin >> m;

 switch (m)
     {
        case 'e':
        case 'E':   cout << "Encryption." << endl;
                  break;
        case 'd':
        case 'D':   cout <<  "Decryption." << endl;
                    break;
            case 'q':
        case 'Q':   cout <<  "Quitting" << endl;
                break;
        default: cout << "Error, Please try another message. ";
                 return getMenuSelection(m);
    }
    return (m);
 }
int random (int r)
 {
    srand(time(0));
    r=rand();
    return r;
  }
void encrypt (char key[], int r) //encryption function.
{
    for(int i = 0; i <= 40; i++) key[i] += r;
}
 void decrypt (char code[], int r) //decryption function.
 {
     for(int i = 0; i <= 40; i++) code[i] -= r;
 }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
char getMenuSelection(charm);
无效加密(字符密钥[]);
无效解密(字符代码[]);
int main()
{
ifstream输入;
如果输入;
流出的液体;
字符菜单;
字符消息[80];
//字符码键;
//char myname[128];
菜单=0;
菜单=获取菜单选择(菜单);
if(menu==“e”| | menu==“e”)//加密选项
{
cout>消息;
input.open(消息);
加密(消息);
input.close();
库特
  • 使用静态变量或全局变量会使其按原样工作吗
  • 不完全正确。这里的问题只是您声明和调用的函数签名:

    void encrypt (char key[]);
    
    与正在定义的函数的签名不匹配:

    void encrypt (char key[], int r);
    
    这使它成为一个单独的函数(由于函数重载)

    使
    r
    成为全局变量可能是解决方案的一部分,但仍需要使此函数的签名保持一致

    至于如何生成该值,则取决于您。不过,可能需要由用户设置该值或将其作为消息的一部分;除非您随机生成相同的数字,否则使用随机数将无法解密消息

  • 我是否需要为这两个函数创建单独的类文件

  • 不需要。此处不需要任何类(或多个文件)。

    要使用随机数进行加密,您需要确保(1)您和收件人都有完全相同的算法(而不仅仅是“本地编译器的rand()做什么”),以及(2)你们两个都用相同的键值为RNG播种,而不是像时钟这样随机的东西。如果你同时做这两件事,那么你可以做一些简单的事情,比如用RNG中的数字对消息的每个字符进行加法或异或运算,基本上是复制一个一次性键盘。然后你需要担心密钥交换。。。