2016-10-15 35 views
-2

Ceaser密碼不考慮加密中的數字。如何擴展它,以便用字母和數字替換每個字母,然後用XOR每個字符加上一個加密密鑰。這是我到目前爲止編寫的加密代碼...我可以知道我的錯在哪裏....輸出不準確。Ceaser密碼的擴展

string Encrypt (string s) 
{ 
    string cipher = s; 
    int size = s.length(); 

    for(int i = 0; i < size ; i++) 
    { 
     if(isalpha(cipher[i])) 
     { 
      int x = static_cast<int>(cipher[i]) ; //changing to ascii code 

      if ((x >= 88 && x <= 90) || (x >= 120 && x <= 122)) 
       x = x - 23; 
      else 
       x = x + 3; 

      cipher[i] = static_cast<char>(x); //changing back to ascii char 
      cipher[i] ^= 5;     // XOR with 5 
     } 
     else if(isdigit(cipher[i])) 
     { 
      int x = static_cast<int>(cipher[i]) ; 
      cout << x ; 

      if (x >= 55 && x <= 57) 
       x = x - 7; 
      else 
       x = x + 3; 

      cipher[i] = static_cast<char>(x); 
      cipher[i] ^= 5;                                    
     }  
    }  
    return cipher ; 
} 
+0

你所描述的不再是凱撒密碼,所以它的實現完全取決於你。 – duskwuff

+0

有很多人已經解決了這個任務。您是否嘗試尋找他們的解決方案,例如通過做一個簡單的搜索? –

回答

0

您可以按照link關於凱撒密碼,你可以很容易地擴展到您的用例。簡單地用你的算法用另一個數字替換數字(例如,如果它是一個數字,你可以乘以2並替換)同樣也可以用於字符。然後你可以使用你自己的邏輯來解密它。

+0

我想要做的是用另一個字母替換每個字母,並用另一個數字替換每個數字,偏移量爲3 .....我迄今爲止只做了這個...但輸出不準確.. ..現在我不知道我做錯了 –

+0

我編輯了問題@hariprasath –