} // End Constructor public string GenerateKey() { string retValue = null; using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create()) { aes.GenerateKey(); aes.GenerateIV(); byte[] bIV = aes.IV; byte[] bKey = aes.Key; aes.Clear(); retValue = "IV: " + ByteArrayToHexString(bIV) + System.Environment.NewLine + "Key: " + ByteArrayToHexString(bKey); System.Array.Clear(bIV, 0, bIV.Length); System.Array.Clear(bKey, 0, bKey.Length); bIV = null; bKey = null; } // End Using aes return(retValue); } // End Function GenerateKey
static void KeyGenConsole() { Console.WriteLine("Which?"); // Console.WriteLine("1. RSA"); Console.WriteLine("2. AES"); Console.Write("> "); string input = Console.ReadLine(); switch (input) { case "1": // TODO break; case "2": System.Security.Cryptography.Aes aesAlg = System.Security.Cryptography.Aes.Create(); aesAlg.GenerateKey(); byte[] key = aesAlg.Key; Util.SaveFile.WriteFile("./AES.-2.key", key); aesAlg.GenerateKey(); Util.SaveFile.WriteFile("./AES.-1.key", aesAlg.Key); break; } }
} // End Sub SetIV public static string GenerateKey() { byte[] bIV; byte[] bKey; using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create()) { aes.GenerateKey(); aes.GenerateIV(); bIV = aes.IV; bKey = aes.Key; // aes.Clear(); } // End Using aes return("IV: " + ByteArrayToHexString(bIV) + System.Environment.NewLine + "Key: " + ByteArrayToHexString(bKey)); } // End Function GenerateKey