ASE实现数据加密

本文主要实现加密代码,更多ASE简介,还请在简介的连接去观看大佬的介绍
一、AES简介 AES为分组密码,分组密码也就是把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文。在AES标准规范中,分组长度只能是128位,也就是说,每个分组为16个字节(每个字节8位)。密钥的长度可以使用128位、192位或256位。密钥的长度不同,推荐加密轮数也不同,推荐加密轮数也不同,如下表所示:
AES 密钥长度(32位比特字) 分组长度(32位比特字) 加密轮数
AES-128 4 4 10
AES-192 6 4 12
AES-256 8 4 14
摘自:[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 staticCipherencCipher = null; private staticCipherdecCipher = 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 staticString 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 staticbyte[] 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有货领取更多资料 ASE实现数据加密
文章图片

如遇到问题可以联系小编。微信:372787553,互相学习
技术博客:https://blog.csdn.net/weixin_38937840
SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud
【ASE实现数据加密】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

    推荐阅读