加密算法之BlowFish算法

BlowFish加密算法在加密速度上就超越了DES加密算法,引起了人们的关注。BlowFish加密算法没有注册专利,不需要授权,可以免费使用。但是知名度不如AES,BlowFish加密算法是一种对称的分组加密算法,每次加密一个64位分组,使用32位~448位的可变长度密钥,应用于内部加密。

BlowFish算法用来加密64Bit长度的字符串。

  BlowFish算法使用两个“盒”——ungignedlongpbox[18]和unsignedlongsbox[4,256]。

  BlowFish算法中,有一个核心加密函数:BF_En(后文详细介绍)。该函数输入64位信息,运算后,以64位密文的形式输出。用BlowFish算法加密信息,需要两个过程:密钥预处理和信息加密。

代码示例如下:

package com.command;

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";
}

猜你喜欢

转载自www.cnblogs.com/b2cup/p/11950387.html