如何从DecryptStringFromBytes_Aes中获得与.NETC#中相同但在NodeJS中相同的结果?
我需要在Node js中获得与在.NET C中从我的网站获得的相同的解密值# 我在.NET C#类中称为Crypto的代码是:如何从DecryptStringFromBytes_Aes中获得与.NETC#中相同但在NodeJS中相同的结果?,c#,node.js,.net,encryption,node-crypto,C#,Node.js,.net,Encryption,Node Crypto,我需要在Node js中获得与在.NET C中从我的网站获得的相同的解密值# 我在.NET C#类中称为Crypto的代码是: private static readonly string key = "Z8,omB0pxZwñ3h9s"; public string Encrypt(string data) { string encData = null; byte[][] keys = GetHashKeys(key);
private static readonly string key = "Z8,omB0pxZwñ3h9s";
public string Encrypt(string data)
{
string encData = null;
byte[][] keys = GetHashKeys(key);
try
{
encData = EncryptStringToBytes_Aes(data, keys[0], keys[1]);
return encData;
}
catch (CryptographicException) {
return null;
}
catch (ArgumentNullException) {
return null;
}
}
public string Decrypt(string data)
{
string decData = null;
byte[][] keys = GetHashKeys(key);
try
{
decData = DecryptStringFromBytes_Aes(data, keys[0], keys[1]);
}
catch (CryptographicException) { }
catch (ArgumentNullException) { }
return decData;
}
private byte[][] GetHashKeys(string key)
{
byte[][] result = new byte[2][];
Encoding enc = Encoding.UTF8;
SHA256 sha2 = new SHA256CryptoServiceProvider();
byte[] rawKey = enc.GetBytes(key);
byte[] rawIV = enc.GetBytes(key);
byte[] hashKey = sha2.ComputeHash(rawKey);
byte[] hashIV = sha2.ComputeHash(rawIV);
Array.Resize(ref hashIV, 16);
result[0] = hashKey;
result[1] = hashIV;
return result;
}
//source: https://msdn.microsoft.com/de-de/library/system.security.cryptography.aes(v=vs.110).aspx
private static string EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt =
new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encrypted);
}
//source: https://msdn.microsoft.com/de-de/library/system.security.cryptography.aes(v=vs.110).aspx
private static string DecryptStringFromBytes_Aes(string cipherTextString, byte[] Key, byte[] IV)
{
byte[] cipherText = Convert.FromBase64String(cipherTextString);
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
string plaintext = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
如果我在.NET中加密,变量ZZZZZZ会给出我需要的结果。
但是在Node js中,虽然结果可能有点类似,但它与我在.NET中得到的结果不同,我确实需要对加密和解密获得完全相同的结果
求你了,我需要帮助。我根本不知道什么是加密问题不仅仅是因为IV不同。我将在下文提供更多细节: 在.NETC#中,hashKey是一个32字节的数组,它来自一个16字节的文本字符串。但是hashIV是一个数组,相当于hashKey数组的前半部分 因此,在Node.js中,使用的算法是“aes-256-cbc”,将键串和IV变量分配给每个变量对应的字节的Buffer.from,这必须与C#中的相同 接下来,我离开Node.js解决方案,它给出了相同的结果
const crypto = require('crypto');
algorithm = "aes-256-cbc",
keystring = Buffer.from([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]),
iv = Buffer.from([ 1, 2, 3, 4, 5, ,6 ,7 ,8 9, 10, 11, 12, 13, 14, 15, 16]);
inputEncoding = 'utf8',
outputEncoding = 'base64';
function Encrypt(text) {
let cipher = crypto.createCipheriv(algorithm,keystring, iv);
let encrypted = cipher.update(text, inputEncoding, outputEncoding)
encrypted += cipher.final(outputEncoding);
return encrypted;
}
function Decrypt(encrypted) {
let decipher = crypto.createDecipheriv(algorithm,keystring, iv)
let dec = decipher.update(encrypted, outputEncoding, inputEncoding)
dec += decipher.final(inputEncoding);
return dec;
}
var enc = Encrypt("ZZZZZZ");
console.log("enc: " + enc);
var dec = Decrypt(enc);
console.log("dec: " + dec);
答案中指定的字节实际上不是C#中捕获的字节,但我将它们作为示例
如果对某人有用,那就太好了。手机上很难找到密码。你有没有检查过钥匙和IV的两端是否相同?一般来说,人们都会忘记IVD,事实上@Flydog57是在C#中调试时,IV是一个字节数组,通过获取密钥的字节来获得。但在NodeJS中,我无法执行相同的过程,因此IV是相同的。我很迷茫,我不知道该怎么办。加密时创建一个IV,然后以明文形式发送给任何要解密的人。它们不需要保护——但两端都需要相同。您确实需要两端都有钥匙,但需要加以保护。
const crypto = require('crypto');
algorithm = "aes-256-cbc",
keystring = Buffer.from([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]),
iv = Buffer.from([ 1, 2, 3, 4, 5, ,6 ,7 ,8 9, 10, 11, 12, 13, 14, 15, 16]);
inputEncoding = 'utf8',
outputEncoding = 'base64';
function Encrypt(text) {
let cipher = crypto.createCipheriv(algorithm,keystring, iv);
let encrypted = cipher.update(text, inputEncoding, outputEncoding)
encrypted += cipher.final(outputEncoding);
return encrypted;
}
function Decrypt(encrypted) {
let decipher = crypto.createDecipheriv(algorithm,keystring, iv)
let dec = decipher.update(encrypted, outputEncoding, inputEncoding)
dec += decipher.final(inputEncoding);
return dec;
}
var enc = Encrypt("ZZZZZZ");
console.log("enc: " + enc);
var dec = Decrypt(enc);
console.log("dec: " + dec);