送分了,一个JAVA中使用blowfish的问题!

smartnose 2002-04-19 10:43:31
请问谁对用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异常!可问怎么设置!
...全文
60 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