2017-10-29 55 views
-1
int main(void) { 

    char plain[6] = "lorem"; 
    char cipher[27] = "nbajyfowlzmpxikuvcdegrqsth"; 
    int length = strlen(plain); 
    char encrypted_text[6] = "pkcyx"; 

    /*encryption*/ 
    for (int i = 0 ; i < length ; i++) { 
     plain[i] = cipher[plain[i] - 97]; 
     //printf("%c", plain[i]); 
    } 

    /*decryption*/ 
    for (int i = 0 ; i < length ; i++) { 
     encrypted_text[i] = cipher index of encryted text.... how to write this... 
     eg: cipher index of o is 6 (0-n ,1-b, 2-a, 3-j, 4-y, 5-f, 6-o) ... i want this index so i + 97 will decrypt the msg 
     //printf("%c", plain[i]); 
    } 
} 

我想知道如何得到一個加密的信密碼指數?解密在C單字母替代密碼

回答

0

plain[i] = cipher[plain[i] - 97];覆蓋平原,你不應該寫入encrypted_text?另外,plain[i] - 97不可能成爲密碼數組的有效索引。

encrypted_text[i] = cipher index of encrypted text....只是錯誤,您應該解密encrypted_text

解密替代密碼要求您知道密碼,並使用該密碼反轉在純文本上執行的任何操作。在你的情況下,初始加密操作只是無效的C代碼。

+0

@ jwdonahue感謝您的回覆! 我跑這個視覺工作室代碼,輸出:pkcyx(這將隨後被加密) 的#include 的#include INT主(無效){ 炭平原[6] = 「LOREM」; char cipher [27] =「nbajyfowlzmpxikuvcdegrqsth」; int length = strlen(plain); /* encryption */ for(int i = 0; i Beansprout

+0

如果給出 plain =「pkcyx」; cipher [27] =「nbajyfowlzmpxikuvcdegrqsth」; 要解密這個,我只能想到使用密碼的索引號和字母代表的+97來達到原始字母的小數。對於這種情況,p是密碼[11],11 + 97會給我108這是原始字母「l」(L的小寫字母)。 但我不知道如何編寫代碼。你能給我一些關於實現部分的提示嗎? – Beansprout