分享

AES加密代码 【重要】

 一本正经地胡闹 2019-09-06

private static byte[] encrypt(byte[] text, byte[] key) throws Exception {

SecretKeySpec aesKey = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, aesKey);

return cipher.doFinal(text);

}

private static byte[] decrypt(byte[] text, byte[] key) throws Exception {

SecretKeySpec aesKey = new SecretKeySpec(key, "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, aesKey);

return cipher.doFinal(text);

}

public static String encodeAES(String text, String key) throws Exception {

byte[] keyBytes = DigestUtils.md5(key);//在线网站上没有md5加密,直接getBytes

//等同于 MessageDigest.getInstance("MD5").digest(key.getBytes())

byte[] textBytes = text.getBytes();

byte[] aesBytyes = encrypt(textBytes, keyBytes);

return new String(Base64.encodeBase64(aesBytyes));

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多