package day15_java_API; import java.util.Base64; import java.util.Base64.Encoder; /** * Base64.Encoder:进行加密处理 * 加密处理:public byte[] encode(byte[] src); * Base64-Decoder:进行解密处理 * 解密处理:public byte[] decode (String src); * @author asus *实现加密与解密操作 */ class StringUtil{ private static final String SALT = "hello"; private static final int REPEAT = 4;//加密4次 /** * * @param str 要加密的字符,需要与盐值整合 * @param repeat 加密的重复次数 * @return 加密后的数据 */ public static String encode(String str) {//进行加密处理 String temp = str+"{"+SALT+"}"; // 盐值内容不对外公布 byte data [] = temp.getBytes(); //将字符串变为字节数组 for (int i = 0; i < REPEAT; i++) { data = Base64.getEncoder().encode(data); //重复加密 } return new String (data); } /** * 解密处理 * @param str 要解密的内容 * @return 解密后的原始数据 */ public static String decode(String str) { byte data [] = str.getBytes(); for (int i = 0; i < REPEAT; i++) { data = Base64.getDecoder().decode(data); } return new String (data).replaceAll("\\{\\w+\\}", ""); } } public class java_Base64 { public static void main(String[] args) { /* String salt = "Hello" ; //盐值操作 String msg = "www.TaoLan.com"+"{"+salt+"}"; String encMsg = new String(Base64.getEncoder().encode(msg.getBytes()));//数据加密 System.out.println("加密后结果:"+encMsg); //加密后结果 String oldMsg = new String (Base64.getDecoder().decode(encMsg)); System.out.println("解密后结果:"+oldMsg); //解密后结果 */ String str = StringUtil.encode("www.TaoLan.com"); System.out.println("加密后结果:"+str); String strA = StringUtil.decode(str); System.out.println("解密后的结果:"+strA); } }
数据的加密与解密
猜你喜欢
转载自blog.csdn.net/qq_41663470/article/details/113773361
今日推荐
周排行