java加解密工具類,支持RSA,AES

ew3y 9年前發布 | 3K 次閱讀 Java

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.KeySpec;

import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.SecretKeySpec;

/**

  • 加解密統一接口,支持流行的對稱和非對稱算法
  • 目前可以使用3DES,AES,RSA進行加解密
  • @author luis.chen
  • @version $Id: EncryptUtil.java, v 0.1 2014年6月17日 上午11:08:28 luis.chen Exp $ */ public class EncryptUtil {

    public static void main(String args[]){

     String plainText = "我是一只小小鳥";      
     try {
    
         System.out.println("開始使用AES加密....");
         //使用AES加密
         byte[] asKey = getDefaultKey(EncryptAlgorithm.AES);
         String encStr = testSymmEncry(plainText,asKey,EncryptAlgorithm.AES);
         System.out.println("AES加密之后:"+encStr);
         //使用AES解密
         String decStr = testSymmDecry(encStr,asKey,EncryptAlgorithm.AES);
         System.out.println("AES解密之后:"+decStr);
    
         System.out.println("開始使用RSA加密....");
    
         KeyPair kp = getDefaultKeyPair(EncryptAlgorithm.RSA);
         String rsaEncStr = testAsymmEncry(plainText,kp.getPublic(),EncryptAlgorithm.RSAWithPadding);
         System.out.println("RSA加密之后:"+rsaEncStr);
         //使用RSA解密
         String desDecStr = testAsymmDecry(rsaEncStr,kp.getPrivate(),EncryptAlgorithm.RSAWithPadding);
         System.out.println("RSA解密之后:"+desDecStr);
    
     } catch (Exception e) {
         e.printStackTrace();
     }
    
    

    }

    public static String testSymmEncry(String plainText,byte[] key,EncryptAlgorithm alg) throws Exception{

     /*測試對稱加密方法的應用場景類*/
     byte[] encResult = encryt(EncryptStringUtils.getEncByteFromStr(plainText),key,alg);
     String encStr = EncryptStringUtils.byte2hex(encResult);
     return encStr;
    

    } public static String testAsymmEncry(String plainText,Key key,EncryptAlgorithm alg) throws Exception{

     /*測試非對稱加密方法的應用場景類*/
    

    // byte[] encResult = encryt(EncryptStringUtils.getEncByteFromStr(plainText),key,alg);

     byte[] encResult = encryt(plainText.getBytes(),key,alg);
     String encStr = EncryptStringUtils.byte2hex(encResult);
     return encStr;
    

    }

public static String testSymmDecry(String ciperText, byte[] key,EncryptAlgorithm alg) throws Exception{
    /*測試解密方法的應用場景類*/
    byte[] decResult = decryt(EncryptStringUtils.getDecByteFromStr(ciperText),key,alg);
    String decStr = new String(decResult);
    return decStr;
}

public static String testAsymmDecry(String ciperText, Key key,EncryptAlgorithm alg) throws Exception{
    /*測試非對稱解密方法的應用場景類*/
    byte[] decResult = decryt(EncryptStringUtils.getDecByteFromStr(ciperText),key,alg);
    String decStr = new String(decResult);
    return decStr;
}

/**
 * 對稱加密方法
 * @param plainText 明文的16進制字節數組
 * @param encrytKey 16進制的密鑰數組
 * @param alg 加密算法的枚舉
 * @return 加密結果,返回加密后的字節數組
 * @throws Exception
 * */
public static byte[] encryt(byte[] plainText, byte[] encrytKey,EncryptAlgorithm alg) throws Exception{
    Key k = toKey(encrytKey,alg);       
    return encryt(plainText,k,alg);
}

/**
 * 非對稱加密方法
 * @param plainText 明文的16進制字節數組
 * @param key 通過KeyPair獲得的公鑰
 * @param alg 加密算法的枚舉
 * @return 加密結果,返回加密后的字節數組
 * @throws Exception
 * */
public static byte[] encryt(byte[] plainText, Key key,EncryptAlgorithm alg) throws Exception{
    Cipher cipher = Cipher.getInstance(alg.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(plainText);
}
/**
 * 對稱加密解密方法
 * @param ciperText 密文的16進制字節數組
 * @param decrytKey 16進制的密鑰數組
 * @param alg 加密算法的枚舉
 * @return 解密結果,返回解密后的字節數組
 * @throws Exception
 * */
public static byte[] decryt(byte[] ciperText, byte[] decrytKey,EncryptAlgorithm alg) throws Exception{
    Key k = toKey(decrytKey,alg);
    return decryt(ciperText,k,alg);
}
/**
 * 非對稱加密解密方法
 * @param ciperText 密文的16進制字節數組
 * @param key 通過keypair得到的非對稱加密私鑰
 * @param alg 加密算法的枚舉
 * @return 解密結果,返回解密后的字節數組
 * @throws Exception
 * */
public static byte[] decryt(byte[] ciperText, Key key,EncryptAlgorithm alg) throws Exception{
    Cipher cipher = Cipher.getInstance(alg.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, key);
    return cipher.doFinal(ciperText);
}

/**
 * 獲取對稱加密算法算法的密鑰
 * @param alg 加密算法枚舉
 * @return 16進制的密鑰數組
 * @throws
 * */
public static byte[] getDefaultKey(EncryptAlgorithm alg) throws Exception{
    KeyGenerator keygen = KeyGenerator.getInstance(alg.getAlgorithm());       
    SecretKey deskey = keygen.generateKey();       
    return deskey.getEncoded();
}
/**
 * 獲取非對稱加密算法的密鑰
 * @param alg 加密算法枚舉
 * @return 16進制的密鑰數組
 * @throws
 * */
public static KeyPair getDefaultKeyPair(EncryptAlgorithm alg) throws Exception{
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(alg.getAlgorithm());
    //密鑰位數
    keyPairGen.initialize(1024);
    //密鑰對
    KeyPair keyPair = keyPairGen.generateKeyPair();
    return keyPair;
}
/**
 * 通過key的字節數組和特定的算法得到用于加解密的密鑰對象
 * @param key 密鑰數組
 * @param alg 加解密算法的枚舉
 * @return KEY
 * @throws Exception
 */
private static Key toKey(byte[] key, EncryptAlgorithm alg) throws Exception {
    SecretKeySpec spec = new SecretKeySpec(key,alg.getAlgorithm());
    if(alg.getAlgorithm().indexOf("DES") > -1 ){
        KeySpec desKey = null;
        SecretKeyFactory keyFactory = null;
        if("DES".equals(alg.getAlgorithm())){
            desKey = new DESKeySpec(key);
            keyFactory = SecretKeyFactory.getInstance(alg.getAlgorithm());
        }
        else{
            desKey = new DESedeKeySpec(key);
            keyFactory = SecretKeyFactory.getInstance(EncryptAlgorithm.ThreeDES.getAlgorithm()); 
        }// 將DESKeySpec對象轉換成SecretKey對象 
        SecretKey securekey = keyFactory.generateSecret(desKey); 
        return securekey;
    }

    return spec;
}


}</pre>

 本文由用戶 ew3y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!