AES 加密解密爬坑

2020-04-03  本文已影响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;

    }

上一篇下一篇

猜你喜欢

热点阅读