Ich habe älteren Verschlüsselungscode in C# und jetzt muss ich die gleiche Verschlüsselung in JavaScript durchführen. Ich habe etwas recherchiert und 3 verschiedene Bibliotheken ausprobiert, konnte jedoch nicht die gleichen Ergebnisse erzielen. Letzte Bibliothek, die ich benutzt habe, ist CryptoJS und ich muss herausfinden, warum ich unterschiedliche Ergebnisse bekomme.C# & JS-Verschlüsselungsmethoden geben unterschiedliche Ergebnisse zurück
Hier sind beide Code-Schnipsel:
C# -Code:
text = "chocolate";
PasswordHash = "hashhash";
SaltKey = "saltsaltsaltsa";
VIKey = "iviviviviviviviv";
byte[] plainTextBytes = Encoding.UTF8.GetBytes(text);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256/8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
var result = Convert.ToBase64String(cipherTextBytes);
Ergebnis ==> "8AbNsyyqHHfi/PEF/bbiew =="
JavaScript-Code:
pass = 'chocolate';
//Creating the Vector Key
var iv = CryptoJS.enc.Utf8.parse('iviviviviviviviv');
//Encoding the Password in from UTF8 to byte array
var passHash = CryptoJS.enc.Utf8.parse('hashhash');
//Encoding the Salt in from UTF8 to byte array
var Salt = CryptoJS.enc.Utf8.parse("saltsaltsaltsa");
//Creating the key in PBKDF2 format to be used during the encryption
var key128Bits1000Iterations = CryptoJS.PBKDF2(passHash, Salt, { keySize: 256/8, iterations: 1000 });
//Encrypting the string contained in cipherParams using the PBKDF2 key
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(pass), key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.ZeroPadding });
var result = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
Ergebnis = "dpgTA41PD yrM8ef9C1c8iA == "
Sie scheinen mit 'Encoding.ASCII' in C# werden und 'CryptoJS.enc.Utf8' in der JS ..? – stuartd
@stuartd - dachte darüber nach, dasselbe Verhalten für sowohl ASCII als auch UTF8 im gegebenen Szenario – Niro