zoukankan      html  css  js  c++  java
  • AES加密解密帮助类

    AES加密解密帮助类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Security.Cryptography;
     6 using System.IO;
     7 
     8 namespace Components.Helper.Security
     9 {
    10     /// <summary>
    11     /// ASE加解密
    12     /// </summary>
    13     public class AESHelper
    14     {
    15         /// <summary>
    16         /// 获取密钥
    17         /// </summary>
    18         private static string Key
    19         {
    20             get
    21             {
    22                 return "happynewyear2015";    ////必须是16位
    23             }
    24         }
    25         //默认密钥向量 
    26         private static byte[] _key1 = { 0x19, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    27         /// <summary>
    28         /// AES加密算法
    29         /// </summary>
    30         /// <param name="plainText">明文字符串</param>
    31         /// <returns>将加密后的密文转换为Base64编码,以便显示</returns>
    32         public static string AESEncrypt(string plainText)
    33         {
    34             //分组加密算法
    35             SymmetricAlgorithm des = Rijndael.Create();
    36             byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组 
    37             //设置密钥及密钥向量
    38             des.Key = Encoding.UTF8.GetBytes(Key);
    39             des.IV = _key1;
    40             byte[] cipherBytes = null;
    41             using (MemoryStream ms = new MemoryStream())
    42             {
    43                 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
    44                 {
    45                     cs.Write(inputByteArray, 0, inputByteArray.Length);
    46                     cs.FlushFinalBlock();
    47                     cipherBytes = ms.ToArray();//得到加密后的字节数组
    48                     cs.Close();
    49                     ms.Close();
    50                 }
    51             }
    52             return Convert.ToBase64String(cipherBytes);
    53         }
    54         /// <summary>
    55         /// AES解密
    56         /// </summary>
    57         /// <param name="cipherText">密文字符串</param>
    58         /// <returns>返回解密后的明文字符串</returns>
    59         public static string AESDecrypt(string showText)
    60         {
    61             var tt = showText.Length;
    62             showText = showText.Replace(' ', '+');
    63             byte[] cipherText = Convert.FromBase64String(showText);
    64             SymmetricAlgorithm des = Rijndael.Create();
    65             des.Key = Encoding.UTF8.GetBytes(Key);
    66             des.IV = _key1;
    67             byte[] decryptBytes = new byte[cipherText.Length];
    68             using (MemoryStream ms = new MemoryStream(cipherText))
    69             {
    70                 using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
    71                 {
    72                     cs.Read(decryptBytes, 0, decryptBytes.Length);
    73                     cs.Close();
    74                     ms.Close();
    75                 }
    76             }
    77             return Encoding.UTF8.GetString(decryptBytes).Replace("", "");   ///将字符串后尾的''去掉
    78         }
    79     }
    80 }
    AESHelper
  • 相关阅读:
    Eclipse集成weblogic教程
    Weblogic项目部署教程
    Eclipse导出WAR包
    Eclipse集成Tomcat教程
    Eclipse错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    Eclipse导入Oracle/MySQL数库驱动包教程
    Java连接Oracle/MySQL数据库教程
    Oracle常用表和常见操作命令
    VMware如何进入安全模式
    VMware进入BIOS
  • 原文地址:https://www.cnblogs.com/HuberyHu/p/5390896.html
Copyright © 2011-2022 走看看