-1

我正在学习加密和使用crypto-js我制作了一个Js & c#版本。我想要完成的是 JS 或 c# 版本将能够相互解码消息。

为了测试,我在 JS 和 C# 实例中保持IVKEY填充模式相同。

我让它们分别解密和加密数据,但我还没有完成的是提供从 JS 加密的能够使用 c# 解码的。

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 字符串时,我得到 Padding is invalid and cannot be removed,我哪里错了?

4

1 回答 1

3

基本上你会遇到编码问题。首先,您在一个实现中使用 Base64 解码解析您的 IV,而在另一个实现中使用直接字符编码。您的 Base64 字符串也不像 Base64 字符串。

此外,许多库(错误地)允许使用不正确的密钥和 IV 大小。然而,这令人困惑,因为没有通用的密钥或 IV 扩展方法。因此,您应该确保密钥和 IV 的二进制表示对于特定算法是正确的。

对于 AES,您应该使用 128、192 或 256 位的密钥大小和与块大小相同的 IV 大小,即 128 位。IV 应该随机生成并传送给另一方,例如通过在密文前加上 IV。

于 2013-02-15T16:34:25.433 回答