I am facing a scenario where I need to replicate an encryption method that is currently implemented in C#. The idea is to use the same C# project to decrypt the encrypted key from wherever it is logged.
Below is the logic utilized in C#:
using var aes = new AesCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
aes.GenerateIV();
using var encrypter = aes.CreateEncryptor(aes.Key, aes.IV);
using var cipherStream = new MemoryStream();
using (var tCryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var tBinaryWriter = new BinaryWriter(tCryptoStream))
{
cipherStream.Write(aes.IV);
tBinaryWriter.Write(Encoding.UTF8.GetBytes(encryptMe));
tCryptoStream.FlushFinalBlock();
}
return Convert.ToBase64String(cipherStream.ToArray());
The key used is the same in both the C# and JavaScript implementations. However, I am still unable to generate the same encryption value as in C#.
I have attempted to find solutions on other Stack Overflow posts regarding this issue, but I have not been able to identify the missing element in my JavaScript implementation. Can someone please assist me?