AES在项目中的使用
来源:互联网 发布:tia博途有哪些软件 编辑:程序博客网 时间:2024/05/05 01:21
AES在项目中的使用
Java AES 加解密。
/* * 加密方法 */ public static String encrypt(String message){ byte[] unencrypted = message.getBytes(Charset.forName("utf-8")); byte[] aesKey = "0123456789123456".getBytes(); // 设置加密模式为AES的CBC模式 Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16)); try { cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } // 加密 byte[] encrypted = null; try { encrypted = cipher.doFinal(unencrypted); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } // 使用BASE64对加密后的字符串进行编码 String base64Encrypted = new Base64().encodeToString(encrypted); System.out.println(base64Encrypted); return base64Encrypted; }
/* * 解密方法 */ public static String decode(String text){ byte[] aesKey = "0123456789123456".getBytes(); Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } SecretKeySpec key_spec = new SecretKeySpec(aesKey, "AES"); IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16)); try { cipher.init(Cipher.DECRYPT_MODE, key_spec, iv); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } // 使用BASE64对密文进行解码 byte[] encrypted = Base64.decodeBase64(text); // 解密 byte[] original = null; try { original = cipher.doFinal(encrypted); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } String str = new String(original); return str; }
0 0
- AES在项目中的使用
- java中RSA,AES,MD5算法在实际项目中的综合应用
- ANT 在项目中的使用
- lucene 在项目中的使用
- ldap在项目中的使用
- Granule 在项目中的使用
- GSON在项目中的使用
- log4j在项目中的使用
- CocoaPods在项目中的使用
- Aidl在项目中的使用
- gulp在项目中的使用
- 多线程在项目中的使用
- redis在项目中的使用
- mybatis在项目中的使用
- redis在项目中的使用
- Log4Net 在项目中的使用
- eslint在项目中的使用
- R2M在项目中的使用
- 节点的类型
- juicer.js模板引擎入门
- 学习html一周了,谈谈收获
- 购物车ListView中CheckBox的逻辑处理
- AutoLayout 专辑
- AES在项目中的使用
- 打包静态库生成bundle流程
- VirtualBox+mininet 网络配置手记2
- 成员变量和静态方法没有多态
- Android Fragment 真正的完全解析(上)
- 图像处理与分析--国内公司&就业
- Eclipse快捷键 10个最有用的快捷键
- Jmeter(1)----下载jmeter并运行一个简单用例
- 天才少年George Hotz在自家车库亲手打造了一台无人自驾驶汽车!