Last active
September 10, 2024 23:16
-
-
Save mhingston/a47caa21298950abc4d8422d98b7437e to your computer and use it in GitHub Desktop.
AES-256-CBC for C# and Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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