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

smartnose smartnose 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请问谁对用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异常!可问怎么设置!
...全文
评论
收藏 已收藏
分享
返回顶部
smartnose
Java SE 62,621 成员 307,251 内容