2009年10月9日 星期五

使用Java實作TripleDES之加密、解密功能!

package com.nelson.util;
import java.security.Key;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
/**
* 此類別提供TripleDES之加解密功能.
*
* 文件建立日期:2009/10/08
* @author Nelson Chen
*/
public class DESedeEncryption {
private static String algorithm = "DESede";
private static Key key = null;
private static Cipher cipher = null;
private static SecretKeyFactory keyFactory = null;
private static DESedeKeySpec keySpec = null;
/**
* 此method切始化加解密之共用鑰匙,其中產生共用之鑰匙規範可以自己決定。 *
* @param chartCode 文字編碼:UTF-8,UTF-16
* @param strKeySpec 欲產生共用鑰匙之規範,其長度需大約24。
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
*/
private static void setUp(String chartCode,String strKeySpec)
throws NoSuchAlgorithmException,
UnsupportedEncodingException,
InvalidKeyException,
InvalidKeySpecException,
NoSuchPaddingException{
keyFactory = SecretKeyFactory.getInstance(algorithm);
if(strKeySpec==null || strKeySpec.equals("")){
//若沒有提供鑰匙規範,則改以下面之值取代!
strKeySpec = "Your secret Key phrase!!";
}
keySpec = new DESedeKeySpec(strKeySpec.getBytes(chartCode));
key = keyFactory.generateSecret(keySpec);
cipher = Cipher.getInstance(algorithm);
}
/**
* 此method之功能為加密
* @param input 欲加密之文字
* @param chartCode 文字編碼:UTF-8,UTF-16
* @param strKeySpec 欲產生共用鑰匙之規範,其長度需大約24。
* @return 回傳加密後之文字
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
* @throws NoSuchPaddingException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
*/
public static String encrypt(String input,String chartCode,String strKeySpec)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,
UnsupportedEncodingException,
NoSuchPaddingException,
InvalidKeySpecException,
NoSuchAlgorithmException{
DESedeEncryption.setUp(chartCode,strKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bAInput = input.getBytes(chartCode);
return (new BASE64Encoder()).encode(cipher.doFinal(bAInput));
}
/**
* 此method之功能解密
* @param encryptString 加密後之文字
* @param chartCode 文字編碼:UTF-8,UTF-16
* @param strKeySpec 欲產生共用鑰匙之規範,其長度需大約24。
* @return 回傳解密後之文字
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
* @throws IOException
* @throws NoSuchPaddingException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
*/
public static String decrypt(String encryptString,String chartCode,String strKeySpec)
throws InvalidKeyException,
BadPaddingException,
IllegalBlockSizeException,
UnsupportedEncodingException,
IOException,
NoSuchPaddingException,
InvalidKeySpecException,
NoSuchAlgorithmException{
DESedeEncryption.setUp(chartCode,strKeySpec);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bPlanText = cipher.doFinal((new BASE64Decoder()).decodeBuffer(encryptString));
return new String(bPlanText,chartCode);
}
/**
* 驗證加密、解密功能是否正確!!
*/
public static void main(String[] args){
String input = "Nelson111111";
String chartCode = "UTF-8";
String strKeySpec = null;
String strKeySpec1 = "222222222222222222222222";
try{
String strEncrption = encrypt(input,chartCode,strKeySpec);
String strDecrption = decrypt(strEncrption,chartCode,strKeySpec);
System.out.println("Input:"+input+",Encryption:"+strEncrption);
System.out.println("Input:"+input+",Decryption:"+strDecrption);
strEncrption = encrypt(input,chartCode,strKeySpec1);
strDecrption = decrypt(strEncrption,chartCode,strKeySpec1);
System.out.println("Input:"+input+",Encryption:"+strEncrption);
System.out.println("Input:"+input+",Decryption:"+strDecrption);
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}

沒有留言:

張貼留言