JAVA AES加密 對應的 C# 方法
阿新 • • 發佈:2019-02-07
由於最近在專案中用到,之前在網上找了好多,來來回回,終於整出來了。 貼出來以後用起來方便
C#
#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 += " "; } Byte[] returnBytes = new Byte[hexString.Length / 2]; for (Int32 i = 0; i < returnBytes.Length; i++) { returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); } return returnBytes; } #endregion
JAVA:
/** * 加密--把加密後的byte陣列先進行二進位制轉16進位制在進行base64編碼 * @param sSrc * @param sKey * @return * @throws Exception */ public static String encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { throw new IllegalArgumentException("Argument sKey is null."); } if (sKey.length() != 16) { throw new IllegalArgumentException( "Argument sKey'length is not 16."); } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8")); String tempStr = parseByte2HexStr(encrypted); Base64Encoder encoder = new Base64Encoder(); return encoder.encode(tempStr.getBytes("UTF-8")); } /** *解密--先 進行base64解碼,在進行16進位制轉為2進位制然後再解碼 * @param sSrc * @param sKey * @return * @throws Exception */ public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { throw new IllegalArgumentException("499"); } if (sKey.length() != 16) { throw new IllegalArgumentException("498"); } byte[] raw = sKey.getBytes("ASCII"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); Base64Encoder encoder = new Base64Encoder(); byte[] encrypted1 = encoder.decode(sSrc); String tempStr = new String(encrypted1, "utf-8"); encrypted1 = parseHexStr2Byte(tempStr); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original, "utf-8"); return originalString; } /** * 將二進位制轉換成16進位制 * * @param buf * @return */ public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 將16進位制轉換為二進位制 * * @param hexStr * @return */ public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; }