java的javax.crypto.Cipher类可以用来做blowfish-cbc算法的编解码;(关于这个类的用法很多好心人已经放在网上,我这里只记录我遇到的问题及解决方法)
我写了一段程序目的是实现php写的一段blowfish-cbc编解码算法,废话不多说了直接上代码:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class BlowfishCoder {
private static final String KEY = "fucking_java_fucking_java_fucking_java_fucking_java_fucking_javafucking_java";
private static final String ALGORITHM = "Blowfish";
private static final String MODE = "Blowfish/CBC/NoPadding";
private SecretKeySpec key = null;
private Cipher cipherEcode = null;
private Cipher cipherDecode = null;
private static BlowfishCoder blowfishCoder = null;
private BlowfishCoder(){}
private void initKey(){
if(null != this.key){
return;
}
try{
byte salt[] = KEY.subSequence(0, 32).toString().getBytes();
this.key = new SecretKeySpec(salt, ALGORITHM);
}catch (Exception e) {
e.printStackTrace();
}
}
private void initCipher(int mode){
initKey();
try {
Cipher cipher = Cipher.getInstance(MODE);
byte[] iv = KEY.subSequence(32, 40).toString().getBytes();
IvParameterSpec sr = new IvParameterSpec(iv);
cipher.init(mode, this.key, sr);
if(Cipher.DECRYPT_MODE == mode){
this.cipherDecode = cipher;
}
if(Cipher.ENCRYPT_MODE == mode){
this.cipherEcode = cipher;
}
}catch (Exception e) {
e.printStackTrace();
}
}
private String cripherDecode(String in){
try {
String pass = in;
byte[] xx = Base64.decode(pass);
Cipher cipher = this.cipherDecode;
byte[] encrypted = cipher.doFinal( xx );
String id = new String(encrypted);
return id;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String cripherEncode(String in){
try {
Cipher cipher = this.cipherEcode;
byte[] encrypted = cipher.doFinal( in.getBytes() );
String code = Base64.encode(encrypted);
return code;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static void getInstance(){
if(null == blowfishCoder){
blowfishCoder = new BlowfishCoder();
blowfishCoder.initCipher(Cipher.DECRYPT_MODE);
blowfishCoder.initCipher(Cipher.ENCRYPT_MODE);
}
}
public static String idCrypt(String text, String mode){
getInstance();
String retStr = null;
if ( 0 == mode.compareTo("ENCRYPT") ){
retStr = blowfishCoder.cripherEncode(text);
}
if ( 0 == mode.compareTo("DECRYPT") ){
retStr = blowfishCoder.cripherDecode(text);
}
return retStr;
}
public static void main(String args[]){
String code = idCrypt("54539405", "ENCRYPT");
String id = idCrypt(code, "DECRYPT");
System.out.println("54539405->"+code);
System.out.println(code+"->"+id);
}
}
调试的时候总是异常:
InvalidKeyException: Illegal key size
我把这段代码:
KEY.subSequence(0, 32).toString().getBytes();
改为:
KEY.subSequence(0, 8).toString().getBytes();
成功执行,但这并不是我想要的结果。
Google一下在国内的论坛并没有发现有人问这样的问题,old外的网站论坛上有很多人遇到这个问题,有人提出解决方法是下载jbc安装,我很兴奋,try it。
下载下来发现有两个jar包:local_policy.jar,US_export_policy.jar;
我把他们直接copy到 jre/lib/security/目录下,发现这里面已经有了,不管那么多直接替换掉。
改回代码:
KEY.subSequence(0, 32).toString().getBytes();
然后运行,成功,结果是:
54539405->jmViRJdYx7s=
jmViRJdYx7s=->54539405
javaeye 开博第一篇日志,小小的庆祝一下。
分享到:
相关推荐
BlowFish Java版实现,里面有工具类,注意修改一下包名!
Java中的Blowfish对称密钥加密算法类和实例
Java Blowfish对称加密算法实例,应用Blowfish对称密钥加密的一个类,另外还有一个使用该类的例子,研究下Java加密的基础知识,仅供参考。
blowfish算法,包括Blowfish-ECB,Blowfish-CBC,Blowfish-CTR,Blowfish-OFB,Blowfish-CFB,目前我的Demo只实现了ECB是如何运用的。[/b][/i]注意:嵌入式开发的小伙伴们,如果blowfish.h找不到头文件的时候,需要将...
对称加密算法Blowfish(java).rar 可运行 对称加密算法Blowfish(java).rar 可运行
FlyUtilsEncrypt AES DES Blowfish CBC RSA ECC MD5 .zip
blowfish 加密解密
Blowfish加密解密工具类,java语言适用
FlyUtilsEncrypt AES DES Blowfish CBC RSA ECC MD5 CRC 各种加密和校验
Blowfish算法的C#与java实现,代码已经过测试,可以实现C#、java加密解密互通。
Blowfish加解密算法(C++实现的)
Blowfish是一个对称加密块算法,由Bruce Schneider于1993年设计,现已应用在多种加密产品。Blowfish能保证很好的加密速度,并且目前为止没有发现有效地破解方法
Java中的Blowfish对称密钥加密算法类和实例
Blowfish加密算法Blowfish加密算法Blowfish加密算法Blowfish加密算法
基于java的开发源码-Blowfish对称密钥加密算法类和实例.zip 基于java的开发源码-Blowfish对称密钥加密算法类和实例.zip 基于java的开发源码-Blowfish对称密钥加密算法类和实例.zip 基于java的开发源码-Blowfish对称...
blowfish java的实现............................
C#与java的 BlowFish算法的实现,代码已经过测试,可以实现C#、java加密解密互通。
实现BloeFish的编解码过程,c++语言实现,可移植参考。
blowfish加密解密算法的C#实现,blowfish加密解密算法的C#实现