.NET和nodejs之间的AESalgorithm值差异,CryptoJS
C#中的下面的AESalgorithm返回一个不同于节点js和CryptoJS返回的encryption值。 NodeJS Crypto库和CryptoJS返回相同的值,但是.NET的AesCryptoServiceProvider返回一个不同的值。 有任何想法吗?
C#示例
private const string AesIV = @"!QAZ2WSX#EDC4RFV"; private const string AesKey = @"5TGB&YHN7UJM(IK<"; public static void Main() { try { string original = "HelloWorld"; Console.WriteLine(Encrypt(original)); } catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); } } /// <summary> /// AES Encryption /// </summary> private static string Encrypt(string text) { // AesCryptoServiceProvider AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); aes.BlockSize = 128; aes.KeySize = 128; aes.IV = Encoding.UTF8.GetBytes(AesIV); aes.Key = Encoding.UTF8.GetBytes(AesKey); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; // Convert string to byte array byte[] src = Encoding.Unicode.GetBytes(text); // encryption using (ICryptoTransform encrypt = aes.CreateEncryptor()) { byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length); // Convert byte array to Base64 strings return Convert.ToBase64String(dest); } }
NodeJS例子:
crypto = require "crypto" algo = 'aes-128-cbc' keyBuffer = new Buffer("!QAZ2WSX#EDC4RFV") ivBuffer = new Buffer("5TGB&YHN7UJM(IK<") cipher = crypto.createCipheriv(algo, keyBuffer, ivBuffer) textBuffer = new Buffer('HelloWorld') encrypted = cipher.update(textBuffer) encryptedFinal = cipher.final() encryptedText = encrypted.toString('base64') + encryptedFinal.toString('base64') console.log encryptedText
CryptoJS例子:
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var key = CryptoJS.enc.Utf8.parse('!QAZ2WSX#EDC4RFV'); var iv = CryptoJS.enc.Utf8.parse('5TGB&YHN7UJM(IK<'); var encrypted = CryptoJS.AES.encrypt("HelloWorld", key, { iv: iv }); alert(encrypted); </script>
您的C#版本使用UTF-16LE将HelloWorld转换为明文字节。 NodeJS之一(也可能是CryptoJS之一)使用UTF-8字节。 在C#中使用Encoding.UTF8.GetBytes()。
NodeJS中的默认填充是PKCS5(假设CryptoJS与您说的相同的结果相同)。 你的C#代码是设置填充到PKCS7。
- Node.js aes128encryption/解密端口到PHP
- mongodb,node.js和encryption的数据
- 使用vb.net AES / CBCencryptionstring,并需要使用JavaScript CryptoJS进行解密
- 适用于NodeJs和mbedtls的encryption程序
- 在节点中encryption并在java中解密
- 2路encryptionalgorithm,对Ruby和Node.JS只encryption到字母和数字
- 为什么节点AES128密码的输出长度为32字节?
- 解密来自PHP RIJNDAEL_128 CBC的node.js中的string
- 用python和nodejsencryption和解密