Skip to content

Instantly share code, notes, and snippets.

@ledunguit
Forked from mhingston/AES.cs
Created August 21, 2021 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ledunguit/c045c97124d2acf0437f99183847bd2d to your computer and use it in GitHub Desktop.
Save ledunguit/c045c97124d2acf0437f99183847bd2d to your computer and use it in GitHub Desktop.
AES-256-CBC for C# and Node
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class AES
{
public static string Encrypt(string plainText, string keyString)
{
byte[] cipherData;
Aes aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes(keyString);
aes.GenerateIV();
aes.Mode = CipherMode.CBC;
ICryptoTransform cipher = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, cipher, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(plainText);
}
}
cipherData = ms.ToArray();
}
byte[] combinedData = new byte[aes.IV.Length + cipherData.Length];
Array.Copy(aes.IV, 0, combinedData, 0, aes.IV.Length);
Array.Copy(cipherData, 0, combinedData, aes.IV.Length, cipherData.Length);
return Convert.ToBase64String(combinedData);
}
public static string Decrypt(string combinedString, string keyString)
{
string plainText;
byte[] combinedData = Convert.FromBase64String(combinedString);
Aes aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes(keyString);
byte[] iv = new byte[aes.BlockSize / 8];
byte[] cipherText = new byte[combinedData.Length - iv.Length];
Array.Copy(combinedData, iv, iv.Length);
Array.Copy(combinedData, iv.Length, cipherText, 0, cipherText.Length);
aes.IV = iv;
aes.Mode = CipherMode.CBC;
ICryptoTransform decipher = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, decipher, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
plainText = sr.ReadToEnd();
}
}
return plainText;
}
}
}
const crypto = require("crypto");
const IV_SIZE = 16;
class AES
{
static Encrypt(plainText, keyString)
{
const iv = crypto.randomBytes(IV_SIZE);
const cipher = crypto.createCipheriv("aes-256-cbc", keyString, iv);
let cipherText = cipher.update(Buffer.from(plainText, "utf8"));
cipherText = Buffer.concat([cipherText, cipher.final()]);
const combinedData = Buffer.concat([iv, cipherText]);
const combinedString = combinedData.toString("base64");
return combinedString;
}
static Decrypt(combinedString, keyString)
{
const combinedData = Buffer.from(combinedString, "base64");
const iv = Buffer.alloc(IV_SIZE);
const cipherText = Buffer.alloc(combinedData.length - iv.length);
combinedData.copy(iv, 0, 0, iv.length);
combinedData.copy(cipherText, 0, iv.length);
const decipher = crypto.createDecipheriv("aes-256-cbc", keyString, iv);
let plainText = decipher.update(cipherText, "utf8");
plainText += decipher.final("utf8");
return plainText;
}
}
module.exports = AES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment