• 使用base64实现用户密码加密


    一个类和一个jar包即可搞定

    jar包:org.apache.commons.codec.binary.Base64;

    下载地址:https://mvnrepository.com/artifact/commons-codec/commons-codec

    类:

    package com.xxl.job.admin.util;
     
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
     
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
     
    import org.apache.commons.codec.binary.Base64;
     
    public class CryptoUtil {
     
        public static Key DEFAULT_KEY = null;
     
        public static final String DEFAULT_SECRET_KEY = "1qaz2wsx3edc$RFV%TGB^YHN&UJM";
     
        public static final String DES = "DES";
     
        static {
            DEFAULT_KEY = obtainKey(DEFAULT_SECRET_KEY);
        }
     
        /**
         * 获得key
         **/
        public static Key obtainKey(String key) {
            if (key == null) {
                return DEFAULT_KEY;
            }
            KeyGenerator generator = null;
            try {
                generator = KeyGenerator.getInstance(DES);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            generator.init(new SecureRandom(key.getBytes()));
            Key key1 = generator.generateKey();
            generator = null;
            return key1;
        }
     
        /**
         * 加密<br>
         * String明文输入,String密文输出
         */
        public static String encode(String str) {
            return encode(null, str);
        }
     
        /**
         * 加密<br>
         * String明文输入,String密文输出
         */
        public static String encode(String key, String str) {
            return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes()));
            // return Hex.encodeHexString(obtainEncode(key, str.getBytes()));
            // 可以转化为16进制数据
        }
     
        /**
         * 解密<br>
         * 以String密文输入,String明文输出
         */
        public static String decode(String str) {
            return decode(null, str);
        }
     
        /**
         * 解密<br>
         * 以String密文输入,String明文输出
         */
        public static String decode(String key, String str) {
            return new String(obtainDecode(key, Base64.decodeBase64(str)));
            // 可以转化为16进制的数据
    //      try {
    //          return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray())));
    //      } catch (DecoderException e) {
    //          // TODO Auto-generated catch block
    //          e.printStackTrace();
    //      }
        }
     
        /**
         * 加密<br>
         * 以byte[]明文输入,byte[]密文输出
         */
        private static byte[] obtainEncode(String key, byte[] str) {
            byte[] byteFina = null;
            Cipher cipher;
            try {
                Key key1 = obtainKey(key);
                cipher = Cipher.getInstance(DES);
                cipher.init(Cipher.ENCRYPT_MODE, key1);
                byteFina = cipher.doFinal(str);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cipher = null;
            }
            return byteFina;
        }
     
        @Override
        public int hashCode() {
            return super.hashCode();
        }
     
        /**
         * 解密<br>
         * 以byte[]密文输入,以byte[]明文输出
         */
        private static byte[] obtainDecode(String key, byte[] str) {
            Cipher cipher;
            byte[] byteFina = null;
            try {
                Key key1 = obtainKey(key);
                cipher = Cipher.getInstance(DES);
                cipher.init(Cipher.DECRYPT_MODE, key1);
                byteFina = cipher.doFinal(str);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cipher = null;
            }
            return byteFina;
        }
     
        public static void main(String[] args) {
            String a = "root";
    //        String key = "yang测试";
            System.out.println(a);
            String m = encode(a);
            System.out.println(m);
            String n = decode(m);
            System.out.println(n);
        }
    
    --------------------- 
    作者:yang1393214887 
    来源:CSDN 
    原文:https://blog.csdn.net/yang1393214887/article/details/83411051 
    

      

  • 相关阅读:
    打开Intellij Idea 2020.1 提示 cannot load a jdk class: com.sun.jdi.Field
    win10触摸板设置为连接鼠标不打开后就自动关闭
    git配置账号
    HTTP请求中的Form Data与Request Payload的区别
    VUE—axios自定义请求配置—3、transformRequest在向服务器发送前,修改请求数据(图文详情)
    在Sass中,我们可以使用“@for”来实现循环操作
    vue项目引入背景图报Module not found: Error: Can't resolve './src/assets/theme/logo_blue.png' in'xxx'错误
    Importing code style from ESLint
    ESLint fix自动修复所有格式问题
    【T07】不要低估tcp的性能
  • 原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/11195844.html
Copyright © 2020-2023  润新知