Java开发中的加密、解密、签名、验签,密钥,证书(上篇)( 三 )
优化版本使用方法如下:@Testpublic void testDES_2() throws Exception {String key = SecurityUtil.generateDESKeyStr();String data = "http://kandian.youth.cn/index/this is a good boy";String encryptedData = http://kandian.youth.cn/index/SecurityUtil.encryptByDES(key, data);String decryptedData = SecurityUtil.decryptByDES(key, encryptedData);Assert.assertEquals(data, decryptedData);}
这里补充一下 , 在实际项目开发过程中 , 还真遇见不少同学对Base64理解有误的情况 , 对于以上处理和转换过程理解有难度的同学 , 可以戳一下这里
3DES/** * 生成 3DES 算法密钥 * @return byte[] * @throws Exception */public static byte[] generate3DESKey() throws Exception {KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");// must be equal to 112 or 168keyGenerator.init(168);SecretKey secretKey = keyGenerator.generateKey();byte[] encodedKey = secretKey.getEncoded();return encodedKey;}/** * 3DES加密 * @param encodedKey generate3DESKey生成的密钥 * @param dataBytes byte[]形式的待加密数据 * @return byte[] * @throws Exception */public static byte[] encryptBy3DES(byte[] encodedKey, byte[] dataBytes) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey, "DESede");Cipher cipher = Cipher.getInstance("DESede");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedData = http://kandian.youth.cn/index/cipher.doFinal(dataBytes);return encryptedData;}/** * 3DES解密 * @param encodedKey generate3DESKey生成的密钥 * @param encryptedData byte[]形式的待解密数据 * @return byte[] * @throws Exception */public static byte[] decryptBy3DES(byte[] encodedKey, byte[] encryptedData) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey,"DESede");Cipher cipher = Cipher.getInstance("DESede");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decryptedData = http://kandian.youth.cn/index/cipher.doFinal(encryptedData);return decryptedData;}
使用方法如下:@Testpublic void test3DES() throws Exception {byte[] encodedKey = SecurityUtil.generate3DESKey();String data = "http://kandian.youth.cn/index/this is a good boy";byte[] encryptedData = http://kandian.youth.cn/index/SecurityUtil.encryptBy3DES(encodedKey, data.getBytes());byte[] decryptedData = SecurityUtil.decryptBy3DES(encodedKey, encryptedData);Assert.assertEquals(data, new String(decryptedData));}
AES/** * 生成 AES 算法密钥 * @return byte[] * @throws Exception */public static byte[] generateAESKey() throws Exception {KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");// must be equal to 128, 192 or 256// 但是当你使用 192/256 时 , 会收到:// java.security.InvalidKeyException: Illegal key size or default parameterskeyGenerator.init(128);SecretKey secretKey = keyGenerator.generateKey();byte[] encodedKey = secretKey.getEncoded();return encodedKey;}/** * AES加密 * @param encodedKey generateAESKey生成的密钥 * @param dataBytes byte[]形式的待加密数据 * @return byte[] * @throws Exception */public static byte[] encryptByAES(byte[] encodedKey, byte[] dataBytes) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedData = http://kandian.youth.cn/index/cipher.doFinal(dataBytes);return encryptedData;}/** * AES密 * @param encodedKey generateAESSKey生成的密钥 * @param encryptedData byte[]形式的待解密数据 * @return byte[] * @throws Exception */public static byte[] decryptByAES(byte[] encodedKey, byte[] encryptedData) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey,"AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decryptedData = http://kandian.youth.cn/index/cipher.doFinal(encryptedData);return decryptedData;}
推荐阅读
- 谷歌建立新AI系统 可开发甜品配方
- Eyeware Beam使用iPhone追踪玩家在游戏中的眼睛运动
- 计算机专业大一下学期,该选择学习Java还是Python
- “全能神”开发谷歌应用APP传播邪教教义
- 联想正开发下一代ThinkReality智能眼镜
- Apple Glass正进入第二开发阶段 目标成品重量轻 续航长
- 运动计数开发项目的对抗赛:飞算全自动软件工程平台碾压传统模式
- 程序员为教师妻子开发应用:将iPhone变成文档摄像头
- 想自学Python来开发爬虫,需要按照哪几个阶段制定学习计划
- 未来想进入AI领域,该学习Python还是Java大数据开发