2013-04-09 58 views
0

我已經制作了一個小型加密程序,用於對rot7和rot13鍵。一切工作正常,除了兩個6字母uvwxyz。加密和解密ASCII表的一部分

如果輸入ABCDEFGHIJKLMNOPQRSTUVWXYZ,那麼加密和解密沒問題。但是,如果我輸入相同的小寫字母,則uvwxyz不起作用。

話雖如此,我已經允許ASCII表內所有可寫的字符爲有效範圍如下:

// allow all writable characters from 32 to 255 
if ((str[i] >= 32) && (str[i] <=255)) 
{ 
    str[i] -= key; 
} 

這裏是加密的過程:

cout << endl; 
    cout << "Encrypting process started " << endl << endl; 
    cout << "--------------------------- " << endl; 

    //get the string length 
    int i = 0; 
    int length = str.length(); 
    int key = rot13 ; 
    int k = 5; 
    int multiple = 0; 
    int count = 0; 

    cout << "the text to encrypt is: " << str << endl; 
    cout << "text length is: " << length << endl; 
    cout << "using rot13"<<endl; 
    cout <<"---------------------------" << endl; 
    cout << "using rot13" << endl; 

    //traverse the string 
    for(i = 0; i < length; i++) 
    { 

     count ++; 

     cout << left; 

     //if it is a multiple of 5 not the first character change the key 
     if((multiple = ((i % 5) == 0)) && (count != 1) && (key == rot13)){ 

      key = rot7; 


     } 
     //if it is a multiple of 5 not the first character change the key 
     else if((multiple = ((i % 5) == 0)) && (count != 1) && (key == rot7)) { 

      key = rot13; 


     } 


     // Capital letters are 65 to 90 (a - z) 
     if ((str[i] >= 32) && (str[i] <= 255)) 
     { 
      str[i] += key; 
     } 


    } 
    return str; 

怎麼回事如果我允許這個範圍,大寫字母可能會起作用,而不是小寫字母?它可能是因爲別的嗎?我已經添加了這些捕獲與發生的事情一步一步...希望這有助於

+0

你能告訴我們它不工作? – 2013-04-09 15:25:33

+0

當然,我可以編輯並提供一個捕獲 – bluetxxth 2013-04-09 15:27:58

+0

嗯,我的意思只是告訴我們完整的代碼和它給你的輸出。 – 2013-04-09 15:29:25

回答

4

在您的代碼:

if ((str[i] >= 32) && (str[i] <= 255)) 
     { 
      if (str[i] + key > 255) 
       str[i] = ((str[i] + key) % 255)+ 32; 
      else 
       str[i] += key; 
     } 

如果key有13 str[i]的值是「U」或更大,str[i]有超過255

你應該在這種情況下使用模%運營商高的值,這是旋轉,不僅是轉變

+0

你的意思是關鍵? – bluetxxth 2013-04-09 15:49:07

+0

這樣的事情? range str [i] =(str [i] + key)%character_range; – bluetxxth 2013-04-09 15:49:48

+0

255 + 32有點。嘗試編寫字符串的值,以便您可以自己觀察。順便說一句。 – 2013-04-09 15:50:44