1.c#版本java
/// <summary> /// Aes加密解密.c#版 /// </summary> public class BjfxEncryptHelper { /// <summary> /// 加密解密Key /// </summary> ////文檔示例 public const string Key = "asjfasfaskjdlfljasjfsafsfsadfsdffds";/// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文</param> /// <returns></returns> public static string Encrypt(string encryptStr) { return Encrypt(encryptStr, Key); } /// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文</param> /// <param name="key">密鑰</param> /// <returns></returns> public static string Encrypt(string encryptStr, string key) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> /// <param name="decryptStr">密文</param> /// <param name="key">密鑰</param> /// <returns></returns> public static string Decrypt(string decryptStr) { return Decrypt(decryptStr, Key); } /// <summary> /// AES解密 /// </summary> /// <param name="decryptStr">密文</param> /// <param name="key">密鑰</param> /// <returns></returns> public static string Decrypt(string decryptStr, string key) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Convert.FromBase64String(decryptStr); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } }
2.Java版本算法
import java.math.BigInteger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64Decoder; /** * */ public class EncryptUtil { /** * 密鑰 */ private final String Key="asjfasfaskjdlfljasjfsafsfsadfsdffds"; /** * 算法 */ private final String Alogorithmstr= "AES/ECB/PKCS5Padding"; /** * 密鑰 */ public String getKey() { return Key; } /** * 算法 */ public String getAlogorithmstr() { return Alogorithmstr; } /** * aes解密 * * @param encrypt 內容 * @return * @throws Exception */ public String aesDecrypt(String encrypt) throws Exception { return aesDecrypt(encrypt, Key); } /** * aes加密 * * @param content * @return * @throws Exception */ public String aesEncrypt(String content) throws Exception { return aesEncrypt(content, Key); } /** * 將byte[]轉爲各類進制的字符串 * * @param bytes byte[] * @param radix 能夠轉換進制的範圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出範圍後變爲10進制 * @return 轉換後的字符串 */ public String binary(byte[] bytes, int radix) { return new BigInteger(1, bytes).toString(radix);// 這裏的1表明正數 } /** * base 64 encode * * @param bytes 待編碼的byte[] * @return 編碼後的base 64 code */ public String base64Encode(byte[] bytes) { return Base64.encodeBase64String(bytes); } /** * base 64 decode * * @param base64Code 待解碼的base 64 code * @return 解碼後的byte[] * @throws Exception */ public byte[] base64Decode(String base64Code) throws Exception { return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code); } /** * AES加密 * * @param content 待加密的內容 * @param encryptKey 加密密鑰 * @return 加密後的byte[] * @throws Exception */ public byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(Alogorithmstr); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES")); return cipher.doFinal(content.getBytes("utf-8")); } /** * AES加密爲base 64 code * * @param content 待加密的內容 * @param encryptKey 加密密鑰 * @return 加密後的base 64 code * @throws Exception */ public String aesEncrypt(String content, String encryptKey) throws Exception { return base64Encode(aesEncryptToBytes(content, encryptKey)); } /** * AES解密 * * @param encryptBytes 待解密的byte[] * @param decryptKey 解密密鑰 * @return 解密後的String * @throws Exception */ public String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(Alogorithmstr); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES")); byte[] decryptBytes = cipher.doFinal(encryptBytes); return new String(decryptBytes); } /** * 將base 64 code AES解密 * * @param encryptStr 待解密的base 64 code * @param decryptKey 解密密鑰 * @return 解密後的string * @throws Exception */ public String aesDecrypt(String encryptStr, String decryptKey) throws Exception { return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey); } }
java-pom.xmlapache
引入以下依賴c#
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
3.測試測試
/// <summary> /// 測試加密解密 /// </summary> static void TestFxEncrypt() { string encryptStr, decryptStr; encryptStr = "測試數據看看看卡"; Console.WriteLine("============== 256位AES加密 ============\n"); Console.WriteLine("加密前:" + encryptStr); Console.WriteLine("密 鑰:" + BjfxEncryptHelper.Key); Console.WriteLine(); decryptStr = BjfxEncryptHelper.Encrypt(encryptStr); Console.WriteLine("加密後:" + decryptStr); encryptStr = BjfxEncryptHelper.Decrypt(decryptStr); Console.WriteLine("解密後:" + encryptStr); //c#加密 //1:XtO1f0R4pCOhwLMKiku5+OWeBJTsTgzvSUb5fGQgoUM= //java加密 //2:XtO1f0R4pCOhwLMKiku5+OWeBJTsTgzvSUb5fGQgoUM= }
注意:編碼
測試代碼用到的密碼與示例代碼用的密鑰不一致,真實的密鑰的字符長度應爲16位.加密