(Development support library) Base64 encryption and decryption

Base64 encryption and decryption

Normally, encryption is basically always accompanied by decryption. The so-called encryption or decryption often requires some rules. A new set of encryption processing classes, Base64 processing, has been provided since JDK1.8. There are two internal classes in this class:

  • Base64.Encoder: perform encryption processing;
  • public byte[] encode​(byte[] src) Use the Base64 encoding scheme to encode all the bytes in the specified byte array into the newly allocated byte array
  • Base64.Decoder: Perform decryption processing;
  • public byte[] decode​(byte[] src): Use the Base64 encoding scheme to decode all bytes in the input byte array, and write the result to the newly allocated output byte array.

Example: Implement encryption and decryption operations

 

package 开发支持类库;

import java.util.Base64;

public class Base64类 {
	public static void main(String[] args) {
		//加密
		String msg = "加密内容";		//要发送的信息内容
		//getEncoder()返回一个Base64编码方案,encode利用此方法进行加密
		byte[] enMsg =  Base64.getEncoder().encode(msg.getBytes());	//数据加密
		String enString = new String(enMsg);	//转换为String
		System.out.println("加密后:"+enString);
		
		//解密
		byte[] deMsg = Base64.getDecoder().decode(enMsg); //解密,getDecoder()返回一个Base64编码方案,decode进行解密
		String deString = new String(deMsg);
		System.out.println("解密结果:"+deString);
	}
}

After encryption: vNPD3MTayN0=
Decryption result: encrypted content

Although Base64 can implement encryption and decryption processing, because it is a public version of the algorithm, it is often not safe to directly encrypt data, so the best way is to use salt operation .

String salt = "javaDemo";	//定义盐值
String msg = "加密内容"+"{"+salt+"}";		//加上了盐值,数据不只有加密内容

Even if the salt actually finds that the effect of encryption is not very good, the best way is to encrypt multiple times.

Example: complex encryption operation

package 开发支持类库;

import java.util.Base64;

public class Base64类 {
	public static void main(String[] args) {
		//加密
		String str =StringUtil.encode("加密内容") ;
		System.out.println(str);
		
		//解密
		str = StringUtil.decode(str);
		System.out.println(str);
	}
}


class StringUtil{
	private static final String SALT = "盐值";	//公共的盐值
	private static final int RPEAT = 3;	//加密次数
 	/**
	 * 加密处理
	 * @param str	要加密的字符串,需要与盐值整合
	 * @return	加密后的数据
	 */
	public static String encode(String str) {	//加密处理
		String temp = str +"{"+SALT+"}";	//盐值对外不公布
		byte data[] = temp.getBytes();	//将字符串变为字节数组
		
		for(int x = 0;x <RPEAT ;x++) {
			data = Base64.getEncoder().encode(data);	//重复加密
		}
		
		return  new String(data);	//字节数组转为字符串,返回加密结果
		
	}
	
	/**
	 * 解密处理
	 * @param str	解密的字符串
	 * @return 解密后的内容
	 */
	public static String decode(String str) {
		byte data[] = str.getBytes();//将字符串变为字节数组
		
		for(int x = 0;x <RPEAT ;x++) {
			data = Base64.getDecoder().decode(data);	//重复解密,之前加密多少次就解密多少次
		}
		
		return new String(data).replaceAll("\\{\\S+\\}", "");//S表示任何非空格的字符,将括号和里面所有内容替换成null,字节数组转为字符串,返回解密结果
		
	}
}

ZGs1UVJETk5WR0Y1VGpFM01HTTNWM1JZTUQwPQ==
encrypted content

It is best to use 2-3 encryption programs and find some encryption methods that are completely undecryptable.

Guess you like

Origin blog.csdn.net/weixin_46245201/article/details/112601248