• JAVA AES加密 对应 C# AES加密


    java代码:

     1 public class AESCoder {
     2     private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
     3     private static String KEY_ALGORITHM = "AES";
     4 
     5     public static String decrypt(String sSrc, String sKey) throws Exception {
     6         SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"), KEY_ALGORITHM);
     7         Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
     8         cipher.init(2, skeySpec);
     9         byte[] encrypted1 = hex2byte(sSrc);
    10         byte[] original = cipher.doFinal(encrypted1);
    11         return new String(original);
    12     }
    13 
    14     public static String encrypt(String sSrc, String sKey) throws Exception {
    15         SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"), KEY_ALGORITHM);
    16         Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    17         cipher.init(1, skeySpec);
    18         byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
    19         return byte2hex(encrypted);
    20     }
    21 
    22     private static byte[] hex2byte(String strhex) {
    23         if (strhex == null)
    24             return null;
    25 
    26         int l = strhex.length();
    27         if (l % 2 == 1)
    28             return null;
    29 
    30         byte[] b = new byte[l / 2];
    31         for (int i = 0; i != l / 2; ++i)
    32             b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2), 16);
    33 
    34         return b;
    35     }
    36 
    37     private static String byte2hex(byte[] b) {
    38         String hs = "";
    39         String stmp = "";
    40         for (int n = 0; n < b.length; ++n) {
    41             stmp = Integer.toHexString(b[n] & 0xFF);
    42             if (stmp.length() == 1)
    43                 hs = hs + "0" + stmp;
    44             else
    45                 hs = hs + stmp;
    46         }
    47 
    48         return hs.toUpperCase();
    49     }
    View Code

    c#代码:

      1 public class AESCoder
      2     {
      3         /// <summary>
      4         /// 获取Aes32位密钥
      5         /// </summary>
      6         /// <param name="key">Aes密钥字符串</param>
      7         /// <returns>Aes32位密钥</returns>
      8         static byte[] GetAesKey(string key)
      9         {
     10             if (string.IsNullOrEmpty(key))
     11             {
     12                 throw new ArgumentNullException("key", "Aes密钥不能为空");
     13             }
     14             if (key.Length < 32)
     15             {
     16                 // 不足32补全
     17                 key = key.PadRight(32, '0');
     18             }
     19             if (key.Length > 32)
     20             {
     21                 key = key.Substring(0, 32);
     22             }
     23             return Encoding.UTF8.GetBytes(key);
     24         }
     25 
     26         /// <summary>
     27         ///  16进制转2进制 Aes解密
     28         /// </summary>
     29         /// <param name="source">源字符串</param>
     30         /// <param name="key">aes密钥,长度必须32位</param>
     31         /// <returns>解密后的字符串</returns>
     32         public static string Decrypt(string source, string key)
     33         {
     34             using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
     35             {
     36                 aesProvider.Key = GetAesKey(key);
     37                 aesProvider.Mode = CipherMode.ECB;
     38                 aesProvider.Padding = PaddingMode.PKCS7;
     39                 using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
     40                 {
     41                     byte[] inputBuffers = AESCode.Hex_16To2(source);
     42                     byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
     43                     aesProvider.Clear();
     44                     return Encoding.UTF8.GetString(results);
     45                 }
     46             }
     47         }
     48 
     49         /// <summary>
     50         /// Aes加密 2进制转16进制
     51         /// </summary>
     52         /// <param name="source">源字符串</param>
     53         /// <param name="key">aes密钥,长度必须32位</param>
     54         /// <returns>加密后的字符串</returns>
     55         public static String Encrypt(string source, string key)
     56         {
     57             using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
     58             {
     59                 aesProvider.Key = GetAesKey(key);
     60                 aesProvider.Mode = CipherMode.ECB;
     61                 aesProvider.Padding = PaddingMode.PKCS7;
     62                 using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor())
     63                 {
     64                     byte[] inputBuffers = Encoding.UTF8.GetBytes(source);
     65                     byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
     66                     aesProvider.Clear();
     67                     aesProvider.Dispose();
     68                     return AESCode.Hex_2To16(results);
     69                 }
     70             }
     71         }
     72 
     73         /// <summary>
     74         /// 2进制转16进制
     75         /// </summary>
     76         public static String Hex_2To16(Byte[] bytes)
     77         {
     78             String hexString = String.Empty;
     79             Int32 iLength = 65535;
     80             if (bytes != null)
     81             {
     82                 StringBuilder strB = new StringBuilder();
     83                 if (bytes.Length < iLength)
     84                 {
     85                     iLength = bytes.Length;
     86                 }
     87                 for (int i = 0; i < iLength; i++)
     88                 {
     89                     strB.Append(bytes[i].ToString("X2"));
     90                 }
     91                 hexString = strB.ToString();
     92             }
     93             return hexString;
     94         }
     95 
     96         /// <summary>
     97         /// 16进制转2进制
     98         /// </summary>
     99         public static Byte[] Hex_16To2(String hexString)
    100         {
    101             if ((hexString.Length % 2) != 0)
    102             {
    103                 hexString += " ";
    104             }
    105             Byte[] returnBytes = new Byte[hexString.Length / 2];
    106             for (Int32 i = 0; i < returnBytes.Length; i++)
    107             {
    108                 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    109             }
    110             return returnBytes;
    111         }
    View Code
  • 相关阅读:
    Compiling LIBFFM On OSX 10.9
    Linux shell 脚本入门教程+实例
    Understanding the Bias-Variance Tradeoff
    Learning How To Code Neural Networks
    MXNet设计和实现简介
    数据需求统计常用awk命令
    Deal with relational data using libFM with blocks
    MATLAB 在同一个m文件中写多个独立的功能函数
    Debug 路漫漫-06
    MATLAB 求两个矩阵的 欧氏距离
  • 原文地址:https://www.cnblogs.com/wuweimin/p/7839505.html
Copyright © 2020-2023  润新知