美文网首页
AES CFB模式加密解密

AES CFB模式加密解密

作者: 白色风车_1403 | 来源:发表于2023-04-22 18:23 被阅读0次

    package com.hebabr.xy.exam.util;

    import javax.crypto.*;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.UnsupportedEncodingException;
    import java.nio.charset.StandardCharsets;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.Base64;
    import java.util.Random;

    /**

    • AES 加密方法,是对称的密码算法(加密与解密的密钥一致),这里使用最大的 256 位的密钥
      /
      public class AESUtils3 {
      /
      *

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

      //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);
      byte[] iv = key.getBytes(StandardCharsets.UTF_8);
      IvParameterSpec ivSpec = new IvParameterSpec(iv);
      aesCFB.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
      byte[] contentBytes = content.getBytes();
      byte[] encryptBytes = aesCFB.doFinal(contentBytes);
      return Base64.getEncoder().encodeToString(encryptBytes);
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      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 = key.getBytes(StandardCharsets.UTF_8);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        aesCFB.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] result = aesCFB.doFinal(decodeBytes);

         String AES_decode = new String(result, ENCODING);
         return AES_decode;
        

        } catch (Exception ex) {
        ex.printStackTrace();
        }
        return null;

      }

      public static void main(String[] args) throws Exception {
      String content = "{"token":"92b1f641-9311-4c46-984e-9d9debb2bc60","userId":1,"datetime":1682241938118}";
      System.out.println("需要加密的内容:" + content);

       String hexStr = encryptCFB(content, password);
       System.out.println("加密后:"+ hexStr);
      
       String decrypt = decryptCFB(hexStr, password);
       System.out.println("解密后的内容:" + decrypt);
      

      }

    }

    相关文章

      网友评论

          本文标题:AES CFB模式加密解密

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