简体   繁体   English

Javascript解密C#AES加密

[英]Javascript Decrypt C# AES Encryption

I'm trying to decrypt a C# encrypt string using javascript, 我正在尝试使用javascript解密C#加密字符串,

This is an example of the encryption on my server side 这是服务器端加密的一个示例

 public class AesCrypt
{
    public static string IV = @"!QAZ2WSX#EDC4RFV";
    public static string Key = @"5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<";
    public static string Encrypt(string dectypted)
    {

        byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(dectypted);
        AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
        encdec.BlockSize = 128;
        encdec.KeySize = 256;
        encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
        encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
        encdec.Padding = PaddingMode.PKCS7;
        encdec.Mode = CipherMode.CBC;

        ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);

        byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
        icrypt.Dispose();

        return Convert.ToBase64String(enc);
    }
}

The encryption of "Hello World" is "1i4zI5rB3Df2CYFalsiTwg==" "Hello World"的加密为"1i4zI5rB3Df2CYFalsiTwg=="

Now I'm trying to decrypt it using js on my client and get Hello World and this is where I fail, 现在,我尝试在客户端上使用js对其进行解密,并获取Hello World ,这是我失败的地方,

I'm using <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script> in order to decrypt and I watch some of examples over the web (including stackoverflow). 我正在使用<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>进行解密,我观看了一些网络上的示例集(包括stackoverflow)。

Accoring to some examples over the web this is what I came out of, but it's not returning "Hello World" back. 根据网络上的一些示例,这是我想到的,但是它并没有返回“ Hello World”。

data = "1i4zI5rB3Df2CYFalsiTwg==";
key = "5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<";
iv = "!QAZ2WSX#EDC4RFV";

CryptoJS.AES.decrypt(atob(data), key, {
    iv: atob(iv),
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});

I'm using the same mode and padding, but I'm probably missing something. 我正在使用相同的模式和填充,但是可能缺少一些东西。 I'm not that familiar with CryptoJS and I hope someone can help me understand where I went wrong. 我对CryptoJS不太熟悉,希望有人可以帮助我了解我哪里出了问题。
Thanks in advance 提前致谢

From the old CryptoJS pages , the Cipher input part: 旧的CryptoJS页面上 ,密码输入部分:

For the key, when you pass a string, it's treated as a passphrase and used to derive an actual key and IV. 对于密钥, 当您传递字符串时,将其视为密码短语并用于派生实际密钥和IV。 Or you can pass a WordArray that represents the actual key. 或者,您可以传递代表实际键的WordArray。 If you pass the actual key, you must also pass the actual IV. 如果通过实际密钥,则还必须通过实际IV。

Although you are passing an IV, you do currently put in a string rather than a binary key as word array. 尽管您正在传递IV,但是您目前确实将字符串而不是二进制键作为单词数组。 I suppose this is the problem as I don't see any other obvious programming mistakes. 我认为这是问题所在,因为我没有看到其他明显的编程错误。

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

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