AES加密实例

  1. public class AES  
  2.       
  3.     private static final String AES "AES" 
  4.   
  5. 密钥长度要是16位的
  6.     private static final String CRYPT_KEY "YUUAtestYUUAtest" 
  7.   
  8.       
  9.     public static byte[] encrypt(byte[] src, String key) throws Exception  
  10.         Cipher cipher Cipher.getInstance(AES);  
  11.         SecretKeySpec securekey new SecretKeySpec(key.getBytes(), AES);  
  12.         cipher.init(Cipher.ENCRYPT_MODE, securekey);//设置密钥和加密形式  
  13.         return cipher.doFinal(src);  
  14.      
  15.   
  16.       
  17.     public static byte[] decrypt(byte[] src, String key)  throws Exception   
  18.         Cipher cipher Cipher.getInstance(AES);  
  19.         SecretKeySpec securekey new SecretKeySpec(key.getBytes(), AES);//设置加密Key  
  20.         cipher.init(Cipher.DECRYPT_MODE, securekey);//设置密钥和解密形式  
  21.         return cipher.doFinal(src);  
  22.      
  23.       
  24.       
  25.     public static String byte2hex(byte[] b)  
  26.         String hs "" 
  27.         String stmp "" 
  28.         for (int 0b.length; n++)  
  29.             stmp (java.lang.Integer.toHexString(b[n] 0XFF));  
  30.             if (stmp.length() == 1 
  31.                 hs hs "0" stmp;  
  32.             else  
  33.                 hs hs stmp;  
  34.          
  35.         return hs.toUpperCase();  
  36.      
  37.   
  38.     public static byte[] hex2byte(byte[] b)  
  39.         if ((b.length 2!= 0 
  40.             throw new IllegalArgumentException("长度不是偶数");  
  41.         byte[] b2 new byte[b.length 2];  
  42.         for (int 0b.length; += 2 
  43.             String item new String(b, n, 2);  
  44.             b2[n 2(byteInteger.parseInt(item, 16);  
  45.          
  46.         return b2;  
  47.      
  48.       
  49.       
  50.     public final static String decrypt(String data)  
  51.         try  
  52.             return new String(decrypt(hex2byte(data.getBytes()),  
  53.                     CRYPT_KEY));  
  54.         catch (Exception e)  
  55.          
  56.         return null 
  57.      
  58.   
  59.       
  60.     public final static String encrypt(String data)  
  61.         try  
  62.             return byte2hex(encrypt(data.getBytes(), CRYPT_KEY));  
  63.         catch (Exception e)  
  64.          
  65.         return null 
  66.      
  67.       
  68.   
  69.     public static void main(String[] args)  
  70.         String ID "611111" 
  71.           
  72.         String idEncrypt encrypt(ID);  
  73.         System.out.println(idEncrypt);  
  74.         String idDecrypt decrypt(idEncrypt);  
  75.         System.out.println(idDecrypt);  
  76.      
  77.       
  78. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值