DBMNG数据库管理与应用

抓住自己最有兴趣的东西,由浅入深,循序渐进地学……
当前位置:首页 > 经验分享 > Java组件

JAVA实现AES加密

AES是美国联邦政府采用的商业及政府数据加密标准,预计将在未来几十年里代替DES在各个领域中得到广泛应用。AES提供128位密钥,因此,128位AES的加密强度是56位DES加密强度的1021倍还多。假设可以制造一部可以在1秒内破解DES密码的机器,那么使用这台机器破解一个128位AES密码需要大约149亿万年的时间。(更深一步比较而言,宇宙一般被认为存在了还不到200亿年)因此可以预计,美国国家标准局倡导的AES即将作为新标准取代DES。 

Java代码  收藏代码
  1. package com.jshx.utils;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.security.InvalidKeyException;  
  5. import java.security.NoSuchAlgorithmException;  
  6. import java.security.SecureRandom;  
  7.   
  8. import javax.crypto.BadPaddingException;  
  9. import javax.crypto.Cipher;  
  10. import javax.crypto.IllegalBlockSizeException;  
  11. import javax.crypto.KeyGenerator;  
  12. import javax.crypto.NoSuchPaddingException;  
  13. import javax.crypto.SecretKey;  
  14. import javax.crypto.spec.SecretKeySpec;  
  15.   
  16. import org.apache.axis.encoding.Base64;  
  17.   
  18. public class AES {  
  19.     /** 
  20.       * 加密 
  21.      *  
  22.       * @param content 需要加密的内容 
  23.      * @param password  加密密码 
  24.      * @return 
  25.       */   
  26.      public static byte[] encrypt(String content, String password) {   
  27.              try {              
  28.                      KeyGenerator kgen = KeyGenerator.getInstance("AES");   
  29.                      kgen.init(128new SecureRandom(password.getBytes()));   
  30.                      SecretKey secretKey = kgen.generateKey();   
  31.                      byte[] enCodeFormat = secretKey.getEncoded();   
  32.                      SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");   
  33.                      Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
  34.                      byte[] byteContent = content.getBytes("utf-8");   
  35.                      cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化   
  36.                      byte[] result = cipher.doFinal(byteContent);   
  37.                      return result; // 加密   
  38.              } catch (NoSuchAlgorithmException e) {   
  39.                      e.printStackTrace();   
  40.              } catch (NoSuchPaddingException e) {   
  41.                      e.printStackTrace();   
  42.              } catch (InvalidKeyException e) {   
  43.                      e.printStackTrace();   
  44.              } catch (UnsupportedEncodingException e) {   
  45.                      e.printStackTrace();   
  46.              } catch (IllegalBlockSizeException e) {   
  47.                      e.printStackTrace();   
  48.              } catch (BadPaddingException e) {   
  49.                      e.printStackTrace();   
  50.              }   
  51.              return null;   
  52.      }   
  53.        
  54.      /**解密 
  55.       * @param content  待解密内容 
  56.       * @param password 解密密钥 
  57.       * @return 
  58.        */   
  59.       public static byte[] decrypt(byte[] content, String password) {   
  60.               try {   
  61.                        KeyGenerator kgen = KeyGenerator.getInstance("AES");   
  62.                        kgen.init(128new SecureRandom(password.getBytes()));   
  63.                        SecretKey secretKey = kgen.generateKey();   
  64.                        byte[] enCodeFormat = secretKey.getEncoded();   
  65.                        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");               
  66.                        Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
  67.                       cipher.init(Cipher.DECRYPT_MODE, key);// 初始化   
  68.                       byte[] result = cipher.doFinal(content);   
  69.                       return result; // 加密   
  70.               } catch (NoSuchAlgorithmException e) {   
  71.                       e.printStackTrace();   
  72.               } catch (NoSuchPaddingException e) {   
  73.                       e.printStackTrace();   
  74.               } catch (InvalidKeyException e) {   
  75.                       e.printStackTrace();   
  76.               } catch (IllegalBlockSizeException e) {   
  77.                       e.printStackTrace();   
  78.               } catch (BadPaddingException e) {   
  79.                       e.printStackTrace();   
  80.               }   
  81.               return null;   
  82.       }  
  83.         
  84.         
  85.       /**将二进制转换成16进制 
  86.        * @param buf 
  87.         * @return 
  88.         */   
  89.        public static String parseByte2HexStr(byte buf[]) {   
  90.                StringBuffer sb = new StringBuffer();   
  91.                for (int i = 0; i < buf.length; i++) {   
  92.                        String hex = Integer.toHexString(buf[i] & 0xFF);   
  93.                        if (hex.length() == 1) {   
  94.                                hex = '0' + hex;   
  95.                        }   
  96.                        sb.append(hex.toUpperCase());   
  97.                }   
  98.                return sb.toString();   
  99.        }   
  100.          
  101.        /**将16进制转换为二进制 
  102.         * @param hexStr 
  103.          * @return 
  104.          */   
  105.         public static byte[] parseHexStr2Byte(String hexStr) {   
  106.                 if (hexStr.length() < 1)   
  107.                         return null;   
  108.                 byte[] result = new byte[hexStr.length()/2];   
  109.                 for (int i = 0;i< hexStr.length()/2; i++) {   
  110.                         int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);   
  111.                         int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);   
  112.                         result[i] = (byte) (high * 16 + low);   
  113.                 }   
  114.                 return result;   
  115.         }   
  116.           
  117.         /** 
  118.            * 加密 
  119.           * 
  120.            * @param content 需要加密的内容 
  121.           * @param password  加密密码 
  122.           * @return 
  123.            */   
  124.           public static byte[] encrypt2(String content, String password) {   
  125.                   try {   
  126.                           SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");   
  127.                           Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");   
  128.                           byte[] byteContent = content.getBytes("utf-8");   
  129.                           cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化   
  130.                           byte[] result = cipher.doFinal(byteContent);   
  131.                           return result; // 加密   
  132.                   } catch (NoSuchAlgorithmException e) {   
  133.                           e.printStackTrace();   
  134.                   } catch (NoSuchPaddingException e) {   
  135.                           e.printStackTrace();   
  136.                   } catch (InvalidKeyException e) {   
  137.                           e.printStackTrace();   
  138.                   } catch (UnsupportedEncodingException e) {   
  139.                           e.printStackTrace();   
  140.                   } catch (IllegalBlockSizeException e) {   
  141.                           e.printStackTrace();   
  142.                   } catch (BadPaddingException e) {   
  143.                           e.printStackTrace();   
  144.                   }   
  145.                   return null;   
  146.           }   
  147.         
  148.       public static void main(String[] args) {  
  149.           String content = "test";   
  150.           String password = "12345678";   
  151.           //加密   
  152.          System.out.println("加密前:" + content);   
  153.          byte[] encryptResult = encrypt(content, password);  
  154.          String tt4 = Base64.encode(encryptResult);  
  155.          System.out.println(new String(tt4));  
  156.            
  157.           //解密   
  158.          byte[] decryptResult = decrypt(encryptResult,password);   
  159.          System.out.println("解密后:" + new String(decryptResult));   
  160.     }  
  161. }  
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号