[Java] java加解密工具类,支持RSA,AES →→→→→进入此内容的聊天室

来自 , 2019-04-08, 写在 Java, 查看 131 次.
URL http://www.code666.cn/view/8b5040a8
  1. package security;
  2.  
  3. import java.security.Key;
  4. import java.security.KeyPair;
  5. import java.security.KeyPairGenerator;
  6. import java.security.spec.KeySpec;
  7.  
  8. import javax.crypto.Cipher;
  9. import javax.crypto.KeyGenerator;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.SecretKeyFactory;
  12. import javax.crypto.spec.DESKeySpec;
  13. import javax.crypto.spec.DESedeKeySpec;
  14. import javax.crypto.spec.SecretKeySpec;
  15.  
  16.  
  17. /**
  18.  * 加解密统一接口,支持流行的对称和非对称算法
  19.  * 目前可以使用3DES,AES,RSA进行加解密
  20.  * @author luis.chen
  21.  * @version $Id: EncryptUtil.java, v 0.1 2014年6月17日 上午11:08:28 luis.chen Exp $
  22.  */
  23. public class EncryptUtil {
  24.  
  25.     public static void main(String args[]){
  26.         String plainText = "我是一只小小鸟";      
  27.         try {
  28.            
  29.             System.out.println("开始使用AES加密....");
  30.             //使用AES加密
  31.             byte[] asKey = getDefaultKey(EncryptAlgorithm.AES);
  32.             String encStr = testSymmEncry(plainText,asKey,EncryptAlgorithm.AES);
  33.             System.out.println("AES加密之后:"+encStr);
  34.             //使用AES解密
  35.             String decStr = testSymmDecry(encStr,asKey,EncryptAlgorithm.AES);
  36.             System.out.println("AES解密之后:"+decStr);
  37.            
  38.             System.out.println("开始使用RSA加密....");
  39.            
  40.             KeyPair kp = getDefaultKeyPair(EncryptAlgorithm.RSA);
  41.             String rsaEncStr = testAsymmEncry(plainText,kp.getPublic(),EncryptAlgorithm.RSAWithPadding);
  42.             System.out.println("RSA加密之后:"+rsaEncStr);
  43.             //使用RSA解密
  44.             String desDecStr = testAsymmDecry(rsaEncStr,kp.getPrivate(),EncryptAlgorithm.RSAWithPadding);
  45.             System.out.println("RSA解密之后:"+desDecStr);
  46.            
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         }
  50.        
  51.     }
  52.    
  53.     public static String testSymmEncry(String plainText,byte[] key,EncryptAlgorithm alg) throws Exception{
  54.         /*测试对称加密方法的应用场景类*/
  55.         byte[] encResult = encryt(EncryptStringUtils.getEncByteFromStr(plainText),key,alg);
  56.         String encStr = EncryptStringUtils.byte2hex(encResult);
  57.         return encStr;
  58.     }
  59.     public static String testAsymmEncry(String plainText,Key key,EncryptAlgorithm alg) throws Exception{
  60.         /*测试非对称加密方法的应用场景类*/
  61. //        byte[] encResult = encryt(EncryptStringUtils.getEncByteFromStr(plainText),key,alg);
  62.         byte[] encResult = encryt(plainText.getBytes(),key,alg);
  63.         String encStr = EncryptStringUtils.byte2hex(encResult);
  64.         return encStr;
  65.     }
  66.    
  67.    
  68.     public static String testSymmDecry(String ciperText, byte[] key,EncryptAlgorithm alg) throws Exception{
  69.         /*测试解密方法的应用场景类*/
  70.         byte[] decResult = decryt(EncryptStringUtils.getDecByteFromStr(ciperText),key,alg);
  71.         String decStr = new String(decResult);
  72.         return decStr;
  73.     }
  74.    
  75.     public static String testAsymmDecry(String ciperText, Key key,EncryptAlgorithm alg) throws Exception{
  76.         /*测试非对称解密方法的应用场景类*/
  77.         byte[] decResult = decryt(EncryptStringUtils.getDecByteFromStr(ciperText),key,alg);
  78.         String decStr = new String(decResult);
  79.         return decStr;
  80.     }
  81.    
  82.     /**
  83.      * 对称加密方法
  84.      * @param plainText 明文的16进制字节数组
  85.      * @param encrytKey 16进制的密钥数组
  86.      * @param alg 加密算法的枚举
  87.      * @return 加密结果,返回加密后的字节数组
  88.      * @throws Exception
  89.      * */
  90.     public static byte[] encryt(byte[] plainText, byte[] encrytKey,EncryptAlgorithm alg) throws Exception{
  91.         Key k = toKey(encrytKey,alg);        
  92.         return encryt(plainText,k,alg);
  93.     }
  94.    
  95.     /**
  96.      * 非对称加密方法
  97.      * @param plainText 明文的16进制字节数组
  98.      * @param key 通过KeyPair获得的公钥
  99.      * @param alg 加密算法的枚举
  100.      * @return 加密结果,返回加密后的字节数组
  101.      * @throws Exception
  102.      * */
  103.     public static byte[] encryt(byte[] plainText, Key key,EncryptAlgorithm alg) throws Exception{
  104.         Cipher cipher = Cipher.getInstance(alg.getAlgorithm());
  105.         cipher.init(Cipher.ENCRYPT_MODE, key);
  106.         return cipher.doFinal(plainText);
  107.     }
  108.     /**
  109.      * 对称加密解密方法
  110.      * @param ciperText 密文的16进制字节数组
  111.      * @param decrytKey 16进制的密钥数组
  112.      * @param alg 加密算法的枚举
  113.      * @return 解密结果,返回解密后的字节数组
  114.      * @throws Exception
  115.      * */
  116.     public static byte[] decryt(byte[] ciperText, byte[] decrytKey,EncryptAlgorithm alg) throws Exception{
  117.         Key k = toKey(decrytKey,alg);
  118.         return decryt(ciperText,k,alg);
  119.     }
  120.     /**
  121.      * 非对称加密解密方法
  122.      * @param ciperText 密文的16进制字节数组
  123.      * @param key 通过keypair得到的非对称加密私钥
  124.      * @param alg 加密算法的枚举
  125.      * @return 解密结果,返回解密后的字节数组
  126.      * @throws Exception
  127.      * */
  128.     public static byte[] decryt(byte[] ciperText, Key key,EncryptAlgorithm alg) throws Exception{
  129.         Cipher cipher = Cipher.getInstance(alg.getAlgorithm());
  130.         cipher.init(Cipher.DECRYPT_MODE, key);
  131.         return cipher.doFinal(ciperText);
  132.     }
  133.    
  134.     /**
  135.      * 获取对称加密算法算法的密钥
  136.      * @param alg 加密算法枚举
  137.      * @return 16进制的密钥数组
  138.      * @throws
  139.      * */
  140.     public static byte[] getDefaultKey(EncryptAlgorithm alg) throws Exception{
  141.         KeyGenerator keygen = KeyGenerator.getInstance(alg.getAlgorithm());        
  142.         SecretKey deskey = keygen.generateKey();        
  143.         return deskey.getEncoded();
  144.     }
  145.     /**
  146.      * 获取非对称加密算法的密钥
  147.      * @param alg 加密算法枚举
  148.      * @return 16进制的密钥数组
  149.      * @throws
  150.      * */
  151.     public static KeyPair getDefaultKeyPair(EncryptAlgorithm alg) throws Exception{
  152.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(alg.getAlgorithm());
  153.         //密钥位数
  154.         keyPairGen.initialize(1024);
  155.         //密钥对
  156.         KeyPair keyPair = keyPairGen.generateKeyPair();
  157.         return keyPair;
  158.     }
  159.     /**
  160.      * 通过key的字节数组和特定的算法得到用于加解密的密钥对象
  161.      * @param key 密钥数组
  162.      * @param alg 加解密算法的枚举
  163.      * @return KEY
  164.      * @throws Exception
  165.      */
  166.     private static Key toKey(byte[] key, EncryptAlgorithm alg) throws Exception {
  167.         SecretKeySpec spec = new SecretKeySpec(key,alg.getAlgorithm());
  168.         if(alg.getAlgorithm().indexOf("DES") > -1 ){
  169.             KeySpec desKey = null;
  170.             SecretKeyFactory keyFactory = null;
  171.             if("DES".equals(alg.getAlgorithm())){
  172.                 desKey = new DESKeySpec(key);
  173.                 keyFactory = SecretKeyFactory.getInstance(alg.getAlgorithm());
  174.             }
  175.             else{
  176.                 desKey = new DESedeKeySpec(key);
  177.                 keyFactory = SecretKeyFactory.getInstance(EncryptAlgorithm.ThreeDES.getAlgorithm());  
  178.             }// 将DESKeySpec对象转换成SecretKey对象  
  179.             SecretKey securekey = keyFactory.generateSecret(desKey);  
  180.             return securekey;
  181.         }
  182.        
  183.         return spec;
  184.     }
  185.    
  186.    
  187. }
  188.  

回复 "java加解密工具类,支持RSA,AES"

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

captcha