java、C#配套版AES加密

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. }






posted @ 2016-05-10 11:09  zwwhp  阅读(311)  评论(0编辑  收藏  举报