`
xiaojunhu
  • 浏览: 30111 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Blowfish算法

 
阅读更多
/***//**
*Copyright2008,CSSWEBallrightsreserved.
*
@authorhujun
*@dateMar25,2008
*@fileBlowfish.java
*
@version1.1
*
*/

packagenet.cssweb.common.encrypt;

importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;

importjava.io.IOException;
importjava.security.InvalidAlgorithmParameterException;
importjava.security.InvalidKeyException;
importjava.security.NoSuchAlgorithmException;

importjavax.crypto.BadPaddingException;
importjavax.crypto.Cipher;
importjavax.crypto.CipherOutputStream;
importjavax.crypto.IllegalBlockSizeException;
importjavax.crypto.NoSuchPaddingException;
importjavax.crypto.spec.IvParameterSpec;
importjavax.crypto.spec.SecretKeySpec;

importsun.misc.BASE64Decoder;

publicclassBlowfish...{

publicStringdecrypt(Stringinput)throwsNoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,InvalidAlgorithmParameterException,IOException,IllegalBlockSizeException,BadPaddingException
...{
finalbyte[]k=
...{
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09}
;
//System.out.println("key="+k);
SecretKeySpeckey=newSecretKeySpec(k,"Blowfish");

finalbyte[]ivBytes=...{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

//Stringiv="00000000";
IvParameterSpecspec=newIvParameterSpec(ivBytes);

Ciphercipher
=Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Ciphercipher=Cipher.getInstance("Blowfish/CBC/NoPadding");

cipher.init(Cipher.DECRYPT_MODE,key,spec);
//cipher.init(Cipher.DECRYPT_MODE,key);
//System.out.println("blocksize="+cipher.getBlockSize());
//System.out.println("iv="+cipher.getIV());

BASE64Decoderdecoder
=newBASE64Decoder();
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);

//Strings=newString(decryptData);
/***//**
for(inti=0;i<decryptData.length;i++)
System.out.println(decryptData[i]);
*
*/


//System.out.println("解密数据="+decryptData.toString());
//System.out.println("解密数据="+newString(decryptData));

returnnewString(decryptData);
}


publicStringencrypt(Stringinput)throwsNoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,InvalidAlgorithmParameterException,IOException
...{
finalbyte[]k=
...{
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09}
;
System.out.println(
"key="+k);
SecretKeySpeckey
=newSecretKeySpec(k,"Blowfish");

finalbyte[]ivBytes=...{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

//Stringiv="00000000";
IvParameterSpecspec=newIvParameterSpec(ivBytes);

Ciphercipher
=Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
//Ciphercipher=Cipher.getInstance("Blowfish/CBC/NoPadding");

cipher.init(Cipher.ENCRYPT_MODE,key,spec);

ByteArrayOutputStreambos
=newByteArrayOutputStream();
ByteArrayInputStreambis
=newByteArrayInputStream(input.getBytes());
CipherOutputStreamcos
=newCipherOutputStream(bos,cipher);
inttheByte=0;
while((theByte=bis.read())!=-1)
...{
cos.write(theByte);
}

cos.close();
bis.close();
//System.out.println("加密成功");
returnnewsun.misc.BASE64Encoder().encode(bos.toByteArray());
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics