• c# AES加解密并转ASCII码


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AESDecryptDemo
    {
       public class AES
        {
           public static string AESEncryptASCII(string plainStr)
           {
               StringBuilder sb = new StringBuilder();
               byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
               byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
               byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
    
               string encrypt = null;
               Rijndael aes = Rijndael.Create();
               using (MemoryStream mStream = new MemoryStream())
               {
                   using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                   {
                       cStream.Write(byteArray, 0, byteArray.Length);
                       cStream.FlushFinalBlock();
                       encrypt = Encode(Convert.ToBase64String(mStream.ToArray()));
                   }
               }
               aes.Clear();
               return encrypt;
           }
           public static string AESEncrypt(string plainStr)
           {
               StringBuilder sb = new StringBuilder();
               byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
               byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
               byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
    
               string encrypt = null;
               Rijndael aes = Rijndael.Create();
               using (MemoryStream mStream = new MemoryStream())
               {
                   using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                   {
                       cStream.Write(byteArray, 0, byteArray.Length);
                       cStream.FlushFinalBlock();
                       encrypt = Convert.ToBase64String(mStream.ToArray());
                   }
               }
               aes.Clear();
               return encrypt;
           }
           public static string AESDecryptASCII(string encryptStr)
           {
               encryptStr = Decode(encryptStr);
               byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
               byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
               byte[] byteArray = Convert.FromBase64String(encryptStr);
    
               string decrypt = null;
               Rijndael aes = Rijndael.Create();
               using (MemoryStream mStream = new MemoryStream())
               {
                   using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                   {
                       cStream.Write(byteArray, 0, byteArray.Length);
                       cStream.FlushFinalBlock();
                       decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                   }
               }
               aes.Clear();
               return decrypt;
           }
           public static string AESDecrypt(string encryptStr)
           {
               byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
               byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
               byte[] byteArray = Convert.FromBase64String(encryptStr);
    
               string decrypt = null;
               Rijndael aes = Rijndael.Create();
               using (MemoryStream mStream = new MemoryStream())
               {
                   using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                   {
                       cStream.Write(byteArray, 0, byteArray.Length);
                       cStream.FlushFinalBlock();
                       decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                   }
               }
               aes.Clear();
               return decrypt;
           }
           public static string Encode(string strEncode)
           {
               string strReturn = "";//  存储转换后的编码
               foreach (short shortx in strEncode.ToCharArray())
               {
                   strReturn += shortx.ToString("X");
               }
               return strReturn;
           }
    
           public static string Decode(string strDecode)
           {
               string sResult = "";
               for (int i = 0; i < strDecode.Length / 2; i++)
               {
                   sResult += (char)short.Parse(strDecode.Substring(i * 2, 2), global::System.Globalization.NumberStyles.HexNumber);
               }
               return sResult;
           }
           private static string GetKey()
           {
               return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M";
           }
    
           /// <summary>
           /// 获取向量
           /// </summary>
           private static string GetIV()
           {
               return @"L+~f4,Ir)b$=pkf";
           }
        }
    }
    

      

  • 相关阅读:
    vmware 连网问题
    js控制表单非法输入时提交
    简单的jsp&servlet 购物车项目
    html引入css不显示问题
    每天学点java_反射作用
    java选择特定的值2--抽象enum
    java选择特定的值
    1 小时 SQL 极速入门(一)
    460004600146002MNCMCCIMSI
    网络中存在2台DHCP服务器问题
  • 原文地址:https://www.cnblogs.com/sunyj/p/5163879.html
Copyright © 2020-2023  润新知