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

Lewa ·
更新时间:2024-09-21
· 911 次阅读

喜欢扣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 } } }

原创文章 58获赞 382访问量 21万+ 关注 私信 展开阅读全文
作者:程序员小哲



加密 JAVA 加密与解密 常识 base64

需要 登录 后方可回复, 如果你还没有账号请 注册新账号