Java Base64加密与解密——每天一个Java小常识

喜欢扣1支持下呗

加密
Base64.getEncoder().encodeToString(字节类型);
解密
Base64.getDecoder().decode(加密字符串);

import java.util.Base64;

public class Dmeo {
	public static void main(String[] args) {
		//定义字符串
		String s = "hello world";
		//将字符串转化为字节类型,通过Base64加密
		String s1 = Base64.getEncoder().encodeToString(s.getBytes());
		System.out.println(s1);//输出:aGVsbG8gd29ybGQ=
		//通过Base64解密
		byte[] s2 = Base64.getDecoder().decode(s1);
		for(int i=0;i<s2.length;i++) {
			//s2为ASCⅡ码,需要类型转化
			System.out.print((char)s2[i]);//输出:hello world
		}		
	}
}

在这里插入图片描述

原创文章 57 获赞 381 访问量 20万+

猜你喜欢

转载自blog.csdn.net/xiaozhezhe0470/article/details/105877393
今日推荐