Base64加密工具

        正常来讲加密基本上永远都要伴随着解密,所谓的加密或者解密,往往都需要有一些规则,在JDK1.8开始,提供有新的加密处理操作类,Base64处理类
--Base64类
  在该类之中存在两个内部类:Base64.Base64.Decoder以及Base64.enBase64.Encoder,分别进行加密和解密处理
进行加密处理:byte[] decode(byte[] src)  
进行解密处理:byte[] encode(byte[] src)  
--范例:进行数据加密示例

1 public class MyBase64 {
2     public static void main(String[] args) {
3         String message = "你好,这是一条消息";       //要发送的消息内容
4         Base64.getEncoder().encode(message.getBytes());     //数据加密
5         System.out.println(new String(Base64.getEncoder().encode(message.getBytes())));
6     }
7 }

--运行结果

5L2g5aW9LOi/meaYr+S4gOadoea2iOaBrw==

Process finished with exit code 0

--进行解密操作

public class MyBase64 {
    public static void main(String[] args) {
        String message = "你好,这是一条消息";       //要发送的消息内容
        String encoder = new String(Base64.getEncoder().encode(message.getBytes()));//数据加密
        String decode = new String(Base64.getDecoder().decode(encoder));
        System.out.println(decode);
    }
}

--我们知道Base64是一个公版的加密和解密算法,所以直接使用Base64进行数据的加密和解密并不安全,所以最好的做法是使用盐值操作,所谓盐值操作就是在所需要加密的字符串信息中追加一个盐值

1 public class MyBase64 {
2     public static void main(String[] args) {
3         String salt = "dnsaojnui";      //无序输入的盐值
4         String message = "你好,这是一条消息" + "{" + salt + "}";       //要发送的消息内容
5         String encoder = new String(Base64.getEncoder().encode(message.getBytes()));//数据加密
6         String decode = new String(Base64.getDecoder().decode(encoder));
7         System.out.println(decode);
8     }
9 }

--但是盐值任然是明文规定的,这样实际效果也并不是很好,最好的做法则是多次加密

 1 package 常用类库.base64加密与解密;
 2 
 3 
 4 import java.util.Base64;
 5 
 6 /**
 7  * @author : S K Y
 8  * @version :0.0.1
 9  */
10 class BaseUtil {        //加密的盐值与此处对外不公布
11     private static final String SALT = "dasjidnas";       //公版的盐值
12     private static final int REPEAT = 5;        ///加密的次数
13 
14     /**
15      * 加密处理
16      *
17      * @param password 要加密的字符串密码数据,需要与盐值配合
18      * @return 加密后的数据
19      */
20     public static String encode(String password) {
21         password = password + "{" + SALT + "}";
22         byte[] bytes = password.getBytes();
23         for (int i = 0; i < REPEAT; i++) {
24             bytes = Base64.getEncoder().encode(bytes);
25         }
26         return new String(bytes);
27     }
28 
29     /**
30      * 解密
31      *
32      * @param needDecode 需要解密的数据
33      * @return 解密的结果
34      */
35     public static String decode(String needDecode) {
36         byte[] bytes = needDecode.getBytes();
37         for (int i = 0; i < REPEAT; i++) {
38             bytes = Base64.getDecoder().decode(bytes);
39         }
40         return new String(bytes).replaceAll("\{\w+}", "");
41     }
42 }
43 
44 public class MyBase64 {
45     public static void main(String[] args) {
46         String password = "qwer123456";
47         String encode = BaseUtil.encode(password);
48         System.out.println(encode);
49         System.out.println(BaseUtil.decode(encode));
50     }
51 }

--运行结果

VjFSR2IyRXlTa2hVYmtaVFYwZDRUMXBYZUVabFJsSjBUbFJDYUUxWGVGcFdSelZIWVVaWmVGTnFSbGhXYldoUVZERkZPVkJSUFQwPQ==
qwer123456

Process finished with exit code 0

--此外,最好的方法是使用2-3中加密算法,同时得到一些完全不可解密的加密结果

原文地址:https://www.cnblogs.com/skykuqi/p/11379933.html