AES
问题:
- 密钥为十六进制的字符串
- 密文为十六进制的字符串
AES操作过程
-
加密
//1.将明文转化为字节数组 byte[] data = message.getBytes("UTF-8"); // 2.将密钥也转为字节数组 byte[] key=... // 3. 加密 encrypted // 3.1 创建cipher (译为密码 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 模式,填充 // 3.2 创建 SecretKey 密钥 SecretKey keySpec = new SecretKeySpec(key, "AES"); // 3.3 用密钥进行初始化 cipher.init(Cipher.ENCRYPT_MODE, keySpec); // 3.4 加密 返回byte[] cipher.doFinal(data);
-
解密
//key 和 data都是二进制字节数组 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKey keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); cipher.doFinal(input);
-
完整代码:
import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; public class AESTest2 { /** * 加密 * * @param content * 需要加密的内容 * @param password * 加密密码 * @return */ private static byte[] encryptData_AES(String content, String password) { try { byte[] enCodeFormat = parseHexStr2Byte(password); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES");// 创建密码器 byte[] byteContent = content.getBytes("UTF-8"); cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(byteContent); return result; // 加密 } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将16进制转换为二进制 * * @param hexStr * @return */ private 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); //将radix进制的字符串转化为十进制 int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low);//转化为十进制 然后在强转为二进制 } return result; } /** * 将二进制转换成16进制 * * @param buf * @return */ private 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(); } /** * 字符串加密 * * @param srcStr * 加密字符串 * @param password * 加密密钥 * */ public static String encryptStr(String srcStr, String password) { byte[] encryptResult = encryptData_AES(srcStr, password); String encryptResultStr = parseByte2HexStr(encryptResult); return encryptResultStr; } /** * 解密 * * @param encrystr * 密文 * @param password * 密钥 * @return 解密后的字符串 */ public static String decryptStr(String encryptStr,String password) { byte[] decryptResult=decryptData_AES(encryptStr,password); try { String decryptStr= new String(decryptResult,"UTF-8"); return decryptStr; } catch (Exception e){ e.printStackTrace(); } return null; } /** * 解密 * * @param encrystr * 密文 * @param password * 密钥 * @return 解密后的二进制数据 */ public static byte[] decryptData_AES(String encryptStr,String password){ try { byte[] deCodeFormat = parseHexStr2Byte(password); byte[] byteContent = parseHexStr2Byte(encryptStr); SecretKeySpec key = new SecretKeySpec(deCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES");// 创建密码器 cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(byteContent); return result; // 解密 } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) throws UnsupportedEncodingException { String encryptMessage=encryptStr("2015021115122300", "ADED51171767497B94368AF51050754D"); System.out.println(encryptMessage); String message=decryptStr(encryptMessage,"ADED51171767497B94368AF51050754D"); System.out.println(message); } }