2013-04-07 69 views
0

我的程序必須解密試圖進行所有可能的移位的密文 這是在函數中完成的。如果您有任何想法可以縮短這一點,我們將不勝感激。C編程 - 使這個程序更短的任何方式

char decryptCiphertext(char data[], double data2[]) 
{ 
    int i, convert, shiftingLetter; 
    char decrypted26[42], decrypted1[42], decrypted2[42], decrypted3[42], decrypted4[42], 
    decrypted5[42], decrypted6[42], decrypted7[42], decrypted8[42], decrypted9[42], decrypted10[42], 
    decrypted11[42], decrypted12[42], decrypted13[42], decrypted14[42], decrypted15[42], decrypted16[42], 
    decrypted17[42], decrypted18[42], decrypted19[42], decrypted20[42], decrypted21[42], decrypted22[42], 
    decrypted23[42], decrypted24[42], decrypted25[42]; 

    //Try all possible shifts from 1-25 and store each in an array 
    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc 
     convert = (convert + 1); //add shift 
     shiftingLetter = convert % 26; //cycle around 
     decrypted1[i] = 'A' + shiftingLetter; //store new letter in array 
    } 
    decrypted1[i] = '\0'; 
    likelyPlaintext(decrypted1, data2); 
    //printf(" 1. %s\n", decrypted1); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 2); 
     shiftingLetter = convert % 26; 
     decrypted2[i] = 'A' + shiftingLetter; 
    } 
    decrypted2[i] = '\0'; 
    //printf(" 2. %s\n", decrypted2); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 3); 
     shiftingLetter = convert % 26; 
     decrypted3[i] = 'A' + shiftingLetter; 
    } 
    decrypted3[i] = '\0'; 
    //printf(" 3. %s\n", decrypted3); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 4); 
     shiftingLetter = convert % 26; 
     decrypted4[i] = 'A' + shiftingLetter; 
    } 
    decrypted4[i] = '\0'; 
    //printf(" 4. %s\n", decrypted4); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 5); 
     shiftingLetter = convert % 26; 
     decrypted5[i] = 'A' + shiftingLetter; 
    } 
    decrypted5[i] = '\0'; 
    //printf(" 5. %s\n", decrypted5); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 6); 
     shiftingLetter = convert % 26; 
     decrypted6[i] = 'A' + shiftingLetter; 
    } 
    decrypted6[i] = '\0'; 
    //printf(" 6. %s\n", decrypted6); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 7); 
     shiftingLetter = convert % 26; 
     decrypted7[i] = 'A' + shiftingLetter; 
    } 
    decrypted7[i] = '\0'; 
    //printf(" 7. %s\n", decrypted7); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 8); 
     shiftingLetter = convert % 26; 
     decrypted8[i] = 'A' + shiftingLetter; 
    } 
    decrypted8[i] = '\0'; 
    //printf(" 8. %s\n", decrypted8); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 9); 
     shiftingLetter = convert % 26; 
     decrypted9[i] = 'A' + shiftingLetter; 
    } 
    decrypted9[i] = '\0'; 
    //printf(" 9. %s\n", decrypted9); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 10); 
     shiftingLetter = convert % 26; 
     decrypted10[i] = 'A' + shiftingLetter; 
    } 
    decrypted10[i] = '\0'; 
    //printf(" 10. %s\n", decrypted10); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 11); 
     shiftingLetter = convert % 26; 
     decrypted11[i] = 'A' + shiftingLetter; 
    } 
    decrypted11[i] = '\0'; 
    //printf(" 11. %s\n", decrypted11); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 12); 
     shiftingLetter = convert % 26; 
     decrypted12[i] = 'A' + shiftingLetter; 
    } 
    decrypted12[i] = '\0'; 
    //printf(" 12. %s\n", decrypted12); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 13); 
     shiftingLetter = convert % 26; 
     decrypted13[i] = 'A' + shiftingLetter; 
    } 
    decrypted13[i] = '\0'; 
    //printf(" 13. %s\n", decrypted13); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 14); 
     shiftingLetter = convert % 26; 
     decrypted14[i] = 'A' + shiftingLetter; 
    } 
    decrypted14[i] = '\0'; 
    //printf(" 14. %s\n", decrypted14); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 15); 
     shiftingLetter = convert % 26; 
     decrypted15[i] = 'A' + shiftingLetter; 
    } 
    decrypted15[i] = '\0'; 
    //printf(" 15. %s\n", decrypted15); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 16); 
     shiftingLetter = convert % 26; 
     decrypted16[i] = 'A' + shiftingLetter; 
    } 
    decrypted16[i] = '\0'; 
    //printf(" 16. %s\n", decrypted16); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 17); 
     shiftingLetter = convert % 26; 
     decrypted17[i] = 'A' + shiftingLetter; 
    } 
    decrypted17[i] = '\0'; 
    //printf(" 17. %s\n", decrypted17); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 18); 
     shiftingLetter = convert % 26; 
     decrypted18[i] = 'A' + shiftingLetter; 
    } 
    decrypted18[i] = '\0'; 
    //printf(" 18. %s\n", decrypted18); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 19); 
     shiftingLetter = convert % 26; 
     decrypted19[i] = 'A' + shiftingLetter; 
    } 
    decrypted19[i] = '\0'; 
    //printf(" 19. %s\n", decrypted19); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 20); 
     shiftingLetter = convert % 26; 
     decrypted20[i] = 'A' + shiftingLetter; 
    } 
    decrypted20[i] = '\0'; 
    //printf(" 20. %s\n", decrypted20); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 21); 
     shiftingLetter = convert % 26; 
     decrypted21[i] = 'A' + shiftingLetter; 
    } 
    decrypted21[i] = '\0'; 
    //printf(" 21. %s\n", decrypted21); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 22); 
     shiftingLetter = convert % 26; 
     decrypted22[i] = 'A' + shiftingLetter; 
    } 
    decrypted22[i] = '\0'; 
    //printf(" 22. %s\n", decrypted22); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 23); 
     shiftingLetter = convert % 26; 
     decrypted23[i] = 'A' + shiftingLetter; 
    } 
    decrypted23[i] = '\0'; 
    //printf(" 23. %s\n", decrypted23); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 24); 
     shiftingLetter = convert % 26; 
     decrypted24[i] = 'A' + shiftingLetter; 
    } 
    decrypted24[i] = '\0'; 
    //printf(" 24. %s\n", decrypted24); 

    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 25); 
     shiftingLetter = convert % 26; 
     decrypted25[i] = 'A' + shiftingLetter; 
    } 
    decrypted25[i] = '\0'; 
    //printf(" 25. %s\n", decrypted25); 

} 

