2017-12-22 27 views
0

私は現在、単純なシーザー暗号解読を実装するために、バイト単位でソリッドにオフセットしようとしています。しかし、私はこれを行う方法を理解することはできません。ここでリミックスで、いくつかのコンパイルエラーを与える私の現在のコードは、次のとおりです。どのようにしてバイトを整数でオフセットしますか?

TypeError: Expression has to be an lvalue. 
      decryptedData[i] = (decryptedData[i] - key) % 256; 
      ^--------------^ 

TypeError: Operator - not compatible with types bytes1 and int256 
      decryptedData[i] = (decryptedData[i] - key) % 256; 
           ^--------------------^ 

TypeError: Operator % not compatible with types bytes1 and int_const 256 
      decryptedData[i] = (decryptedData[i] - key) % 256; 
           ^----------------------------^ 

ありがとう:

function decrypt(bytes32 data, int key) public returns (bool) { 
    bytes32 decryptedData = data; // `data` here is the encrypted data 
    // decryption 
    for (int i = 0; i < decryptedData.length; i++) { 
     decryptedData[i] = (decryptedData[i] - key) % 256; 
    } 
    // returns if sha256(decryptedData) matches some value 
} 

しかし、これは私に次のエラーを与えます!

+0

「インデックスアクセス」セクションを参照してください。「キー」はどこに定義されていますか? –

+0

右は、関数パラメータのintとしてキーが定義されています!私はそれについて残念な事を更新します。 – aevumcessi

+0

したがって、質問に示唆されているようにインデックスの数だけバイトを移動していないので、示されたアルゴリズムはシーザー暗号のアルゴリズムではありません。 –

答えて

0

ダミアングリーン氏によると、私はあなたが書きたいアルゴリズムについて少し混乱していますが、以下の契約ではシーザーの暗号化されたテキストを解読します。あなたのニーズに合わせて変更することができます。 (ASCII値の怠惰なハードコーディングを許してください)。

bytes32は、Solidityの特別な配列として扱われ、読み取り専用であるため使用できません。 http://solidity.readthedocs.io/en/develop/types.html#fixed-size-byte-arrays

pragma solidity ^0.4.17; 

contract CaesarDecryption { 
    function decrypt(bytes data, int key) pure public returns (bytes) { 
     bytes memory decryptedData = data; 

     for (uint i = 0; i < decryptedData.length; i++) { 
      decryptedData[i] = decryptByte(decryptedData[i], key); 
     } 

     return decryptedData; 
    } 

    function decryptByte(byte b, int k) pure internal returns (byte) { 
     uint8 ascii = uint8(b); 
     uint8 asciiShift; 

     if (ascii >= 65 && ascii <= 90) 
     asciiShift = 65; 
     else if (ascii >= 97 && ascii <=122) 
     asciiShift = 97; 

     return byte(((ascii - asciiShift - k + 26) % 26) + asciiShift); 
    } 
} 
関連する問題