Skip to content

Instantly share code, notes, and snippets.

@Johannestegner
Last active January 23, 2023 09:57
Show Gist options
  • Save Johannestegner/ca979716dd493b6d171ef6741e69bb88 to your computer and use it in GitHub Desktop.
Save Johannestegner/ca979716dd493b6d171ef6741e69bb88 to your computer and use it in GitHub Desktop.
Crypto JS vs C# with AES256CBC
using System.Security.Cryptography;
using System.Text;
Console.WriteLine(Crypto.Encrypt("ABC123"));
Console.WriteLine(Crypto.Decrypt("23YByDj8nb3FCZAyiV1DMifqCTCGII51EM9BfKU3qIc="));
public static class Crypto
{
private const string AesKey = "QmYHLtarGbVtmjDyesF7BI0GcJ6J+eMOu3pZOsJMSq0=";
public static string Encrypt(string payload)
{
// Generate IV.
var iv = RandomNumberGenerator.GetBytes(16);
// Create AES cipher.
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
// Key is a random 32 bytes long string, base64 encoded.
// Can be done with: openssl rand -base64 32
// The key need to be 32 bytes or the JS code will be angry.
aes.Key = Convert.FromBase64String(AesKey);
// Encrypt with cbc and pskc7 padding (default).
var message = aes.EncryptCbc(Encoding.UTF8.GetBytes(payload), iv, PaddingMode.PKCS7);
// Convert the IV and message into a base64 string.
return Convert.ToBase64String(iv.Concat(message).ToArray());
}
public static string Decrypt(string payload)
{
// Convert payload to binary data.
var payloadAsByteArray = Convert.FromBase64String(payload);
// Iv is first 16 bytes.
var iv = payloadAsByteArray.Take(16).ToArray();
// Data/message is all after iv.
var data = payloadAsByteArray.Skip(16).ToArray();
// Create AES cipher.
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
aes.IV = iv;
// Key is a random 32 bytes long string, base64 encoded.
// Can be done with: openssl rand -base64 32
// The key need to be 32 bytes or the JS code will be angry.
aes.Key = Convert.FromBase64String(AesKey);
// Decrypt with cbc and pskc7 padding (default).
var result = aes.DecryptCbc(data, iv, PaddingMode.PKCS7);
// Convert binary value to string.
return Encoding.UTF8.GetString(result);
}
}
const crypto = require('crypto');
const algo = 'AES-256-CBC';
const key = 'QmYHLtarGbVtmjDyesF7BI0GcJ6J+eMOu3pZOsJMSq0=';
const decrypt = (data) => {
// Payload as base64 decoded binary buffer.
const dataAsByteArray = Buffer.from(data, 'base64');
// Initialization Vector - First 16 bytes of the payload.
const iv = dataAsByteArray.subarray(0, 16);
// Message - All data after IV.
const msg = dataAsByteArray.subarray(16).toString('base64');
// Convert key from base64 to binary and then to hex.
const keyAsByteArray = Buffer.from(key, 'base64');
// Create decipher (using key and iv).
const decipher = crypto.createDecipheriv(algo, keyAsByteArray, iv);
// Update decipher with message (input type binary, output utf8).
let dec = decipher.update(msg, 'base64', 'hex');
return dec + decipher.final().toString('utf8');
};
const encrypt = (data) => {
// Generate 16 byte IV.
const iv = crypto.randomBytes(16);
// Convert key to binary from base64.
const keyAsByteArray = Buffer.from(key, 'base64');
// Create cipher.
const cipher = crypto.createCipheriv(algo, keyAsByteArray, iv);
// Encrypt.
const enc = cipher.update(data, 'utf8');
// Concat buffers and convert to base64.
return Buffer.concat([iv, enc, cipher.final()]).toString('base64');
};
console.log(decrypt('IpxbGjhIgyAwuEmoWRVy5s6gSYc1Njjubq3ze7IzYxE='));
console.log(encrypt('123ABC'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment