已知c#语法的DES加密函数如下
private static readonly string key = "sufei000";
public static string EncryptString(string encryptString, string encryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}
//调用:EncryptString("admin@123", key)
//结果:g9+4i2SWuuz0Kfzkgfm1Ag==
请问要如何在JavaScript里实现呢?
这是我仿照的nodejs加密函数:
const key = 'sufei000'
const arr = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]
const iv = new Int8Array(arr).buffer
function encrypt(code, alg) {
let cipher = crypto.createCipheriv(alg, key, iv);
cipher.setAutoPadding(true);
let ciph = cipher.update(code, 'utf8', 'hex');
ciph += cipher.final('hex')
console.log('result', ciph)
}
//调用:encrypt('admin@123', 'des-cbc')
//结果:83dfb88b6496baecf429fce481f9b502
按理来说加密算法都是一样的,密钥和初始化向量iv也都是一样的,应该结果会是一样的啊。难道是底层加密函数算法不一样嘛?
c#加密你用的是不是utf8编码