private void btnAesEncrypt_Click(object sender, EventArgs e) { byte[] plaintext; if (EncodingUTF8Rb.Checked) { plaintext = Encoding.UTF8.GetBytes(tbAesPlainText.Text); } else { plaintext = Util.GetBytes(tbAesPlainText.Text); } byte[] key = Util.GetBytes(tbAesKey.Text); byte[] iv = Util.GetBytes(tbAesIV.Text); byte[] ciphertext = AesStatic.Encrypt(plaintext, key, iv); tbAesCipherText.Text = Util.ToHexString(ciphertext); tbAesPlainText.Text = ""; }
public void TestAes() { byte[] key = AesStatic.GenerateKey(); Assert.IsNotNull(key); byte[] iv = AesStatic.GenerateIV(); Assert.IsNotNull(iv); Random random = new Random(); byte[] plaintext = new byte[45674]; random.NextBytes(plaintext); byte[] ciphertext = AesStatic.Encrypt(plaintext, key, iv); Assert.IsNotNull(ciphertext); Assert.AreEqual(ciphertext.Length, Util.GetTotalSize(plaintext.Length, 16)); byte[] decrypted = AesStatic.Decrypt(ciphertext, key, iv); Assert.IsNotNull(decrypted); CollectionAssert.AreEqual(plaintext, decrypted); }