-
-
Save anonymous/19d9e5f6747dfe75d553 to your computer and use it in GitHub Desktop.
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
public static class Crypto | |
{ | |
public static byte[] GenerateVector(byte[] key) | |
{ | |
using (var aes = Aes.Create()) | |
{ | |
aes.Key = key; | |
aes.GenerateIV(); | |
return aes.IV; | |
} | |
} | |
public static byte[] GenerateKey() | |
{ | |
using (var aes = Aes.Create()) | |
{ | |
return aes.Key;; | |
} | |
} | |
public static string EncryptBase64(string data, byte[] key, byte[] vector) | |
{ | |
byte[] encrypted; | |
using (var aes = Aes.Create()) | |
{ | |
aes.Key = key; | |
aes.IV = vector; | |
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); | |
using (var stream = new MemoryStream()) | |
{ | |
using (var cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write)) | |
{ | |
using (var streamWriter = new StreamWriter(cryptoStream)) | |
{ | |
streamWriter.Write(data); | |
} | |
encrypted = stream.ToArray(); | |
} | |
} | |
} | |
return Convert.ToBase64String(encrypted); | |
} | |
public static string Decrypt(string data, byte[] key, byte[] vector) | |
{ | |
string result; | |
using (var aes = Aes.Create()) | |
{ | |
aes.Key = key; | |
aes.IV = vector; | |
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV); | |
using (var stream = new MemoryStream(Convert.FromBase64String(data))) | |
{ | |
using (var cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read)) | |
{ | |
using (var streamReader = new StreamReader(cryptoStream)) | |
{ | |
result = streamReader.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment