2016-05-14 91 views
-1

我解决了一些与旧代码有关的问题。现在的问题是解密已经用我的程序加密的密文时,它给出的字母与原始字母不同;因此字 - >加密...然后加密 - >解密给出解密!=字,甚至在纸上密文应该与我得到的不同。 还有一件事:我试过{...} while(strlen(...,...)!= 0),但它不工作,并试图比较sizeof也无法正常工作。我应该如何比较关键字与单词的长度?加密解密错误(一次性密码加密)

这是我的新代码使用XOR:

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#define clear_buffer while(getchar()!='\n'); 
char* encrypt(char texte[],char cle[]){ 
    char *encrypted; 
    int i=0; 
    while(texte[i]!='\0' && cle[i]!='\0'){ 
    if (texte[i] >= 'A' && texte[i] <= 'Z') 
    encrypted[i] = ((texte[i] - 'A')^(cle[i] - 'A')) % 26 + 'A'; 
else if (texte[i] >= 'a' && texte[i] <= 'z') 
    encrypted[i] = ((texte[i] - 'a')^(cle[i] - 'a')) % 26 + 'a'; 
     i++; 
    } 
    encrypted[i+1]='\0'; 
    return encrypted; 

    } 
char* decrypt(char encrypted[],char cle[]){ 
    char *decrypted; 
    int i=0; 

    while(encrypted[i]!='\0' && cle[i]!='\0'){ 
    if (encrypted[i] >= 'A' && encrypted[i] <= 'Z') 
    decrypted[i] = ((encrypted[i] - 'A')^(cle[i] - 'A')) % 26 + 'A'; 
else if (encrypted[i] >= 'a' && encrypted[i] <= 'z') 
    decrypted[i] = ((encrypted[i] - 'a')^(cle[i] - 'a')) % 26 + 'a'; 
     i++; 
    } 
    decrypted[i+1]=0; 
    return decrypted; 

} 
int main() 
{ 
    char reponse,texte[100],cle[100],encrypted[100]; 
    int i=0; 

    do{ 
     printf("Voulez vous crypter ou decrypter un texte?(Ecrire C pour crypter et D pour decrypter)\n"); 
     scanf("%c",&reponse); 
    }while (reponse!='C'&& reponse!='D'&& reponse!='c'&& reponse!='d');//controle pour obliger l'utilisateur à donner c ou d 
    if(reponse=='C'||reponse=='c'){ 
      clear_buffer;//vider le buffer apres le scanf de la reponse 
       //do{ 
     printf("Donner un texte a crypter\n"); 
     fgets(texte,100,stdin); 
     while(texte[i]!=0) 
      i++; 
     if (i>0 && texte[i-1]!='\n') 
      clear_buffer; 
     printf("Donner une cle de meme taille\n"); 
     fgets(cle,100,stdin); 
      //}while(sizeof texte!=sizeof cle); 
     i=0; 
     while(cle[i]!=0) 
      i++; 
     if (i>0 && cle[i-1]!='\n') 
      clear_buffer; 
     printf("Le texte crypte est:%s\n",encrypt(texte,cle)); 
    }else{ 
      clear_buffer;//vider le buffer apres le scanf de la reponse 
     // do{ 
     printf("Donner un texte (deja crypte) à decrypter\n"); 
     fgets(encrypted,100,stdin); 
     i=0; 
     while(encrypted[i]!=0) 
      i++; 
     if (i>0 && encrypted[i-1]!='\n') 
      clear_buffer; 
     printf("Donner la cle (deja utilisee pour crypter\n"); 
     fgets(cle,100,stdin); 
     i=0; 
     while(cle[i]!=0) 
      i++; 
     if (i>0 && cle[i-1]!='\n') 
      clear_buffer; 
     //}while(sizeof encrypted!=sizeof cle); 
     printf("Le texte decrypte est:%s\n",decrypt(encrypted,cle)); 
    } 
    system("pause"); 
    return 0; 
} 
+2

*请考虑使用合适的代码格式标准。你的代码很难阅读。谷歌“C编码标准”,如果你想要一些想法。如果你正在运行Linux,你可以使用'indent'命令(见'man indent')在事实之后自动缩进你的代码。遵循良好的标准将帮助您遵循自己的代码,这有助于调试和修改。请注意,Stackoverflow.com问题区域不支持标签。建议使用空格。 – lurker

+1

我刚刚在你的代码上运行'indent'。 :) – lurker

+0

非常感谢你lurker :) – user105453

回答

1

的问题是scanf函数在缓冲区的第一与fgets遇到留下了一个换行符。不要使用scanf键盘输入;这种方式就是疯狂。

另外,该线路

encrypted = (texte[i] + cle[i]) % 26; 

的方式是错误的。考虑你正在尝试做什么。也许如下:

if (texte[i] >= 'A' && texte[i] <= 'Z') 
    encrypted = (texte[i] - 'A' + cle[i] - 'A') % 26 + 'A'; 
else if (texte[i] >= 'a' && texte[i] <= 'z') 
    encrypted = (texte[i] - 'a' + cle[i] - 'a') % 26 + 'a'; 

与解密相同的更改。通常,这是一个坏主意,让加密的文本可打印这些天,但...

编辑:添加 - “A”和 - “a”到两个CLE [I]

+0

谢谢。我修改了我的代码。但仍然显示程序没有响应:( – user105453

+0

你可能有更多的问题,使用printf自由地找出它挂起的地方 – Joshua

0

你覆盖每次'i'加密数据的值发生变化。 char encrypted[100];

你的代码应该变成:

if (texte[i] >= 'A' && texte[i] <= 'Z') 
    encrypted[i] = (texte[i] - 'A' + cle[i] - 'A') % 26 + 'A'; 
else if (texte[i] >= 'a' && texte[i] <= 'z') 
    encrypted[i] = (texte[i] - 'a' + cle[i] - 'a') % 26 + 'a';` 

,如果你打算使用XOR,它会变成这个样子:

可以通过声明加密为指针或表修复
if (texte[i] >= 'A' && texte[i] <= 'Z') 
    encrypted[i] = ((texte[i] - 'A')^(cle[i] - 'A')) % 26 + 'A'; 
else if (texte[i] >= 'a' && texte[i] <= 'z') 
    encrypted[i] = ((texte[i] - 'a')^(cle[i] - 'a')) % 26 + 'a';` 

我用一些额外的括号来澄清。

希望这会有所帮助。

+0

非常感谢:) – user105453

+0

我用新的代码替换旧的代码。它现在提供可读的字母。但是,当试图解密一个加密的单词时,它不会给出原始单词(它给出了其他的东西:顺便说一句,它也使用不仅是字母的符号) – user105453

+0

你的加密和解密方法并不是最优的。不要使用(文本+键)和(文本 - 键)技术,请尝试使用Xor。它的工作原理如下:cleartext^key =加密文本和加密文本^ key = cleartext。如果仍然出现错误,请在每行说明之后尝试使用printf,以便逐步验证。 – Mincer