送分了,一个JAVA中使用blowfish的问题!
请问谁对用blowfish来做JAVA的加密解密比较熟悉,请帮忙写个文本的加密解密类,(我这有一个但是写得不很成熟不能解密)
这是源代码。
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.SunJCE;
public class SuperCode {
private KeyGenerator keygen;
private SecretKey skey ;
private Cipher cipher;
private byte[] code=new byte[1024*8];
SecretKeySpec skeySpec;
byte[] raw;
//constructor
public SuperCode( ) {
try{
//if(keygen==null){
keygen = KeyGenerator.getInstance("Blowfish");
// }
// if(skey==null) {
skey = keygen.generateKey();
// }
// if(cipher==null){
cipher = Cipher.getInstance("Blowfish");
// }
}catch(Exception unreported){
keygen=null;
skey=null;
cipher=null;
System.out.println("初始化错误");
}
}
public String encrypt(String encryptCode){
try{
String temp;
raw = skey.getEncoded();
skeySpec = new SecretKeySpec(raw, "Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
code=cipher.doFinal(encryptCode.getBytes());
encryptCode=( new Byte(code[1])).toString();
for (int i=1; i<code.length;i++){
temp=(new Byte(code[i])).toString();
encryptCode+=temp ;
}
}catch(Exception unreported){System.out.print("无法加密");}
code=null;
return encryptCode;
}
public String decrypt(String decryptCode){
String temp;
try{
raw = skey.getEncoded();
skeySpec = new SecretKeySpec(raw, "Blowfish");
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
code=cipher.doFinal(decryptCode.getBytes());
decryptCode=( new Byte(code[1])).toString();
for (int i=1; i<code.length;i++){
temp=(new Byte(code[i])).toString();
decryptCode+=temp ;
}
}
catch(IllegalBlockSizeException d){System.out.print("IllegalBlockSizeException");
System.out.print(d); }
catch(IllegalStateException c){System.out.print("IllegalStateException");}
catch(BadPaddingException b){System.out.print("BadPaddingException");}
catch(InvalidKeyException a){System.out.print("无效密钥");}
// catch(Exception unreported){System.out.print("无法解密");}
code=null;
return decryptCode;
}
}
在使用中会出现BadPaddingException异常!可问怎么设置!