Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/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 将密文转换为明文的逆向工程公式_C_Algebra_Cs50_Vigenere - Fatal编程技术网

C 将密文转换为明文的逆向工程公式

C 将密文转换为明文的逆向工程公式,c,algebra,cs50,vigenere,C,Algebra,Cs50,Vigenere,我有以下公式: ciphertext[i] = ((plaintext[i] -'a' + k) % 26) + 'a'; 我正试图解决给定密文[I]的明文[I]问题 我用的是维格纳密码。如果我把明文转换成密文,难道没有办法把密文转换成明文吗 以下是完整的代码: #include <cs50.h> #include <stdio.h> #include <string.h> #include <ctype.h> int main(int

我有以下公式:

ciphertext[i] =  ((plaintext[i] -'a' + k) % 26)  + 'a';
我正试图解决给定密文[I]的明文[I]问题

我用的是维格纳密码。如果我把明文转换成密文,难道没有办法把密文转换成明文吗

以下是完整的代码:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>



int main(int argc, string argv[]){

if (argc !=2){
    printf("FATAL ERROR: All hail Caesar!");
    return 1;
}

for(int i = 0, n=strlen(argv[1]);i < n;i++){
    if(!isupper(argv[1][i]) && !islower(argv[1][i])){
       printf("FATAL ERROR: All hail Mark Anthony!");
       return 1;
    }
}

int cipherlength = strlen(argv[1]),ciphercount=0;
char cipherkey[strlen(argv[1])];
for(int i = 0, n=strlen(argv[1]);i < n;i++){
    cipherkey[i] = argv[1][i];
}

string plaintext = NULL;
int k;

do{
   printf("plaintext:  ");
   plaintext = get_string();
   printf("ciphertext: ");
}while(plaintext == NULL);



char ciphertext[strlen(plaintext)];
char xiphertext[strlen(plaintext)];

k = atoi(argv[1]);

for (int i = 0,n=strlen(plaintext);i < n; i++){

    int index = ciphercount % cipherlength;

    if(isupper(cipherkey[index])) k = cipherkey[index] - 'A';
    else if(islower(cipherkey[index])) k = cipherkey[index] - 'a';

    if      ((int)plaintext[i] >= 65 && (int)plaintext[i] <= 90){
       ciphertext[i] =  ((plaintext[i] -'A' + k) % 26)  + 'A';
       xiphertext[i] =  ((plaintext[i] -'A' - k) % 26)  + 'A';
       ciphercount++;
    }else if ((int)plaintext[i] >= 97 && (int)plaintext[i] <= 122){
       ciphertext[i] =  ((plaintext[i] -'a' + k) % 26)  + 'a';
       xiphertext[i] =  ((plaintext[i] -'a' - k) % 26)  + 'a';
       ciphercount++;
    }else {
        ciphertext[i] = plaintext[i];
        xiphertext[i] = plaintext[i];
    }
    printf("%c %c %c /n",plaintext[i],ciphertext[i],xiphertext[i]);
}
printf("\n");

}
但当我再次输入rbpgsrxhvmyxdnbsedjthykjpz时,我得到的关键词是random:

rbpgsrxhvmyxdnbsedjthykjpz as ciphertext[]
abcdefghijklSnUpWXYt[v]^_z  as xiphertext[]

因此,它一直到字母m,你无法求解它,因为
%26
操作为不同的
明文[i]
生成相同的值。通过计算,您可以生成一个可能的、包含大量可能性的纯文本

plaintext[i] = ((ciphertext[i]-'a'- k) % 26) + 'a';

谢谢,我需要添加以下内容:

 if ((plaintext[i] - 'a' - k)%26 < 0)xiphertext[i] = ((plaintext[i] -'a' - k + 26) % 26)  + 'a';
if((明文[i]-'a'-k)%26<0)xiphertext[i]=((明文[i]-'a'-k+26)%26)+'a';

请提供有关该问题或问题的更多详细信息。我无法计算数学。如何反转模函数?我用的是维格纳密码。如果我将纯文本转换为密码文本,难道没有办法将密码文本转换回纯文本吗?@DCR
%26
将为
'a'
生成相同的值{'
。除非确保所有输入都由字母字符组成,否则无法还原原始值。因此,我添加了代码,它们都是字母,但反之则不是working@DCR看看我的演示链接,它正确地反转了结果。对于较大的
k
值,您可能需要添加
26
to
密文[i]-'a'-k
,即
密文[i]-'a'-k+26
或甚至
密文[i]-'a'-k+52
。维格纳密码只对字母字符进行编码。因此,如果编码为
a
{/code>产生相同的值,则将其解码为
a