Last active
July 22, 2019 09:23
-
-
Save bartwttewaall/1bf5312612ff8afa460e2d3b8cc4d01e to your computer and use it in GitHub Desktop.
AES-256-CBC encryption / decryption with secret key and IV string with additional C# decrypt-only method
This file contains hidden or 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; | |
public class CypherUtils { | |
public static string Decrypt (string cipherData, string keyString, string ivString) { | |
byte[] key = Encoding.UTF8.GetBytes (keyString); | |
byte[] iv = Convert.FromBase64String (ivString); | |
try { | |
using (var rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC }) | |
using (var memoryStream = new MemoryStream (Convert.FromBase64String (cipherData))) | |
using (var cryptoStream = new CryptoStream (memoryStream, rijndaelManaged.CreateDecryptor (key, iv), CryptoStreamMode.Read)) { | |
return new StreamReader (cryptoStream).ReadToEnd (); | |
} | |
} catch (CryptographicException e) { | |
Console.WriteLine ("A Cryptographic error occurred: {0}", e.Message); | |
return null; | |
} | |
// You may want to catch more exceptions here... | |
} | |
} |
This file contains hidden or 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
import * as crypto from "crypto"; | |
export function encrypt(secret: string, utfText: string): string { | |
const key = Buffer.from(secret); | |
const iv = crypto.randomBytes(16); | |
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); | |
const encrypted = cipher.update(utfText, "utf8", "base64") + cipher.final("base64"); | |
return iv.toString("base64") + ":" + encrypted; | |
} | |
export function decrypt(secret: string, base64Input: string) { | |
const key = Buffer.from(secret); | |
const textParts = base64Input.split(":"); | |
const iv = Buffer.from(textParts.shift(), "base64"); | |
const encryptedText = Buffer.from(textParts.join(":"), "base64"); | |
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); | |
const decrypted = decipher.update(encryptedText); | |
return Buffer.concat([decrypted, decipher.final()]).toString(); | |
} | |
// ---- test ---- | |
export function testEncryptDecrypt() { | |
const key = crypto.randomBytes(32).toString(); | |
const data = { | |
serverUrl: "wss://my-game-server.com", | |
playerId: "my-playerId" | |
}; | |
const encrypted = this.encrypt(key, JSON.stringify(data)); | |
console.log("encrypted:", encrypted); | |
const decrypted = this.decrypt(key, encrypted); | |
console.log("decrypted:", decrypted); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment