發表文章

目前顯示的是有「AES加解密」標籤的文章

C# AES encrypt/decrypt and Base64 encode/decode

圖片
範例檔案: 這裡下載 純執行檔: 這裡下載 有興趣研究 AES 加解密 與 Base64 編解碼 的可以研究一下 因為用到了AES 元件,所以只適用 .NET 3.5 以上 畫面如下: 為什麼 AES 和 BASE64 寫在一起? 因為 AES 加密後的內容是 ASCII 碼,很多是無法以明文顯示的,因此會再編碼成BASE64以便傳遞或攜帶。反過來說,接收到AES的密碼通常會以BASE64編成居多,所以必須解碼後再進行AES解密環原本文。 Base64編碼: 要引用  using System; //將文字內容(明文)轉成base64文字(編碼) tbB64_2.Text = Convert.ToBase64String(Encoding.Default.GetBytes(tbB64_1.Text)); Base64解碼: 要引用  using System; //將base64編碼文字轉回明文(解碼) tbB64_1.Text = Encoding.Default.GetString(Convert.FromBase64String(tbB64_2.Text)); AES加密: 要引用  using System.Security.Cryptography; static byte[] EncryptionByAES(string plainText, byte[] key, byte[] IV) {   byte[] encrypted;   //檢查參數   if (plainText == null || plainText.Length <= 0)      throw new ArgumentNullException("沒有要加密的文字");   if (key == null || key.Length <= 0)      throw new ArgumentNullException("沒有提供金鑰");   if (IV == null || IV.Length <= 0)       throw new ArgumentNullException("沒有提供IV");   //這裡使用Ae