如何在Crypto JS中实现AES 128邮差



我有这样的c#代码:

public string Encrypt( string aesKey)
{            
var data = "ASD_POC";
var aesKey = "OxLDVPTHLk5EHR5AE8O0rg==";            
var token = Encoding.UTF8.GetBytes(data);
byte[] _key = Convert.FromBase64String(aesKey);
string retnResult = string.Empty;
AesCryptoServiceProvider aesProvider = new System.Security.Cryptography.AesCryptoServiceProvider();
MemoryStream memStream = null;
CryptoStream cryptoStream = null;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
byte[] cipherText;
try
{
aesProvider.Mode = CipherMode.CBC;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.BlockSize = 128;
aesProvider.KeySize = 128;
aesProvider.GenerateIV();
memStream = new MemoryStream();
cryptoStream = new CryptoStream(memStream, aesProvider.CreateEncryptor(_key, aesProvider.IV), CryptoStreamMode.Write);
cryptoStream.Write(token, 0, token.Length);
cryptoStream.FlushFinalBlock();
cipherText = memStream.ToArray();
var combinedIvCipherText = new byte[aesProvider.IV.Length + cipherText.Length];
Array.Copy(aesProvider.IV, 0, combinedIvCipherText, 0, aesProvider.IV.Length);
Array.Copy(cipherText, 0, combinedIvCipherText, aesProvider.IV.Length, cipherText.Length);
retnResult = Convert.ToBase64String(combinedIvCipherText);
}

我已经尝试为邮差集合实现这个:

var CryptoJS = require("crypto-js");
// Generate random 16 bytes to use as IV
var IV = CryptoJS.lib.WordArray.random(16);
var data = "ASD_POC";
var aesKey = "OxLDVPTHLk5EHR5AE8O0rg=="; 
var tokenData = atob(aesKey);
var Key = Uint8Array.from(tokenData, b => b.charCodeAt(0));
function encrypt(data) {
var val = CryptoJS.enc.Utf8.parse(data);
var encrypted = CryptoJS.AES.encrypt(
val, 
Key, 
{ 
iv: IV,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
keySize: 128,
BlockSize: 128
}).toString();
console.log(encrypted);
var b64 = CryptoJS.enc.Base64.parse(encrypted).toString(CryptoJS.enc.Hex);

console.log(b64)    
return b64;
}

但是当我试图从CryptoJs字符串-我得到了一个错误,我不能。我在实现中做错了什么?我试着在谷歌上搜索更多关于CryptoJs的信息,但找不到更多的信息,而且它不包含很多信息。

CryptoJS代码缺少IV和密文的连接

此外,在某些情况下会使用错误的编码。CryptoJS与WordArrays一起工作,并提供了不同的编码器来转换到这种类型。

使用CryptoJS的一个可能实现是:

var data = "ASD_POC";
var aesKey = "OxLDVPTHLk5EHR5AE8O0rg=="; 
var ivWA = CryptoJS.lib.WordArray.random(16);
//var ivWA = CryptoJS.enc.Hex.parse("2423c5414ead3812e68a799c8c6deab7"); //for testing only
function encrypt(data, aesKey) {
var dataWA = CryptoJS.enc.Utf8.parse(data);
var keyWA = CryptoJS.enc.Base64.parse(aesKey);
var ciphertext = CryptoJS.AES.encrypt(dataWA, keyWA, {iv: ivWA}).ciphertext;
var ivCiphertext = ivWA.clone().concat(ciphertext);
var ivCiphertextB64 = ivCiphertext.toString(CryptoJS.enc.Base64);
return ivCiphertextB64;
}
document.getElementById("ct").innerHTML = encrypt(data, aesKey);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
<p style="font-family:'Courier New', monospace;" id="ct"></p>

作为测试,可以用c#代码创建密文,例如JCPFQU6tOBLminmcjG3qt+JeEXrh5/9x+QSc7Emc6cQ=。前16个字节是IV,它是十六进制编码的2423c5414ead3812e68a799c8c6deab7。如果在CryptoJS代码中使用此IV(注释掉行),则产生相同的密文。

最新更新