使用C#加密了字符串。 Angular 4无法解密

时间:2017-12-20 14:06:56

标签: c# angular

我使用C#加密了字符串,我希望使用Angular 4解密。但Angular代码失败。

C#代码

 public string Encrypt<T>(string value)
                   where T : SymmetricAlgorithm, new()
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes("mykey", Encoding.Unicode.GetBytes("myIV"));

            SymmetricAlgorithm algorithm = new T();

            byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
            byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);

            ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

            using (MemoryStream buffer = new MemoryStream())
            {
                using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
                {
                    using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
                    {
                        writer.Write(value);
                    }
                }

                return Convert.ToBase64String(buffer.ToArray());

                // "U2FsdGVkX1+r8rSCs7ReuU24235xwSG4kkcuKRU7nZU="
            }
        }

Angular 4 Code -

var ciphertext = "IV27YpfWyG0n3Rlfgm2LVL8D5FMqTj72awKR2LMYAhY=";

      var key = crypto.enc.Utf8.parse('mykey');
      var iv = crypto.enc.Utf8.parse('myIV');


        var decrypted = crypto.AES.decrypt(ciphertext, key, {
            keySize: 128 / 8,
            iv: iv,
            mode: crypto.mode.CBC,
            padding: crypto.pad.Pkcs7
        });

        console.log('Decrypted : ' + decrypted);
        console.log('Decrypted Value = ' + decrypted.toString(crypto.enc.Utf8));

0 个答案:

没有答案