import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.nio.charset.Charset; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; import java.util.Scanner; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; //import sun.misc.BASE64Decoder; //import sun.misc.BASE64Encoder; //import RC4; public class AES { public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } public static String AES_Encode(String encodeRules, String content) { try { // 1.构造密钥生成器,指定为AES算法,不区分大小写 KeyGenerator keygen = KeyGenerator.getInstance("AES"); // 2.根据ecnodeRules规则初始化密钥生成器 // 生成一个128位的随机源,根据传入的字节数组 // keygen.init(128, new SecureRandom(encodeRules.getBytes())); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(encodeRules.getBytes()); keygen.init(128, secureRandom); // 3.产生原始对称密钥 SecretKey original_key = keygen.generateKey(); // 4.获得原始对称密钥的字节数组 byte[] raw = original_key.getEncoded(); // 5.根据字节数组生成AES密钥 SecretKey key = new SecretKeySpec(raw, "AES"); // 6.根据指定算法AES自成密码器 Cipher cipher = Cipher.getInstance("AES"); // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY cipher.init(Cipher.ENCRYPT_MODE, key); // 8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码 byte[] byte_encode = content.getBytes("utf-8"); // 9.根据密码器的初始化方式--加密:将数据加密 byte[] byte_AES = cipher.doFinal(byte_encode); // 10.将加密后的数据转换为字符串 // 这里用Base64Encoder中会找不到包 // 解决办法: // 在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。 // String AES_encode=new String(new BASE64Encoder().encode(byte_AES)); String AES_encode = new String(bytesToHexString(byte_AES)); // 11.将字符串返回 return AES_encode; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // 如果有错就返加nulll return null; } /* * 解密 * 解密过程: * 1.同加密1-4步 * 2.将加密后的字符串反纺成byte[]数组 * 3.将加密内容解密 */ public static String AES_Decode(String encodeRules, String content) { String ss; try { // 1.构造密钥生成器,指定为AES算法,不区分大小写 KeyGenerator keygen = KeyGenerator.getInstance("AES"); // 2.根据ecnodeRules规则初始化密钥生成器 // 生成一个128位的随机源,根据传入的字节数组 // keygen.init(128, new SecureRandom(encodeRules.getBytes())); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(encodeRules.getBytes()); keygen.init(128, secureRandom); // 3.产生原始对称密钥 SecretKey original_key = keygen.generateKey(); // 4.获得原始对称密钥的字节数组 byte[] raw = original_key.getEncoded(); // 5.根据字节数组生成AES密钥 SecretKey key = new SecretKeySpec(raw, "AES"); // 6.根据指定算法AES自成密码器 Cipher cipher = Cipher.getInstance("AES"); // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY cipher.init(Cipher.DECRYPT_MODE, key); // 8.将加密并编码后的内容解码成字节数组 byte[] byte_content = hexStringToBytes(content); // byte [] byte_content= new BASE64Decoder().decodeBuffer(content); // System.out.println("ttttttttttttttttttttttttttt"); // System.out.println(new String(byte_content, "UTF-8")); /* * 解密 */ byte[] byte_decode = cipher.doFinal(byte_content); String AES_decode = new String(byte_decode, "utf-8"); return AES_decode; } catch (NoSuchAlgorithmException e) { ss = "NoSuchAlgorithmException"; e.printStackTrace(); } catch (NoSuchPaddingException e) { ss = "NoSuchPaddingException"; e.printStackTrace(); } catch (InvalidKeyException e) { ss = "InvalidKeyException"; e.printStackTrace(); } catch (IOException e) { ss = "IOException"; e.printStackTrace(); } catch (IllegalBlockSizeException e) { ss = "IllegalBlockSizeException"; e.printStackTrace(); } catch (BadPaddingException e) { ss = "BadPaddingException"; e.printStackTrace(); } // 如果有错就返加nulll return ss; } public static void main(String[] args) { // MyClass se=new MyClass(); // Scanner scanner=new Scanner(System.in); /* * 加密 */ System.out.println("使用AES对称加密,请输入加密的规则"); // String encodeRules=scanner.next(); System.out.println("请输入要加密的内容:"); String encodeRules = "a02254eb247146dfa446c4b2795ebf90"; String content = "32770"; String outstr = AES_Encode(encodeRules, content); System.out.println("根据输入的规则" + encodeRules + "加密后的密文是:" + outstr); /* * 解密 */ System.out.println("使用AES对称解密,请输入加密的规则:(须与加密相同)"); String byte_str = "0212f41b372fe457c3f7df0757151615"; outstr = AES_Decode(encodeRules, byte_str); System.out.println("根据输入的规则" + encodeRules + "解密后的明文是:" + outstr); } }