AES加密範例

AES加密範例

AES加密 網路上範例很多,但我找到很多都是無法使用的範例

所以參考一些能用的文章,自己做一個 保證可用的AES範例,方便使用

AES.rar

 

以下是AESClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;   
namespace AES
{

    class AESClass
    {
        //參考來源http://www.cnblogs.com/kiwifruit/archive/2010/10/28/1863141.html
        private static readonly String strAesKey = "iwww.maoblog.comiwww.maoblog.com";//加密所需32位密匙

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="str">要加密字符串</param>
        /// <returns>返回加密後字符串</returns>
        public  String Encrypt_AES(String str)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
            Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="str">要解密字符串</param>
        /// <returns>返回解密後字符串</returns>
        public  String Decrypt_AES(String str)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
        }
    }

    }


 

以下是Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace AES
{
    public partial class Aes_Sample : Form
    {
        public Aes_Sample()
        {
            InitializeComponent();
        }
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            AESClass StrAES = new AESClass();
            textBox2.Text = StrAES.Encrypt_AES(textBox1.Text);
        }
        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            AESClass StrAES = new AESClass();
            textBox1.Text = StrAES.Decrypt_AES(textBox2.Text);
        }
    }
}

 


如有錯誤 歡迎指正