尝试使用凯撒密码解密的多个密钥从密钥9而不是1开始

时间:2015-10-12 19:33:18

标签: c++ encryption caesar-cipher

我不知道我的代码有什么问题。它没有按预期运行。我只想使用if语句中的条件加密我的cstring中的字符,并在每次迭代键时输出所有结果(键从1到100),但即使迭代也无法正常工作。它从9开始。我在编译期间也收到以下警告:

[Warning] multi-character character constant [-Wmultichar] 
[Warning] overflow in implicit constant conversion [-Woverflow]

输出示例(如您所见,它从9开始迭代而不是1,然后上升到100):

Encrypted using key (9) : n
Encrypted using key (10) : o
Encrypted using key (11) : p
Encrypted using key (12) : q
Encrypted using key (13) : r
Encrypted using key (14) : s
Encrypted using key (15) : t
Encrypted using key (16) : u
Encrypted using key (17) : v
Encrypted using key (18) : w
Encrypted using key (19) : x
Encrypted using key (20) : y
Encrypted using key (21) : z
Encrypted using key (22) : {
Encrypted using key (23) : |
Encrypted using key (24) : }
Encrypted using key (25) : ~
Encrypted using key (26) :
Encrypted using key (27) : !

然后代码:

#include <iostream>
#include <cstring>
using namespace std;

string myfunction(char a[]);


int main()
{   
    //char a[] = ":mmZ\dxZmx]Zpgy";
    //char a[] = {':','m','m','Z',92,'d','x','Z','m','x',']','Z','p','g','y','\0'};
    //char a[] = {58,109,109,90,92,100,120,90,109,120,93,90,112,103,121,0};
    char a[] = {'e','o','i','/0'};
    cout << myfunction(a) << endl;

    return 1;
    system("pause");
}

string myfunction(char a[])
{   
    string encrypted;

    for (int i=0; i<strlen(a)-1; i++){
        cout << " Taking first character for encryption : " << a[i] << endl;
        for (int key =1; key<=100; key++)
        {
            if (a[i]+key > 126){
                encrypted= (a[i]+key)-95;
                cout << "Encrypted using key (" <<key<<") : " << encrypted << endl;}
            else{
                encrypted= a[i]+key;
                cout <<  "Encrypted using key (" <<key<<") : " << encrypted << endl;}
        }
    }

    return ("\nGood Job!");
}

1 个答案:

答案 0 :(得分:1)

char a[] = {'e','o','i','/0'};   
你的意思是

char a[] = {'e','o','i','\0'};

这可能会修复你的警告。我不知道它是否能解决你的问题 - 因为你不问一个明确的问题。