对称加密DES加密
Posted chengyangyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对称加密DES加密相关的知识,希望对你有一定的参考价值。
DES加密: des是对称加密,加密和解密需要相同的秘钥,它的密码最长56位,必须是8的倍数,秘钥越长,越安全.
package com.trm.util.encrypt; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; /** * <p>加密/解密 * <p> * @author * */ public class Encryption { /** * DES密钥 * 注意:DES加密和解密过程中,密钥长度都必须是8的倍数,最长为56位 */ private static final String key = "12345678"; /** * DES加密 * @param str * @return */ @SuppressWarnings("restriction") public static String encrypt(String str) { String encryptStr = ""; try{ SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(key.getBytes()); //创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); //用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, random); //现在,获取数据并加密 //正式执行加密操作 encryptStr = new sun.misc.BASE64Encoder().encode(cipher.doFinal(str.getBytes())); }catch(Throwable e){ e.printStackTrace(); } return encryptStr; } /** * DES解密 * @param str * @return */ @SuppressWarnings("restriction") public static String decrypt(String str) { String decryptStr = ""; try { // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(key.getBytes()); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 decryptStr = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(str))); } catch (Exception e) { throw new RuntimeException("解密错误,错误信息:", e); } return decryptStr; } public static void main(String[] args) { String jdbcPassword = "123"; //String password = decrypt(jdbcPassword); System.out.println(encrypt(jdbcPassword)); } }
以上是关于对称加密DES加密的主要内容,如果未能解决你的问题,请参考以下文章