Ich habe eine Node.js -Anwendung mit Datenbankzugriff. Der ConnectionString wird von einer JSON-Datei versendet und der Kennwortteil innerhalb des ConnectionString enthält Salz, Nonce und die Chiffre, die von AES-GCM verschlüsselt wird. Das Geheimnis für Verschlüsseln + Entschlüsseln Das Kennwort verwendet eine Schlüsselableitung von Scrypt in der internen Krypto -Bibliothek von NodeJS. Soweit funktioniert so eine gute Verschlüsselung und Entschlüsselung gut. < /P>
jedoch. Für einen weiteren Usecase muss ich diesen ConnectionString jetzt von einer C# -Anwendung verwenden und kann die Kennwortzeichenfolge nicht entschlüsseln. Die Methode schlägt mit dem folgenden Fehler aus:
"Das Computer -Authentifizierungs -Tag hat nicht mit dem Eingangsauthentifizierungs -Tag übereinstimmt. Ich konnte keine .NET -Implementierung finden, die mit der Schlüsselableitung von NodeJs übereinstimmt. decrypt(encryptedText) {
const decoded = Buffer.from(encryptedText, "base64").toString("utf8");
const [saltHex, ivHex, authTagHex, encryptedHex] = decoded.split(":");
const salt = Buffer.from(saltHex, "hex");
const secretByte = Buffer.from(this.secretKey, "utf-8");
const key = crypto.scryptSync(salt, secretByte, 32, { cost: 16384, blockSize: 8, parallelization: 1 });
const iv = Buffer.from(ivHex, "hex");
const authTag = Buffer.from(authTagHex, "hex");
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encryptedHex, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
< /code>
public static String DecryptAESGCM(Dictionary encrypted, string secret)
{
byte[] secretByte = Encoding.UTF8.GetBytes(secret);
//clsLogger.Log(Encoding.UTF8.GetString(encrypted.GetValueOrDefault("salt")), enumLogLevel.INFO);
//Derive key
byte[] derivedKey = SCrypt.ComputeDerivedKey(secretByte, encrypted.GetValueOrDefault("salt"), 16384, 8, 1, 1, 32);
using (AesGcm aes = new AesGcm(derivedKey, encrypted.GetValueOrDefault("tag").Length))
{
byte[] plainText = new byte[encrypted.GetValueOrDefault("cipher").Length];
aes.Decrypt(encrypted.GetValueOrDefault("nonce"), encrypted.GetValueOrDefault("cipher"), encrypted.GetValueOrDefault("tag"), plainText);
return Encoding.UTF8.GetString(plainText);
}
}
< /code>
The Dictionary in c# is a key value store with the byte[] for salt, nonce, tag and cipher.
I already tried several different librarys to reproduce the derived key and I ensured, that salt and secret do have the same values in nodejs and c# before the function is called, but the derived keys are always different.
Does anyone had a similar case and solved it or do have any ideas how?
Thanks in advance!
Robin
Übereinstimmende Schlüsselableitung mit Scrypt in node.js und c# ⇐ C#
-
- Similar Topics
- Replies
- Views
- Last post