2016-05-14 92 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