[Java] AES加密解密 →→→→→进入此内容的聊天室

来自 , 2020-08-18, 写在 Java, 查看 170 次.
URL http://www.code666.cn/view/c1fea270
  1. package com.dc;
  2.  
  3. import java.security.InvalidKeyException;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.Security;
  6.  
  7. import javax.crypto.BadPaddingException;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.IllegalBlockSizeException;
  10. import javax.crypto.KeyGenerator;
  11. import javax.crypto.NoSuchPaddingException;
  12. import javax.crypto.SecretKey;
  13.  
  14. /* ******************  类说明  *********************
  15.  * class       :  DcAESUtil
  16.  * @author     :  ncc
  17.  * create time :  2017-12-19 上午10:13:17
  18.  * @version    :  1.0  
  19.  * description :  AES密码学中的高级加密标准(Advanced Encryption Standard,AES),又称  高级加密标准
  20.  * Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,
  21.  * 已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与
  22.  * 技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。
  23.  * 2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。
  24.  * 该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,
  25.  * 以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhinedoll"。)
  26.  * @see        :                        
  27.  * ************************************************/  
  28. public class DcAESUtil {
  29.  
  30.         // KeyGenerator 提供对称密钥生成器的功能,支持各种算法
  31.         private KeyGenerator keygen;
  32.         // SecretKey 负责保存对称密钥
  33.         private SecretKey deskey;
  34.         // Cipher负责完成加密或解密工作
  35.         private Cipher c;
  36.         // 该字节数组负责保存加密的结果
  37.         private byte[] cipherByte;
  38.  
  39.         public DcAESUtil() throws NoSuchAlgorithmException, NoSuchPaddingException {
  40.                 Security.addProvider(new com.sun.crypto.provider.SunJCE());
  41.                 // 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
  42.                 keygen = KeyGenerator.getInstance("AES");
  43.                 // 生成密钥
  44.                 deskey = keygen.generateKey();
  45.                 // 生成Cipher对象,指定其支持的DES算法
  46.                 c = Cipher.getInstance("AES");
  47.         }
  48.  
  49.         /* ********************************************
  50.          * method name   : Encrytor
  51.          * description   : 对字符串加密
  52.          * @return       : byte[]
  53.          * @param        : @param str
  54.          * @param        : @return
  55.          * @param        : @throws InvalidKeyException
  56.          * @param        : @throws IllegalBlockSizeException
  57.          * @param        : @throws BadPaddingException
  58.          * modified      : ncc ,  2017-12-19
  59.          * @see          :
  60.          * ********************************************/      
  61.         public byte[] Encrytor(String str) throws InvalidKeyException,
  62.                         IllegalBlockSizeException, BadPaddingException {
  63.                 // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
  64.                 c.init(Cipher.ENCRYPT_MODE, deskey);
  65.                 byte[] src = str.getBytes();
  66.                 // 加密,结果保存进cipherByte
  67.                 cipherByte = c.doFinal(src);
  68.                 return cipherByte;
  69.         }
  70.  
  71.         /* ********************************************
  72.          * method name   : Decryptor
  73.          * description   : 对字符串解密
  74.          * @return       : byte[]
  75.          * @param        : @param buff
  76.          * @param        : @return
  77.          * @param        : @throws InvalidKeyException
  78.          * @param        : @throws IllegalBlockSizeException
  79.          * @param        : @throws BadPaddingException
  80.          * modified      : ncc ,  2017-12-19
  81.          * @see          :
  82.          * ********************************************/      
  83.         public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
  84.                         IllegalBlockSizeException, BadPaddingException {
  85.                 // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
  86.                 c.init(Cipher.DECRYPT_MODE, deskey);
  87.                 cipherByte = c.doFinal(buff);
  88.                 return cipherByte;
  89.         }
  90.  
  91.         /**
  92.          * @param args
  93.          * @throws NoSuchPaddingException
  94.          * @throws NoSuchAlgorithmException
  95.          * @throws BadPaddingException
  96.          * @throws IllegalBlockSizeException
  97.          * @throws InvalidKeyException
  98.          */
  99.         public static void main(String[] args) throws Exception {
  100.                 DcAESUtil de = new DcAESUtil();
  101.                 String msg = "欢迎光临得草之家!";
  102.                 byte[] encontent = de.Encrytor(msg);
  103.                 byte[] decontent = de.Decryptor(encontent);
  104.                 System.out.println("明文是:" + msg);
  105.                 System.out.println("加密后:" + new String(encontent));
  106.                 System.out.println("解密后:" + new String(decontent));
  107.         }
  108. }
  109.  

回复 "AES加密解密"

这儿你可以回复上面这条便签

captcha