Java开发中的加密、解密、签名、验签,密钥,证书(上篇)( 二 )

基础版本使用方法如下:@Testpublic void testDES_1() throws Exception {byte[] encodedKey = SecurityUtil.generateDESKey();String data = "http://kandian.youth.cn/index/this is a good boy";byte[] encryptedData = http://kandian.youth.cn/index/SecurityUtil.encryptByDES(encodedKey, data.getBytes());byte[] decryptedData = SecurityUtil.decryptByDES(encodedKey, encryptedData);Assert.assertEquals(data, new String(decryptedData));}可以看到 , 以上的方法使用起来并不友好 , 参数、返回等大量存在byte[] , 不便于理解 , 中间结果不便于查看和传输 , 比如如果需要将encryptedData返回给下游系统 , 那么还得使用Base64进行处理 , 基于此 , 我对在上述接口基础上进一步进行封装 , 使其使用起来更贴近日常使用场景 。
优化版本:/** * 生成 DES 算法密钥 * @return 经过Base64编码的字符串密钥 * @throws Exception */public static String generateDESKeyStr() throws Exception {return Base64.encodeBase64String(generateDESKey());}/** * DES加密 * @param key 经过Base64编码的字符串密钥 * @param data String形式的待加密数据 * @return 经过Base64编码的加密数据 * @throws Exception */public static String encryptByDES(String key, String data) throws Exception {byte[] encodedKey = Base64.decodeBase64(key);byte[] dataBytes = data.getBytes();byte[] encryptedData = http://kandian.youth.cn/index/encryptByDES(encodedKey, dataBytes);return Base64.encodeBase64String(encryptedData);}/** * DES解密 * @param key 经过Base64编码的字符串密钥 * @param data String形式的待解密数据 * @return 原始数据 * @throws Exception */public static String decryptByDES(String key, String data) throws Exception {byte[] encodedKey = Base64.decodeBase64(key);byte[] dataBytes = Base64.decodeBase64(data);byte[] decryptedData = decryptByDES(encodedKey, dataBytes);return new String(decryptedData);}优化版本使用方法如下:@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;}