ASE实现数据加密

本文主要实现加密代码,更多ASE简介,还请在简介的连接去观看大佬的介绍

一、AES简介

AES为分组密码,分组密码也就是把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文。在AES标准规范中,分组长度只能是128位,也就是说,每个分组为16个字节(每个字节8位)。密钥的长度可以使用128位、192位或256位。密钥的长度不同,推荐加密轮数也不同,推荐加密轮数也不同,如下表所示:

AES密钥长度(32位比特字)分组长度(32位比特字)加密轮数
AES-1284410
AES-1926412
AES-2568414
摘自:[ASE介绍](https://blog.csdn.net/lingzhm/article/details/80775680)

二、加密代码

@Slf4j
public class ASEUtil {


	private static final String PASS_NEED = "javayh-haiji-dylan";
	private static final String INSTANCE = "AES/ECB/PKCS5Padding";
	private static SecretKeySpec key;
	private static  Cipher  encCipher = null;
	private static  Cipher  decCipher = null;
	
	static {
		byte[] enCodeFormat = Arrays.copyOf(PASS_NEED.getBytes(), 16);
		key = new SecretKeySpec(enCodeFormat, "AES");
		try {
			encCipher = Cipher.getInstance(INSTANCE);
			encCipher.init(Cipher.ENCRYPT_MODE, key);
			decCipher = Cipher.getInstance(INSTANCE);
			decCipher.init(Cipher.DECRYPT_MODE, key);
		} catch (NoSuchAlgorithmException e) {
			log.info(" NoSuchAlgorithmException is error {}", e.getMessage());
		} catch (NoSuchPaddingException e) {
			log.info(" NoSuchPaddingException is error {}", e.getMessage());
		} catch (InvalidKeyException e) {
			log.info(" InvalidKeyException is error {}", e.getMessage());
		}
	}
	
	public static String encryData(String plaintData){
		byte result[] = new byte[0];
		try {
			result = encCipher.doFinal(plaintData.getBytes());
		} catch (IllegalBlockSizeException e) {
			log.info(" IllegalBlockSizeException is error {}", e.getMessage());
		} catch (BadPaddingException e) {
			log.info(" BadPaddingException is error {}", e.getMessage());
		}
		return new String(Base64.encode(result));
	}
	
	public static  String decryData(String encData){
		byte result[] = new byte[0];
		try {
			result = decCipher.doFinal(Base64.decode(encData.getBytes()));
		}  catch (IllegalBlockSizeException e) {
			log.info(" IllegalBlockSizeException is error {}", e.getMessage());
		} catch (BadPaddingException e) {
			log.info(" BadPaddingException is error {}", e.getMessage());
		}
		return new String(result);
	}
	
	
	
	public static byte[] encryData(byte []plaintData){
		byte result[] = new byte[0];
		try {
			result = encCipher.doFinal(plaintData);
		} catch (IllegalBlockSizeException e) {
			log.info(" IllegalBlockSizeException is error {}", e.getMessage());
		} catch (BadPaddingException e) {
			log.info(" BadPaddingException is error {}", e.getMessage());
		}
		return result;
	}
	
	public static  byte[] decryData(byte [] encData){
		byte result[] = new byte[0];
		try {
			result = decCipher.doFinal(encData);
		} catch (IllegalBlockSizeException e) {
			log.info(" IllegalBlockSizeException is error {}", e.getMessage());
		} catch (BadPaddingException e) {
			log.info(" BadPaddingException is error {}", e.getMessage());
		}
		return result;
	}

//	public static void main(String[] args) throws BadPaddingException, IllegalBlockSizeException {
//		String admin = encryData("admin");
//		System.out.println(admin);
//	}
}

关注 Java有货领取更多资料

在这里插入图片描述
如遇到问题可以联系小编。微信:372787553,互相学习

技术博客:https://blog.csdn.net/weixin_38937840

SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud

Redis、Mongo、Rabbitmq、Kafka学习代码: https://github.com/Dylan-haiji/javayh-middleware

AlibabaCloud学习代码:https://github.com/Dylan-haiji/javayh-cloud-nacos
SpringBoot+SpringSecurity实现自定义登录学习代码:https://github.com/Dylan-haiji/javayh-distribution

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨同学~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值