顯示具有 Encryption 標籤的文章。 顯示所有文章
顯示具有 Encryption 標籤的文章。 顯示所有文章

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());
}
}
}

2009年10月8日 星期四

使用Java實作單向加密(MD5,SHA-1)功能

package com.nelson.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
/**
* 此類別提供文字加密功能其加密之演算法可為MD5,SHA-1,SHA-256,SHA-384,SHA-512等.
*
* 文件建立日期:2009/10/08
* @author Nelson Chen
*
*/
public class Encryption {
public Encryption(){
}
/**
* 此method提供加密之功能.
* @param strPlanText 欲加密的文字.
* @param strAlgorithm 欲使用加密的演算法:MD5,SHA-1,SHA-256,SHA-384,SHA-512.
* @param strEncoding 文字之編碼:UTF-8,UTF-16,
* @return 回傳一串加密後的文字.
*/
public String Encrypt(String strPlanText,String strAlgorithm,String strEncoding){
String strEncrypt = null;
MessageDigest md = null;
try{
if(strEncoding==null || strEncoding.equals("")){
strEncoding = "UTF-8";
}
if(strAlgorithm==null || strAlgorithm.equals("")){
strAlgorithm = "MD5";
}
byte[] bstr = strPlanText.getBytes(strEncoding);
md = MessageDigest.getInstance(strAlgorithm);
md.update(bstr);
strEncrypt = (new BASE64Encoder()).encode(md.digest());
}catch(UnsupportedEncodingException ue){
ue.printStackTrace();
System.out.println(ue.getMessage());
return null;
}catch(NoSuchAlgorithmException na){
na.printStackTrace();
System.out.println(na.getMessage());
return null;
}
return strEncrypt;
}
/**
* 驗證加密功能是否正確!!
*/
public static void main(String[] args){
String chartCode = "UTF-8";
String chartCode1 = "UTF-16";
String inputStr = "Nelson";
Encryption enc = new Encryption();
System.out.println("input String:"+inputStr+",Encryption(SHA-1):"+enc.Encrypt(inputStr, "SHA-1",chartCode));
System.out.println("input String:"+inputStr+",Encryption(SHA-256):"+enc.Encrypt(inputStr, "SHA-256",chartCode));
System.out.println("input String:"+inputStr+",Encryption(SHA-384):"+enc.Encrypt(inputStr, "SHA-384",chartCode));
System.out.println("input String:"+inputStr+",Encryption(MD5):"+enc.Encrypt(inputStr, "MD5",chartCode));
System.out.println("input String:"+inputStr+",Encryption(MD5):"+enc.Encrypt(inputStr, "MD5",chartCode1));
}
}