简体   繁体   English

Crypto-Js 与 c#

[英]Crypto-Js with c#

I am learning about encryption and using crypto-js I have made a Js & c# version.我正在学习加密并使用crypto-js我制作了一个Js & c#版本。 What I am trying to accomplish is that the JS or c# version will be able to decode each other messages.我想要完成的是 JS 或 c# 版本将能够解码彼此的消息。

For testing I have kept the IV and KEY , paddding and mode the same in both the JS and C# instance.为了测试,我在 JS 和 C# 实例中保持了IVKEY填充模式相同。

I have them both decrypting and encrypting data respectivly but what I have yet to accomplish is providing an encrypted from JS be able to decode using c#.我让他们分别解密和加密数据,但我尚未完成的是提供从 JS 加密的能够使用 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# 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);
            }


        }

Where the problem arises in decryption.解密出现问题的地方。

  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;

    }

I am producing different encrypted size strings:我正在生成不同的加密大小字符串:

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

I get Padding is invalid and cannot be removed, when trying to decrypt the JS string in c# Where have I got wrong?.尝试在 c# 中解密 JS 字符串时,我得到 Padding is invalid and cannot be removed, 我哪里出错了?。

Basically you run into encoding issues.基本上你会遇到编码问题。 First of all, you parse your IV using Base64 decoding in one implementation and one using direct character encoding in the other.首先,您在一个实现中使用 Base64 解码解析您的 IV,在另一个实现中使用直接字符编码。 Your Base64 strings don't look like Base64 strings either.您的 Base64 字符串看起来也不像 Base64 字符串。

Furthermore, many libraries (incorrectly) allow that incorrect key and IV sizes are used.此外,许多库(错误地)允许使用不正确的密钥和 IV 大小。 This is however confusing as there is no generic way for key or IV expansion.然而,这令人困惑,因为没有用于键或 IV 扩展的通用方法。 So you should make sure that the binary representations of the key and IV are correct for the specific algorithm.因此,您应该确保密钥和 IV 的二进制表示对于特定算法是正确的。

For AES you should use a key size of 128, 192 or 256 bits and an IV size identical to the block size, 128 bits.对于 AES,您应该使用 128、192 或 256 位的密钥大小和与块大小相同的 IV 大小,128 位。 The IV should be randomly generated and communicated to the other side, eg by prefixing the IV to the ciphertext. IV 应该随机生成并传送到另一方,例如通过在密文前添加 IV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM