/** */
/**
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 25, 2008
*@file Blowfish.java
*@version 1.1
**/
package net.cssweb.common.encrypt;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class Blowfish ... {
public String decrypt(String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalBlockSizeException, BadPaddingException
...{
final byte[] k =
...{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09 };
//System.out.println("key = " + k);
SecretKeySpec key = new SecretKeySpec(k, "Blowfish");
final byte[] ivBytes = ...{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
//String iv = "00000000";
IvParameterSpec spec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, spec);
//cipher.init(Cipher.DECRYPT_MODE, key);
//System.out.println("block size = " + cipher.getBlockSize());
//System.out.println("iv = " + cipher.getIV());
BASE64Decoder decoder = new BASE64Decoder();
byte[] data = decoder.decodeBuffer(input);
//cipher.update
//data = cipher.update(data, 0, data.length);
//System.out.println("长度为" + data.length);
byte[] decryptData = cipher.doFinal(data, 0, data.length);
//cipher.
//System.arraycopy("12345678".getBytes()decryptData, 0, , 0, 8);
//String s = new String(decryptData);
/** *//**
for (int i=0; i<decryptData.length; i++)
System.out.println(decryptData[i]);
**/
//System.out.println("解密数据=" + decryptData.toString());
//System.out.println("解密数据=" + new String(decryptData));
return new String(decryptData);
}
public String encrypt(String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException
...{
final byte[] k =
...{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09 };
System.out.println("key = " + k);
SecretKeySpec key = new SecretKeySpec(k, "Blowfish");
final byte[] ivBytes = ...{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
//String iv = "00000000";
IvParameterSpec spec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes());
CipherOutputStream cos = new CipherOutputStream(bos, cipher);
int theByte = 0;
while ((theByte = bis.read()) != -1)
...{
cos.write(theByte);
}
cos.close();
bis.close();
//System.out.println("加密成功");
return new sun.misc.BASE64Encoder().encode(bos.toByteArray());
}
}
*Copyright 2008, CSSWEB all rights reserved.
*@author hujun
*@date Mar 25, 2008
*@file Blowfish.java
*@version 1.1
**/
package net.cssweb.common.encrypt;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
public class Blowfish ... {
public String decrypt(String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalBlockSizeException, BadPaddingException
...{
final byte[] k =
...{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09 };
//System.out.println("key = " + k);
SecretKeySpec key = new SecretKeySpec(k, "Blowfish");
final byte[] ivBytes = ...{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
//String iv = "00000000";
IvParameterSpec spec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, spec);
//cipher.init(Cipher.DECRYPT_MODE, key);
//System.out.println("block size = " + cipher.getBlockSize());
//System.out.println("iv = " + cipher.getIV());
BASE64Decoder decoder = new BASE64Decoder();
byte[] data = decoder.decodeBuffer(input);
//cipher.update
//data = cipher.update(data, 0, data.length);
//System.out.println("长度为" + data.length);
byte[] decryptData = cipher.doFinal(data, 0, data.length);
//cipher.
//System.arraycopy("12345678".getBytes()decryptData, 0, , 0, 8);
//String s = new String(decryptData);
/** *//**
for (int i=0; i<decryptData.length; i++)
System.out.println(decryptData[i]);
**/
//System.out.println("解密数据=" + decryptData.toString());
//System.out.println("解密数据=" + new String(decryptData));
return new String(decryptData);
}
public String encrypt(String input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException
...{
final byte[] k =
...{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09 };
System.out.println("key = " + k);
SecretKeySpec key = new SecretKeySpec(k, "Blowfish");
final byte[] ivBytes = ...{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
//String iv = "00000000";
IvParameterSpec spec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(input.getBytes());
CipherOutputStream cos = new CipherOutputStream(bos, cipher);
int theByte = 0;
while ((theByte = bis.read()) != -1)
...{
cos.write(theByte);
}
cos.close();
bis.close();
//System.out.println("加密成功");
return new sun.misc.BASE64Encoder().encode(bos.toByteArray());
}
}