public static void Main()
String timestamp = "ARCA" + ((long)DateTimeOffset.Now.ToUnixTimeSeconds()).ToString();
var encrypted = AES.EncryptStringToString(timestamp, secret);
Console.WriteLine("Stringa originale: {0}", timestamp);
Console.WriteLine("Stringa criptata : {0}",encrypted);
var decrypted = AES.DecryptStringToString(encrypted, secret);
Console.WriteLine("Stringa decriptata : {0}",decrypted);
internal static class AES
private static readonly System.Text.Encoding encoding = new System.Text.UTF8Encoding();
internal static String EncryptStringToString(String msg, String pwd) { return Convert.ToBase64String(EncryptBytesToBytes(encoding.GetBytes(msg), encoding.GetBytes(pwd))); }
internal static String EncryptStringToString(String msg, Byte[] pwd) { return Convert.ToBase64String(EncryptBytesToBytes(encoding.GetBytes(msg), pwd)); }
internal static Byte[] EncryptStringToBytes(String msg, String pwd) { return EncryptBytesToBytes(encoding.GetBytes(msg), encoding.GetBytes(pwd)); }
internal static Byte[] EncryptStringToBytes(String msg, Byte[] pwd) { return EncryptBytesToBytes(encoding.GetBytes(msg), pwd); }
internal static String EncryptBytesToString(Byte[] msg, String pwd) { return Convert.ToBase64String(EncryptBytesToBytes(msg, encoding.GetBytes(pwd))); }
internal static String EncryptBytesToString(Byte[] msg, Byte[] pwd) { return Convert.ToBase64String(EncryptBytesToBytes(msg, pwd)); }
internal static Byte[] EncryptBytesToBytes(Byte[] msg, String pwd) { return EncryptBytesToBytes(msg, encoding.GetBytes(pwd)); }
internal static Byte[] EncryptBytesToBytes(Byte[] msg, Byte[] pwd)
using (var hashProvider = new System.Security.Cryptography.SHA256CryptoServiceProvider())
using (var aesAlgorithm = new System.Security.Cryptography.AesCryptoServiceProvider())
aesAlgorithm.Mode = System.Security.Cryptography.CipherMode.ECB;
aesAlgorithm.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aesAlgorithm.Key = hashProvider.ComputeHash(pwd);
using (var encryptor = aesAlgorithm.CreateEncryptor())
return encryptor.TransformFinalBlock(msg, 0, msg.Length);
internal static String DecryptStringToString(String msg, String pwd) { return encoding.GetString(DecryptBytesToBytes(Convert.FromBase64String(msg), encoding.GetBytes(pwd))); }
internal static String DecryptStringToString(String msg, Byte[] pwd) { return encoding.GetString(DecryptBytesToBytes(Convert.FromBase64String(msg), pwd)); }
internal static Byte[] DecryptStringToBytes(String msg, String pwd) { return DecryptBytesToBytes(Convert.FromBase64String(msg), encoding.GetBytes(pwd)); }
internal static Byte[] DecryptStringToBytes(String msg, Byte[] pwd) { return DecryptBytesToBytes(Convert.FromBase64String(msg), pwd); }
internal static String DecryptBytesToString(Byte[] msg, String pwd) { return encoding.GetString(DecryptBytesToBytes(msg, encoding.GetBytes(pwd))); }
internal static String DecryptBytesToString(Byte[] msg, Byte[] pwd) { return encoding.GetString(DecryptBytesToBytes(msg, pwd)); }
internal static Byte[] DecryptBytesToBytes(Byte[] msg, String pwd) { return DecryptBytesToBytes(msg, encoding.GetBytes(pwd)); }
internal static Byte[] DecryptBytesToBytes(Byte[] msg, Byte[] pwd)
using (var hashProvider = new System.Security.Cryptography.SHA256CryptoServiceProvider())
using (var aesAlgorithm = new System.Security.Cryptography.AesCryptoServiceProvider())
aesAlgorithm.Mode = System.Security.Cryptography.CipherMode.ECB;
aesAlgorithm.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
aesAlgorithm.Key = hashProvider.ComputeHash(pwd);
using (var decryptor = aesAlgorithm.CreateDecryptor())
return decryptor.TransformFinalBlock(msg, 0, msg.Length);