美文网首页
AES 加密解密爬坑

AES 加密解密爬坑

作者: 做个积极向上的普通人 | 来源:发表于2020-04-03 18:08 被阅读0次

加密,要长度为 (16 *n),则getInstance("AES/ECB/NoPadding")中NoPadding是关键

public static byte[] encrypt(byte[] key, byte[] data) throws Exception {

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

        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");//"算法/模式/补码方式"

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted = cipher.doFinal(data);

        return encrypted;

    }

public static byte[] decrypt(byte[] key, byte[] data) throws Exception {

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

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

        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        byte[] decrypted = cipher.doFinal(data);

        return decrypted;

    }

相关文章

网友评论

      本文标题:AES 加密解密爬坑

      本文链接:https://www.haomeiwen.com/subject/kfukphtx.html