步步为营-30-AES加密与解密
Posted 逍遥小天狼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营-30-AES加密与解密相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Security.Cryptography; namespace AES { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnEncrypt_Click(object sender, EventArgs e) { string toEncrypt = txtString.Text.Trim().ToString(); string key = txtKey.Text.Trim().ToString(); string iv = txtIv.Text.Trim().ToString(); lblResult.Text = Encrypt(toEncrypt, key, iv); } private void btnDecrypt_Click(object sender, EventArgs e) { string toEncrypt = txtString.Text.Trim().ToString(); string key = txtKey.Text.Trim().ToString(); string iv = txtIv.Text.Trim().ToString(); lblResult.Text = Decrypt(toEncrypt, key, iv); } public static string Encrypt(string toEncrypt, string key, string iv) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.Zeros; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } public static string Decrypt(string toDecrypt, string key, string iv) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv); byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.IV = ivArray; rDel.Mode = CipherMode.CBC; rDel.Padding = PaddingMode.Zeros; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } } }
以上是关于步步为营-30-AES加密与解密的主要内容,如果未能解决你的问题,请参考以下文章