0

キー rot7 と rot13 を使用する小さな暗号化プログラムを作成しました。2 つの 6 文字 uvwxyz を除いて、すべて正常に動作します。

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;

この範囲を許可した場合、小文字ではなく大文字が機能する可能性はありますか? それは他の何かのせいでしょうか?これらのキャプチャを段階的に追加しました...これが役立つことを願っています

4

1 に答える 1

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 より大きくなります。

この場合、モジュロ%演算子を使用する必要があります。これは、シフトだけでなく回転です

于 2013-04-09T15:47:13.723 に答える