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


只对文件进行base64编码 , 而不使用加解密/*对文件进行base64编码*/openssl enc -base64 -in plain.txt -out base64.txt/*对base64格式文件进行解密操作*/openssl enc -base64 -d -in base64.txt -out plain2.txt/*使用diff命令查看可知解码前后明文一样*/diff plain.txt plain2.txt不同方式的密码输入方式/*命令行输入 , 密码123456*/openssl enc -aes-128-cbc -in plain.txt -out out.txt -pass pass:123456/*文件输入 , 密码123456*/echo 123456 > passwd.txtopenssl enc -aes-128-cbc -in plain.txt -out out.txt -pass file:passwd.txt/*环境变量输入 , 密码123456*/passwd=123456export passwdopenssl enc -aes-128-cbc -in plain.txt -out out.txt -pass env:passwd/*从文件描述输入*/ openssl enc -aes-128-cbc -in plain.txt -out out.txt -pass fd:1/*从标准输入输入*/ openssl enc -aes-128-cbc -in plain.txt -out out.txt -pass stdin Java实现对称加密DES/** * 生成 DES 算法密钥 * @return byte[] * @throws Exception */public static byte[] generateDESKey() throws Exception {KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");// must be equal to 56keyGenerator.init(56);SecretKey secretKey = keyGenerator.generateKey();byte[] encodedKey = secretKey.getEncoded();return encodedKey;}/** * DES加密 * @param encodedKey generateDESKey生成的密钥 * @param dataBytes byte[]形式的待加密数据 * @return byte[] * @throws Exception */public static byte[] encryptByDES(byte[] encodedKey, byte[] dataBytes) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey, "DES");Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedData = http://kandian.youth.cn/index/cipher.doFinal(dataBytes);return encryptedData;}/** * DES解密 * @param encodedKey generateDESKey生成的密钥 * @param encryptedData byte[]形式的待解密数据 * @return byte[] * @throws Exception */public static byte[] decryptByDES(byte[] encodedKey, byte[] encryptedData) throws Exception {SecretKey secretKey = new SecretKeySpec(encodedKey,"DES");Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decryptedData = http://kandian.youth.cn/index/cipher.doFinal(encryptedData);return decryptedData;}基础版本使用方法如下:@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);}


推荐阅读