• 对称加密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));
        }
        
    }
  • 相关阅读:
    QuantLib 金融计算——案例之固息债的关键利率久期(KRD)
    Elasticsearch 最佳运维实践
    Ansible常用的Ansible Module
    中国七夕节( Chinese Valentine's Day)IT的浪漫情结
    Leetcode之C++刷题生死战
    无需Root可自动定时发送微信和短信的黑科技APP,支持跳过开屏启动广告
    tmp
    【整理中】可靠性基础_抽样检测
    【汇总】命名及标识
    Treelist父节点上下移
  • 原文地址:https://www.cnblogs.com/chengyangyang/p/9488245.html
Copyright © 2020-2023  润新知