-1

私は暗号化について学んでおり、crypto-jsを使用してJs & c#バージョンを作成しました。私が達成しようとしているのは、JS または C# バージョンが互いのメッセージをデコードできるようにすることです。

テストのために、IVKEYパディングモードを JS と C# インスタンスの両方で同じにしました。

私はそれらにそれぞれデータの復号化と暗号化の両方を行っていますが、まだ達成していないのは、c# を使用してデコードできるように JS から暗号化されたものを提供することです。

JS

var key = CryptoJS.enc.Base64.parse('7061737323313233'); 
var iv = CryptoJS.enc.Base64.parse('7061737323313233'); 
var encrypted = CryptoJS.AES.encrypt("It works", key, 
 { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7 }); 

var decrypted = CryptoJS.AES.decrypt(encrypted, key, { 
keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); 

document.write('Encrypted :' + encrypted + '<br>');
document.write('Key :' + encrypted.key + '<br>');
document.write('Salt :' + encrypted.salt + '<br>');
document.write('iv :' + encrypted.iv + '<br>');
document.write('Decrypted : ' + decrypted + '<br>');
document.write('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8) + '<br>');

C#

  public void startEncryption(string original )
        {

            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {
                //Settings
                myRijndael.Mode = CipherMode.CBC;
                myRijndael.Padding = PaddingMode.PKCS7;
                myRijndael.FeedbackSize = 128;

                keybytes = Encoding.UTF8.GetBytes("7061737323313233");
                //Should be made unique for each message!. TODO
                iv = Encoding.UTF8.GetBytes("7061737323313233");

                // Encrypt the string to an array of bytes.
                encrypted = EncryptStringToBytes(original, keybytes, iv);

                //Show Encrypted data
                txt_Output.Text = Convert.ToBase64String(encrypted);

                // Decrypt the bytes to a string.
                string roundtrip = DecryptStringFromBytes(encrypted, keybytes, iv);

                //Display the original data and the decrypted data.
                Console.WriteLine("Original:   {0}", original);
                Console.WriteLine("Round Trip: {0}", roundtrip);
            }


        }

復号化で問題が発生する場所。

  private void btn_Decrypt_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Decrypting..");
        using (RijndaelManaged myRijndael = new RijndaelManaged())
        {
            //Settings
            myRijndael.Mode = CipherMode.CBC;
            myRijndael.Padding = PaddingMode.PKCS7;
            myRijndael.FeedbackSize = 128;

            keybytes = Encoding.UTF8.GetBytes("7061737323313233");
            //Should be made unique for each message!. TODO
            iv = Encoding.UTF8.GetBytes("7061737323313233");

            // Decrypt the bytes to a string.
            string roundtrip = DecryptToString(txt_Output.Text);

            txt_Output.Text = roundtrip;
            //Display the original data and the decrypted data.

        }
    }

  public string DecryptToString(string TextValue)
    {

        return DecryptStringFromBytes(Convert.FromBase64String(TextValue), keybytes, iv);
    }


         static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        // Create an RijndaelManaged object
        // with the specified key and IV.
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;
            rijAlg.Padding = PaddingMode.PKCS7;
            rijAlg.Mode = CipherMode.CBC;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption.
           using (MemoryStream msDecrypt = new MemoryStream(cipherText))
           {
         using (CryptoStream csDecrypt =
        new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
                 {
                 using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                 {

                  // Read the decrypted bytes from the decrypting stream
                   // and place them in a string.
                  plaintext = srDecrypt.ReadToEnd();
                 }
             }
         }

       }

      return plaintext;

    }

私は異なる暗号化されたサイズの文字列を生成しています:

JS:MhAP11fHa+fUfRzSw2UHVQ== C#:+Ijpt1GDVgM4MqMAQUwf0Q==

c# で JS 文字列を復号化しようとすると、パディングが無効であり、削除できません。

4

1 に答える 1

3

基本的に、エンコードの問題が発生します。まず、一方の実装では Base64 デコードを使用して IV を解析し、もう一方の実装では直接文字エンコーディングを使用して IV を解析します。Base64 文字列も Base64 文字列のようには見えません。

さらに、多くのライブラリでは (誤って) 不適切なキーと IV サイズの使用が許可されています。ただし、キーまたは IV 拡張の一般的な方法がないため、これは混乱を招きます。そのため、キーと IV のバイナリ表現が特定のアルゴリズムに対して正しいことを確認する必要があります。

AES の場合、128、192、または 256 ビットのキー サイズと、ブロック サイズと同じ 128 ビットの IV サイズを使用する必要があります。IV はランダムに生成し、相手側に伝達する必要があります。たとえば、IV を暗号文にプレフィックスとして付けます。

于 2013-02-15T16:34:25.433 に答える