package com.xzz.md5encryption;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/**
* @ClassName ENCRYPTIONS
* @Date 20210427
* @Author lcl-xiezezhong
* @Version 1.0
* java 常用的加密方式
* Base64加密算法
* MD5加密
* 对称加密
* 非对称加密
*/
@Slf4j
@RestController
public class MD5ENCRYPTIONS {
/**
* 测试md5加密
*/
@RequestMapping("MD5")
public void ceshimd5(){
log.info("开始进行加密=============MD5");
Scanner scannerName = new Scanner(System.in);
log.info("请输入账户名称:");
String md5Name = null;
try {
md5Name = md5Name(scannerName.nextLine());
}catch (Exception e){
e.printStackTrace();
log.error("调用MD5名称异常");
}
log.info("加密成功MD5:"+md5Name);
//在控制台输出
Scanner scanner = new Scanner(System.in);
log.info("请输入支付密码:");
String decode = JM(scanner.nextLine());
log.info("解密成功BASE64:"+decode);
}
private String JM(String md5Number) {
log.info("开始进行加密=============BASE64");
BASE64Encoder base64Encoder = new BASE64Encoder();
String base64En = null;
try {
base64En = base64Encoder.encode(md5Number.getBytes("GBK"));
log.info("加密BASE64:"+base64En);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BASE64Decoder base64Decoder = new BASE64Decoder();
String decode = null;
try {
decode = new String(base64Decoder.decodeBuffer(base64En));
log.info("解密BASE64:"+decode);
} catch (IOException e) {
e.printStackTrace();
}
return decode;
}
public String md5Name(String name){
String md5 = DigestUtils.md5Hex(name).toUpperCase();
log.info("加密MD5:"+md5);
return md5;
}
}
MD5和Base64
最新推荐文章于 2024-07-08 17:27:15 发布