当前位置:编程学习 > JAVA >>

JAVA AES加密 对应的 C# 方法

由于最近在项目中用到,之前在网上找了好多,来来易做图,终于整出来了。 贴出来以后用起来方便

 

 

C#


[csharp] view plaincopyprint?
#region AES加解密  
       /// <summary>  
       ///AES加密(加密步骤)  
       ///1,加密字符串得到2进制数组;  
       ///2,将2禁止数组转为16进制;  
       ///3,进行base64编码  
       /// </summary>  
       /// <param name="toEncrypt">要加密的字符串</param>  
       /// <param name="key">密钥</param>  
       public String Encrypt(String toEncrypt, String key)  
       { 
           Byte[] _Key = Encoding.ASCII.GetBytes(key); 
           Byte[] _Source = Encoding.UTF8.GetBytes(toEncrypt); 
 
           Aes aes = Aes.Create("AES"); 
           aes.Mode = CipherMode.ECB; 
           aes.Padding = PaddingMode.PKCS7; 
           aes.Key = _Key; 
           ICryptoTransform cTransform = aes.CreateEncryptor(); 
           Byte[] cryptData = cTransform.TransformFinalBlock(_Source, 0, _Source.Length); 
           String HexCryptString = Hex_2To16(cryptData); 
           Byte[] HexCryptData = Encoding.UTF8.GetBytes(HexCryptString); 
           String CryptString =Convert.ToBase64String(HexCryptData); 
           return CryptString; 
       } 
 
       /// <summary>  
       /// AES解密(解密步骤)  
       /// 1,将BASE64字符串转为16进制数组  
       /// 2,将16进制数组转为字符串  
       /// 3,将字符串转为2进制数据  
       /// 4,用AES解密数据  
       /// </summary>  
       /// <param name="encryptedSource">已加密的内容</param>  
       /// <param name="key">密钥</param>  
       public String Decrypt(string encryptedSource, string key) 
       { 
           Byte[] _Key = Encoding.ASCII.GetBytes(key); 
           Aes aes = Aes.Create("AES"); 
           aes.Mode = CipherMode.ECB; 
           aes.Padding = PaddingMode.PKCS7; 
           aes.Key = _Key; 
           ICryptoTransform cTransform = aes.CreateDecryptor(); 
 
           Byte[] encryptedData = Convert.FromBase64String(encryptedSource); 
           String encryptedString = Encoding.UTF8.GetString(encryptedData); 
           Byte[] _Source = Hex_16To2(encryptedString); 
           Byte[] originalSrouceData = cTransform.TransformFinalBlock(_Source, 0, _Source.Length); 
           String originalString = Encoding.UTF8.GetString(originalSrouceData); 
           return originalString; 
       } 
 
       /// <summary>  
       /// 2进制转16进制  
       /// </summary>  
       String Hex_2To16(Byte[] bytes) 
       { 
           String hexString = String.Empty; 
           Int32 iLength = 65535; 
           if (bytes != null) 
           { 
               StringBuilder strB = new StringBuilder(); 
 
               if (bytes.Length < iLength) 
               { 
                   iLength = bytes.Length; 
               } 
 
               for (int i = 0; i < iLength; i++) 
               { 
                   strB.Append(bytes[i].ToString("X2")); 
               } 
               hexString = strB.ToString(); 
           } 
           return hexString; 
       } 
 
       /// <summary>  
       /// 16进制转2进制  
       /// </summary>  
       Byte[] Hex_16To2(String hexString) 
       { 
           if ((hexString.Length % 2) != 0) 
           { 
               hexString += " "; 
         

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,