【问题标题】:AES Encryption in C# and decryption in CryptoJSC# 中的 AES 加密和 CryptoJS 中的解密
【发布时间】:2013-09-30 12:40:42
【问题描述】:

我想在 C# 中进行 AES 加密并在 CryptoJS 中进行解密。

【问题讨论】:

  • 您的问题没有说明您为尝试解决问题所做的工作;它目前读起来像是对代码的请求。请分享您尝试的实施,并说明它如何无法满足您的要求。

标签: encryption aes cryptojs


【解决方案1】:

在获得 Google CryptoJS 小组 (https://groups.google.com/forum/#!msg/crypto-js/ysgzr2Wxt_k/_Wh8l_1rhQAJ) 的一些参考后,它现在正在运行。

这是 C#.NET 中的加密代码。

public class ClsCrypto
{
    private RijndaelManaged myRijndael = new RijndaelManaged();
    private int iterations;
    private byte [] salt;

    public ClsCrypto(string strPassword)
    {
        myRijndael.BlockSize = 128;
        myRijndael.KeySize = 128;
        myRijndael.IV = HexStringToByteArray("e84ad660c4721ae0e84ad660c4721ae0");

        myRijndael.Padding = PaddingMode.PKCS7;
        myRijndael.Mode = CipherMode.CBC;
        iterations = 1000;
        salt = System.Text.Encoding.UTF8.GetBytes("insight123resultxyz");
        myRijndael.Key = GenerateKey(strPassword);
    }

    public string Encrypt(string strPlainText)
    {
        byte [] strText = new System.Text.UTF8Encoding().GetBytes(strPlainText);
        ICryptoTransform transform = myRijndael.CreateEncryptor();
        byte [] cipherText = transform.TransformFinalBlock(strText, 0, strText.Length);

        return Convert.ToBase64String(cipherText);
    }

    public string Decrypt(string encryptedText)
    {
       byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
       var decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);
       byte[] originalBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);

       return Encoding.UTF8.GetString(originalBytes);
    }

    public static byte [] HexStringToByteArray(string strHex)
    {
        dynamic r = new byte[strHex.Length / 2];
        for (int i = 0; i <= strHex.Length - 1; i += 2)
        {
            r[i/2] = Convert.ToByte(Convert.ToInt32(strHex.Substring(i, 2), 16));
        }
        return r;
    }

    private byte[] GenerateKey(string strPassword)
    {
        Rfc2898DeriveBytes rfc2898 = new          Rfc2898DeriveBytes(System.Text.Encoding.UTF8.GetBytes(strPassword), salt, iterations);

        return rfc2898.GetBytes(128 / 8);
    }
}

以下是Java脚本中的解密代码。

<head runat="server">
        <script src="rollups/aes.js" type="text/javascript"></script>
        <script src="rollups/sha256.js" type="text/javascript"></script>
        <script src="rollups/pbkdf2.js" type="text/javascript"></script>
        <script type="text/javascript">
          function DecryptData() {
            var encryptData = document.getElementById('TextEncrypted').value;
            var decryptElement = document.getElementById('TextDecrypt');
    
            try {
                //Creating the Vector Key
                var iv = CryptoJS.enc.Hex.parse('e84ad660c4721ae0e84ad660c4721ae0');
                //Encoding the Password in from UTF8 to byte array
                var Pass = CryptoJS.enc.Utf8.parse('insightresult');
                //Encoding the Salt in from UTF8 to byte array
                var Salt = CryptoJS.enc.Utf8.parse("insight123resultxyz");
                //Creating the key in PBKDF2 format to be used during the decryption
                var key128Bits1000Iterations = CryptoJS.PBKDF2(Pass.toString(CryptoJS.enc.Utf8), Salt, { keySize: 128 / 32, iterations: 1000 });
                //Enclosing the test to be decrypted in a CipherParams object as supported by the CryptoJS libarary
                var cipherParams = CryptoJS.lib.CipherParams.create({
                    ciphertext: CryptoJS.enc.Base64.parse(encryptData)
                });
    
                //Decrypting the string contained in cipherParams using the PBKDF2 key
                var decrypted = CryptoJS.AES.decrypt(cipherParams, key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 });
                decryptElement.value = decrypted.toString(CryptoJS.enc.Utf8);
            }
            //Malformed UTF Data due to incorrect password
            catch (err) {
                return "";
            }
        }
    </script>
</head>

【讨论】:

  • 这是我的js编码/解码代码,补充你的建议:var cfg = { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 };var encrypted = CryptoJS.AES.encrypt("MMMessageąćęł", key, cfg);var sEncrypted = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);var decrypted2 = CryptoJS.AES.decrypt(sEncrypted, key, cfg);
  • 请注意,IV 应该在每次加密运行时随机启动。出于安全原因,您不能两次使用相同的 IV。 en.wikipedia.org/wiki/…
  • 谢谢@user2025187。这段代码非常有用。
猜你喜欢
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多