AES使用CFB加密方式(java实现)

@Slf4j
public class AesUtils {

    /**
     * 编码格式
     */
    public static final String ENCODING = "utf-8";
    /**
     * 密钥算法
     */
    private static final String KEY_ALGORITHM = "AES";

    //padding模式
    //NoPadding
    //PKCS5Padding
    //ISO10126Padding
    //PaddingMode.Zeros
    //PaddingMode.PKCS7
    public static String encryptCFB(String content, String key) {
        try {
            Cipher aesCFB = Cipher.getInstance("AES/CFB/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(ENCODING), KEY_ALGORITHM);
            final Random random = new SecureRandom();
            byte[] iv = new byte[16];
            random.nextBytes(iv);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            aesCFB.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] contentBytes = content.getBytes();
            //补0
            int remainder = contentBytes.length % 16;
            byte[] finalBytes = new byte[contentBytes.length + 16 - remainder];
            if (remainder != 0){
                System.arraycopy(contentBytes,0, finalBytes,0, contentBytes.length);
            }else {
                finalBytes = contentBytes;
            }
            byte[] encryptBytes = aesCFB.doFinal(finalBytes);
            //结果数组加上iv
            byte[] bytes1 = new byte[contentBytes.length + 16];
            System.arraycopy(iv, 0,bytes1,0,16);
            System.arraycopy(encryptBytes,0,bytes1,16,contentBytes.length);
            return Base64.getEncoder().encodeToString(bytes1);
        } catch (Exception ex) {
            log.error("encryptCFB error:", ex);
        }
        return null;
    }

    /***
     * @paramcontent 待解密内容,字符串形式
     * @paramkey 解密用的密钥
     * @return 使用字符串形式返回解密内容
     * @throws Exception
     **/
    public static String decryptCFB(String content, String key) {
        try {
            Cipher aesCFB = Cipher.getInstance("AES/CFB/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
            byte[] decodeBytes = Base64.getMimeDecoder().decode(content);
            byte[] iv = new byte[16];
            byte[] realBytes = new byte[decodeBytes.length - 16];
            System.arraycopy(decodeBytes,0, iv,0,16);
            System.arraycopy(decodeBytes,16,realBytes,0,decodeBytes.length - 16);
            int remainder = realBytes.length % 16;
            byte[] finalBytes = new byte[realBytes.length + 16 - remainder];
            if (remainder != 0){
                System.arraycopy(realBytes,0, finalBytes,0, realBytes.length);
            }else {
                finalBytes = realBytes;
            }
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            aesCFB.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] result = aesCFB.doFinal(finalBytes);
            byte[] bytes1 = new byte[realBytes.length];
            System.arraycopy(result,0, bytes1,0, realBytes.length);
            String AES_decode = new String(bytes1, ENCODING);
            return AES_decode;
        } catch (Exception ex) {
            log.error("decryptCFB error:", ex);
        }
        return null;

    }





}

注意:Base64解密的方式由getDecoder()替换成getMimeDecoder()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中支持AES加密模式的有五种,分别是ECB、CBC、CFB、OFB和CTR。下面我将分别介绍这五种加密模式的实现方式: 1. ECB模式(电子密码本模式) ECB模式是最基本的加密模式,它将明文分成若干块,每一块分别进行加密加密后的密文组合起来形成最终的密文。在使用ECB模式时,如果明文中有相同的数据块,那么加密后的密文也是相同的,因此ECB模式并不安全,一般不建议使用Java实现代码如下: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class AESECBUtil { public static byte[] AESEncrypt(byte[] content, byte[] keyBytes) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(content); return result; } public static byte[] AESDecrypt(byte[] content, byte[] keyBytes) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] result = cipher.doFinal(content); return result; } } ``` 2. CBC模式(加密块链模式) CBC模式是一种分组密码加密模式,它需要一个初始化向量(IV)来进行加密。在加密时,明文首先与IV进行异或操作,然后再进行加密加密后的密文再与下一个明文块进行异或操作再进行加密,以此类推。在使用CBC模式时,同样的明文会因为不同的IV而加密成不同的密文,因此相对于ECB模式来说,CBC模式更加安全。 Java实现代码如下: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESCBCUtil { public static byte[] AESEncrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } public static byte[] AESDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } } ``` 3. CFB模式(加密反馈模式) CFB模式是一种分组密码加密模式,它采用了反馈机制,将上一次加密的结果作为下一次加密的输入,以此来增强加密的安全性。在使用CFB模式时,同样的明文会因为不同的初始向量而加密成不同的密文,因此相对于ECB模式来说,CFB模式更加安全。 Java实现代码如下: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESCFBUtil { public static byte[] AESEncrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } public static byte[] AESDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } } ``` 4. OFB模式(输出反馈模式) OFB模式也是一种分组密码加密模式,它采用了输出反馈机制,将上一次加密的结果作为下一次加密的输入。在使用OFB模式时,同样的明文会因为不同的初始向量而加密成不同的密文,因此相对于ECB模式来说,OFB模式更加安全。 Java实现代码如下: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESOFBUtil { public static byte[] AESEncrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/OFB/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } public static byte[] AESDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/OFB/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } } ``` 5. CTR模式(计数器模式) CTR模式也是一种分组密码加密模式,它将明文分成若干块,每一块分别进行加密。在加密时,先生成一个计数器,将计数器与密钥进行加密得到密文,再将密文与明文进行异或操作得到最终的密文。在使用CTR模式时,同样的明文会因为不同的计数器而加密成不同的密文,因此相对于ECB模式来说,CTR模式更加安全。 Java实现代码如下: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESCTRUtil { public static byte[] AESEncrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } public static byte[] AESDecrypt(byte[] content, byte[] keyBytes, byte[] iv) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] result = cipher.doFinal(content); return result; } } ``` 以上就是Java实现AES加密的五种模式,希望能对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值