BlowFish加密算法在加密速度上就超越了DES加密算法,引起了人們的關注。BlowFish加密算法沒有注冊專利,不需要授權,可以免費使用。但是知名度不如AES,BlowFish加密算法是一種對稱的分組加密算法,每次加密一個64位分組,使用32位~448位的可變長度密鑰,應用於內部加密。
BlowFish算法用來加密64Bit長度的字符串。
BlowFish算法使用兩個“盒”——ungignedlongpbox[18]和unsignedlongsbox[4,256]。
BlowFish算法中,有一個核心加密函數:BF_En(后文詳細介紹)。該函數輸入64位信息,運算后,以64位密文的形式輸出。用BlowFish算法加密信息,需要兩個過程:密鑰預處理和信息加密。
代碼示例如下:
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class BlowFish
{
public BlowFish()
{
}
public static String byteToString(byte bytes[])
{
StringBuffer buf = new StringBuffer();
for(int i = 0; i < bytes.length; i++)
{
int d = bytes[i];
if(d < 0)
d += 256;
if(d < 16)
buf.append("0");
buf.append(Integer.toString(d, 16));
}
return buf.toString();
}
public static byte[] stringToByte(String string)
{
byte bytes[] = new byte[string.length() / 2];
for(int i = 0; i < string.length() / 2; i++)
{
String b = string.substring(i * 2, i * 2 + 2);
bytes[i] = (byte)Integer.parseInt(b, 16);
}
return bytes;
}
public static byte[] encrypt(String key, byte text[])
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(1, sksSpec);
byte encrypted[] = cipher.doFinal(text);
return encrypted;
}
public static byte[] decrypt(String key, byte encrypted[])
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(2, sksSpec);
byte decrypted[] = cipher.doFinal(encrypted);
return decrypted;
}
public static String base64Decoder(String base64String)
{
String str = new String(Base64.encode(base64String.getBytes()));
return str;
}
public static String base64Eecoder(byte img[])
{
String str = new String(Base64.encode(img));
return str;
}
public static String hexToString(String str)
{
String value = "";
if(str != null && !"".equals(str))
value = new String(stringToByte(str));
return value;
}
public static String stringToHex(String str)
{
String value = "";
if(str != null && !"".equals(str))
value = byteToString(str.getBytes());
return value;
}
public static String decryption(String str, String key)
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
byte bt1[] = stringToByte(str);
byte value[] = decrypt(key, bt1);
return hexToString(byteToString(value));
}
public static String encryption(String str, String key)
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
str = stringToHex(str);
byte bt[] = stringToByte(str);
byte value[] = encrypt(key, bt);
return byteToString(value);
}
public static final String ENCODING = "GBK";
}