我的程序有解密的密文嘗試這是在函數中完成所有可能發生的變化 。如果您有任何想法可以縮短這一點,我們將不勝感激。

+1

這屬於在代碼審查。 – 2013-04-07 23:58:25

+0

首先將所有這些向量打包到一個雙數組中(例如解密的[] []),或者最好將它們包含到元素正確對齊的數組中。將所有這些循環包裝成一個循環(容易)。 – 2013-04-07 23:59:57

回答

1

首先,每個for循環可能是一個函數,您將變量傳遞給。

記得幹。不要重複自己。

我不知道C但下面應該是一個函數,你傳遞解密4和26並返回結果。

for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; 
     convert = (convert + 4); 
     shiftingLetter = convert % 26; 
     decrypted4[i] = 'A' + shiftingLetter; 
    } 
+0

我需要將所有移位的字符存儲在數組中。所以如果它被移動了1我需要它在一個數組中,如果它被移動了2,也需要在另一個數組中,這樣我可以計算出最高頻率來找到最可能的明文。對不起,我對c編程有點新鮮。 – user2255744 2013-04-07 23:59:59

1

這就是:

int i, j, convert, shiftingLetter; 
    char decryptedArr[25][42]; 

    //Try all possible shifts from 1-25 and store each in an array 
    for (j = 0; j < 25; j++) 
    for (i = 0; data[i] != '\0'; i++) 
    { 
     convert = data[i] - 'A'; //convert letters to equal 0-26 ex:A=0,B=1,C=2,etc 
     convert = (convert + i + 1); //add shift 
     shiftingLetter = convert % 26; //cycle around 
     decryptedArr[j][i] = 'A' + shiftingLetter; //store new letter in array 
    } 
    decryptedArr[j][i] = '\0'; 
+0

我從來沒有學過二維數組。我試過你的代碼,但我得到錯誤。 – user2255744 2013-04-08 00:25:52

+0

請再次檢查,我忘了修改一些東西,現在應該沒問題 – 2013-04-08 01:01:54

+0

從類型int爲此行分配類型char [42]時,出現錯誤消息,說明不兼容類型decryptedArr [j] ='A'+ shiftingLetter ; – user2255744 2013-04-08 01:07:37