具有c#的CryptoJs

问题描述 投票:-1回答:2

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

为了进行测试,我在JS和C#实例中都将IVKEYpadddingmode保持相同。

我让它们分别解密和加密数据,但是我还没有完成的是提供从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无效并且无法删除我在哪里弄错了?。

encryption cryptography aes
2个回答
3
投票
此外,许多库(错误地)允许使用错误的密钥和IV大小。但是,由于没有通用的密钥或IV扩展方式,这令人困惑。因此,您应该确保密钥和IV的二进制表示对于特定算法是正确的。

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


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.