码迷,mamicode.com
首页 > 编程语言 > 详细

java、C#配套版AES加密

时间:2016-05-10 12:43:29      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

java版加密
  1. public static String aesEncrypt(String str, String key) throws Exception {
  2. if (str == null || key == null) return null;
  3. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  4. cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Md5Utils.ShortMd5(key).getBytes("UTF-8"), "AES"));
  5. byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
  6. return new BASE64Encoder().encode(bytes);
  7. }

java版解密
  1. public static String aesDecrypt(String str, String key) throws Exception {
  2. if (str == null || key == null) return null;
  3. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  4. cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Md5Utils.ShortMd5(key).getBytes("UTF-8"), "AES"));
  5. byte[] bytes = new BASE64Decoder().decodeBuffer(str);
  6. bytes = cipher.doFinal(bytes);
  7. return new String(bytes, "utf-8");
  8. }

C#版加密
  1. public static string Encrypt(string toEncrypt,string key)
  2. {
  3. // 256-AES key
  4. //byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  5. byte[] keyArray = Encoding.UTF8.GetBytes(Get16ByteMd5(key));
  6. byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
  7. RijndaelManaged rDel = new RijndaelManaged();
  8. rDel.Key = keyArray;
  9. rDel.Mode = CipherMode.ECB;
  10. rDel.Padding = PaddingMode.PKCS7;
  11. ICryptoTransform cTransform = rDel.CreateEncryptor();
  12. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  13. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  14. }

C#版解密
  1. public static string Decrypt(string toDecrypt,string key)
  2. {
  3. // 256-AES key
  4. //byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  5. byte[] keyArray = Encoding.UTF8.GetBytes(Get16ByteMd5(key));
  6. byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
  7. RijndaelManaged rDel = new RijndaelManaged();
  8. rDel.Key = keyArray;
  9. rDel.Mode = CipherMode.ECB;
  10. rDel.Padding = PaddingMode.PKCS7;
  11. //rDel.IV = IV;
  12. ICryptoTransform cTransform = rDel.CreateDecryptor();
  13. byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  14. return UTF8Encoding.UTF8.GetString(resultArray);
  15. }

java版用到的代码:
  1. public static String ShortMd5(String sourceStr)
  2. {
  3. String result = "";
  4. try
  5. {
  6. MessageDigest md = MessageDigest.getInstance("MD5");
  7. md.update(sourceStr.getBytes());
  8. byte b[] = md.digest();
  9. int i;
  10. StringBuffer buf = new StringBuffer("");
  11. for (int offset = 0; offset < b.length; offset++)
  12. {
  13. i = b[offset];
  14. if (i < 0)
  15. i += 256;
  16. if (i < 16)
  17. buf.append("0");
  18. buf.append(Integer.toHexString(i));
  19. }
  20. result = buf.toString().substring(8, 24);
  21. }
  22. catch (NoSuchAlgorithmException e)
  23. {
  24. System.out.println(e);
  25. }
  26. return result.toUpperCase();
  27. }

C#版用到的代码:
  1. public static string Get16ByteMd5(string ConvertString)
  2. {
  3. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  4. string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(ConvertString)), 4, 8);
  5. t2 = t2.Replace("-", "");
  6. return t2.ToUpper();
  7. }






java、C#配套版AES加密

标签:

原文地址:http://www.cnblogs.com/zwwhp/p/5477094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!