带c#的Crypto-Js

时间:2013-02-15 15:25:21

标签: encryption cryptography aes

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

为了测试我在JS中保持了 IV KEY paddding 模式和C#实例。

我让他们分别对数据进行解密和加密,但我还要完成的是提供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无效且无法删除 我哪里错了?。

1 个答案:

答案 0 :(得分:3)

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

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

对于AES,您应使用128,192或256位的密钥大小以及与块大小相同的IV大小(128位)。 IV应随机生成并传达给另一方,例如通过在IV前面加密密文。