?? blowfishtool.java
字號:
package jct;import javax.crypto.*;import javax.crypto.spec.*;import java.security.Key;import java.security.KeyFactory;import java.security.spec.EncodedKeySpec;import java.security.Security;import java.security.*;import java.util.Random;import java.util.Vector;/** * <p>封裝同Blowfish對稱加密算法有關的方法,包括了使用創建Blowfish密碼,使用Blowfish加密、解密, 使用PBE(基于口令的加密)存取blowfiwsh密碼 </p> * @Copyright:WDSsoft * @ad:WDSsoft “企業多級數字簽名系統”- 最佳的企業電子文檔多級數字簽名方案 * @URL:www.wdssoft.com * @作者 吳東升 mdss@wdssoft.com bluesunday@sohu.com */public class BlowfishTool { public BlowfishTool() { }/** * 使用Blowfish加密 * @param Key key:密碼 * @param byte[] text:明文 * @return byte[]:密文 */ public static byte[] encryptReturnByte(Key key,byte[] text){ try{ Cipher cipher=Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,key); return cipher.doFinal(text); }catch(Exception e){ return null; } }/** * Blowfish解密 * @param Key key:密碼 * @param byte[] text:密文 * @return byte[] : 明文 */ public static byte[] decryptReturnByte(Key key,byte[] text){ try{ Cipher cipher=Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE,key); return cipher.doFinal(text); }catch(Exception e){ return null;} }/** * 使用PBE保管Blowfish密碼 * @param Key targetKey: Blowfish密碼 * @param char[] password: 口令 * @return Vector-element1:byte[]:密文-element2:byte[]:鹽 */ public static Vector wrapKey(Key targetKey,char[] password){ try{ PBEKeySpec keySpec=new PBEKeySpec(password); SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key=keyFactory.generateSecret(keySpec); Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES"); byte[] salt=new byte[8]; Random random=new Random(); random.nextBytes(salt); PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100); cipher.init(cipher.WRAP_MODE,key,paramSpec); Vector encryptedKey=new Vector(); encryptedKey.addElement(cipher.wrap(targetKey)); encryptedKey.addElement(salt); return encryptedKey; }catch(Exception e){e.printStackTrace(); return null; } }/** * 取出PBE加密的密鑰 * @param Vector encryptedKey-element1:byte[]:密文-element2:byte[]:鹽 * @param char[] password:口令 * @return Key:Blowfish密碼 */ public static Key unwrapKey(Vector encryptedKey,char[] password){ try{ byte[] wrapedKey=(byte[])encryptedKey.elementAt(0); byte[] salt=(byte[])encryptedKey.elementAt(1); PBEKeySpec keySpec=new PBEKeySpec(password); SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key=keyFactory.generateSecret(keySpec); Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES"); PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100); cipher.init(cipher.UNWRAP_MODE,key,paramSpec); return cipher.unwrap(wrapedKey,"Blowfish",cipher.SECRET_KEY); }catch(Exception e){ return null; } } /** * 產生一個Blowfish密碼 * @return Key Blowfish 密碼 */ public static Key creatKey(){ try{ KeyGenerator keyGenerator=KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128); Key key=keyGenerator.generateKey(); return key; }catch(Exception e){return null;} }/** * 將Blowfish密碼Key 對象轉為byte[] 形式 * @param Key Blowfish密碼Key 對象 * @return byte[]形式Blowfish密碼 */ public static byte[] encodeKey(Key key){ return key.getEncoded(); }/*** 將byte[] 形式Blowfish密碼轉為Key 對象* @param byte[] 形式Blowfish密碼* @return Key Blowfish密碼Key 對象 */ public static Key decodeKey(byte[] keyByteCode){ SecretKeySpec myKey=new SecretKeySpec(keyByteCode,"Blowfish"); return myKey; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -