• Base64


    package com.util;
    
    import java.nio.charset.StandardCharsets;
    
    
    public class Base64 {
    
        final static java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
        final static java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
    
        /**
         * 符串加密
         *
         * @param text
         * @return
         */
        public static String encode(String text) {
    //        byte[] textByte = text.getBytes(StandardCharsets.UTF_8);
    //        String encodedText = encoder.encodeToString(textByte);
    //        return encodedText;
            return encoder.encodeToString( text.getBytes( StandardCharsets.UTF_8 ) ); // StandardCharsets.UTF_8 代替了 "UTF-8
        }
    
        /**
         * 将加密后的字符串进行解密
         *
         * @param encodedText
         * @return
         */
        public static String decode(String encodedText) {
            return new String( decoder.decode( encodedText ), StandardCharsets.UTF_8 );
        }
    
        // 测试
        //
        public static void main(String[] args) {
    
            String url = "jdbc:mysql://118.24.13.38:3308/goods?characterEncoding=utf8&useSSL=false";
            String username = "11353268@qq.com.com";
            String password = "pass=p@sSW0rd";
    
            // 加密
            System.out.println( "====  [加密后] 用户名/密码  =====" );
            System.out.println( Base64.encode( url ) );
            System.out.println( Base64.encode( username ) );
            System.out.println( Base64.encode( password ) );
    
    
            // 解密
            System.out.println( "
    ====  [解密后] 用户名/密码  =====" );
            System.out.println( Base64.decode( Base64.encode( url ) ) );
            System.out.println( Base64.decode( Base64.encode( username ) ) );
            System.out.println( Base64.decode( Base64.encode( password ) ) );
            System.out.println( "===============" + Base64.decode( Base64.encode( password ) ) );
    
        }
    }
    

    测试结果:

      

     
  • 相关阅读:
    easyUI之tree
    MSSQL索引优化
    MongoDB学习笔记(一) MongoDB介绍及安装
    项目经理必备的11种人际关系技能
    http协议详细介绍
    ERP存储过程
    UVA1339 UVALive3213 POJ2159 ZOJ2658 Ancient Cipher【密码】
    UVA1588 UVALive3712 POJ3158 Kickdown
    UVA1588 UVALive3712 POJ3158 Kickdown
    UVA10340 POJ1936 ZOJ1970 All in All【字符串匹配】
  • 原文地址:https://www.cnblogs.com/Alexr/p/13672600.html
Copyright © 2020-2023  润新知