帮我看一下这个Des加解密类...
编译没有问题,加密也没有问题,就是解密的时候,出现:
java.lang.RuntimeException: Error initializing SqlMap class. Cause: java.lang.RuntimeException: Error initializing SqlMap class. Cause: javax.crypto.BadPaddingException: Given final block not properly padded
不知道如何解决,
test.jsp文件
**************************************************************
<%@ page import= "net.moftree.client.DesEncrypt " contentType= "text/html;charset=UTF-8 " errorPage= "/app/errorHandler.jsf " %>
DesEncrypt des = new DesEncrypt( "19771205 ");
String str1 = "dmhwxf ";
//DES加密
String str2=des.getEncString(str1);
out.println( "根据密钥加密后的密文: "+str2+ ": ");
String str3= "9UKtP8c4gu4= ";//
out.println( "根据密钥解密后的明文: "+des.getDesString(str3));
我每刷一次test.jsp 相同的key和str1所生成的 str2值都不一样,我用生成的str3值写到str3里面,然后再解密,就出现了上面的问题.
DesEncrypt.java文件
package net.moftree.client;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;
public class DesEncrypt
{
Key key;
public DesEncrypt(String str)
{
getKey(str);
}
public void getKey(String strKey)
{
try
{
KeyGenerator _generator = KeyGenerator.getInstance( "DES ");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
}
catch (Exception e)
{
throw new RuntimeException( "Error initializing SqlMap class. Cause: "+e);
}
}
public String getEncString(String strMing)
{
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = " ";
BASE64Encoder base64en = new BASE64Encoder();
try
{
byteMing = strMing.getBytes( "UTF-8 ");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
}
catch (Exception e)
{
throw new RuntimeException( "Error initializing SqlMap class. Cause: "+e);
}
finally
{
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
public String getDesString(String strMi)
{
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = " ";
try
{
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF-8 ");
}
catch (Exception e)
{
throw new RuntimeException( "Error initializing SqlMap class. Cause: "+e);
}
finally
{
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
private byte[] getEncCode(byte[] byteS)
{
byte[] byteFina = null;
Cipher cipher;
try
{
cipher = Cipher.getInstance( "DES ");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
}
catch (Exception e)
{
throw new RuntimeException( "Error initializing SqlMap class. Cause: "+e);
}
finally
{
cipher = null;
}
return byteFina;
}
private byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina = null;
try
{
cipher = Cipher.getInstance( "DES ");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}
catch (Exception e)
{
throw new RuntimeException( "Error initializing SqlMap class. Cause: "+e);
}
finally
{
cipher = null;
}
return byteFina;
}
}
[解决办法]
up