带DES的Java-JEC-crytpo

带DES的Java-JEC-crytpo,java,cryptography,padding,Java,Cryptography,Padding,我使用的是WAS7、WindowsXP和Java6。我在解密时遇到以下异常: javax.crypto.BadPaddingException: Given final block not properly padded [12/27/12 17:50:48:832 PKT] 00000013 SystemErr R at com.ibm.crypto.provider.DESCipher.engineDoFinal(Unknown Source) [12/27/12 17:50:

我使用的是WAS7、WindowsXP和Java6。我在解密时遇到以下异常:

javax.crypto.BadPaddingException: Given final block not properly padded
[12/27/12 17:50:48:832 PKT] 00000013 SystemErr     R    at com.ibm.crypto.provider.DESCipher.engineDoFinal(Unknown Source)
[12/27/12 17:50:48:832 PKT] 00000013 SystemErr     R    at com.ibm.crypto.provider.DESCipher.engineDoFinal(Unknown Source)
[12/27/12 17:50:48:832 PKT] 00000013 SystemErr     R    at javax.crypto.Cipher.doFinal(Unknown Source)
[12/27/12 17:50:48:832 PKT] 00000013 SystemErr     R    at com.test.util.encrypt.StringEncrypter.decrypt(StringEncrypter.java:170)
以下是我的java代码:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.log4j.Logger;
import com.test.exceptions.EncryptionException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public final class StringEncrypter {

  private KeySpec keySpec;
  private SecretKeyFactory keyFactory;
  private Cipher cipher;

  private static final Logger LOGGER = Logger.getLogger("com.test.util.StringEncrypter");


  public StringEncrypter(String anEncryptionKey) throws EncryptionException {

    if (anEncryptionKey == null) {
      throw new EncryptionException(new InvalidKeyException("encryption key was null"));
    }
    else if (anEncryptionKey.trim().length() < 24) {
      throw new EncryptionException(new InvalidKeyException("encryption key was less than 24 characters"));
    }
    else {

      try {

        byte[] keyAsBytes = anEncryptionKey.getBytes("UTF-8");

        keySpec = new DESKeySpec(keyAsBytes);

        keyFactory = SecretKeyFactory.getInstance("DES");

        cipher = Cipher.getInstance("DES");

      }
      catch (InvalidKeyException ike) {
        throw new EncryptionException(ike);
      }
      catch (UnsupportedEncodingException uee) {
        throw new EncryptionException(uee);
      }
      catch (NoSuchAlgorithmException nsae) {
        throw new EncryptionException(nsae);
      }
      catch (NoSuchPaddingException nspee) {
        throw new EncryptionException(nspee);
      }
    }
  }


  public String encrypt(String anUnencryptedString) throws EncryptionException {
    if (anUnencryptedString == null || anUnencryptedString.trim().length() == 0) {
      throw new EncryptionException(new InvalidKeyException("unencrypted string was null or empty"));
    }
    else {
      try {
        SecretKey secretKey = keyFactory.generateSecret(keySpec);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cleartext = anUnencryptedString.getBytes("UTF-8");


        byte[] ciphertext = cipher.doFinal(cleartext);

        BASE64Encoder base64encoder = new BASE64Encoder();
        return base64encoder.encode(ciphertext);
      }
      catch (InvalidKeySpecException ikse) {
        throw new EncryptionException(ikse);
      }
      catch (InvalidKeyException ike) {
        throw new EncryptionException(ike);
      }
      catch (UnsupportedEncodingException uee) {
        throw new EncryptionException(uee);
      }
      catch (IllegalBlockSizeException ibse) {
        throw new EncryptionException(ibse);
      }
      catch (BadPaddingException bpee) {
        throw new EncryptionException(bpee);
      }
    }
  }


  public String decrypt(String anEncryptedString) throws EncryptionException {
    if (anEncryptedString == null || anEncryptedString.trim().length() <= 0) {
      throw new EncryptionException(new InvalidKeyException("encrypted string was null or empty"));
    }
    else {
      try {
        SecretKey secretKey = keyFactory.generateSecret(keySpec);

        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        BASE64Decoder base64decoder = new BASE64Decoder();

        byte[] cleartext = base64decoder.decodeBuffer(anEncryptedString);

        byte[] ciphertext = cipher.doFinal(cleartext);

        return bytesToString(ciphertext);
      }
      catch (InvalidKeySpecException ikse) {
        throw new EncryptionException(ikse);
      }
      catch (InvalidKeyException ike) {
        throw new EncryptionException(ike);
      }
      catch (IllegalBlockSizeException ibse) {
        throw new EncryptionException(ibse);
      }
      catch (BadPaddingException bpee) {
          bpee.printStackTrace();
        throw new EncryptionException(bpee);
      }
      catch (IOException ioe) {
        throw new EncryptionException(ioe);
      }
    }
  }

  private static String bytesToString(byte[] someBytes) {
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < someBytes.length; i++) {
      stringBuffer.append((char) someBytes[i]);
    }
    return stringBuffer.toString();
  }
}
import java.io.IOException;
导入java.io.UnsupportedEncodingException;
导入java.security.InvalidKeyException;
导入java.security.NoSuchAlgorithmException;
导入java.security.spec.InvalidKeySpecException;
导入java.security.spec.KeySpec;
导入javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.SecretKey;
导入javax.crypto.SecretKeyFactory;
导入javax.crypto.spec.DESKeySpec;
导入org.apache.log4j.Logger;
导入com.test.exceptions.EncryptionException;
导入sun.misc.base64解码器;
导入sun.misc.base64编码器;
公共最终类加密程序{
专用密钥规范密钥规范;
私密钥匙厂钥匙厂;
专用密码;
私有静态最终记录器Logger=Logger.getLogger(“com.test.util.StringEncrypter”);
公共StringEncrypter(字符串anEncryptionKey)抛出EncryptionException{
if(anEncryptionKey==null){
抛出新的EncryptionException(新的InvalidKeyException(“加密密钥为空”);
}
否则如果(anEncryptionKey.trim().length()<24){
抛出新的EncryptionException(新的InvalidKeyException(“加密密钥少于24个字符”);
}
否则{
试一试{
byte[]keyAsBytes=anEncryptionKey.getBytes(“UTF-8”);
keySpec=新的DESKeySpec(keyAsBytes);
keyFactory=SecretKeyFactory.getInstance(“DES”);
cipher=cipher.getInstance(“DES”);
}
捕获(InvalidKeyException ike){
抛出新的EncryptionException(ike);
}
捕获(不支持的编码异常uee){
抛出新的EncryptionException(uee);
}
捕获(无算法异常nsae){
抛出新的EncryptionException(nsae);
}
捕获(无此填充异常nspee){
抛出新的EncryptionException(nspee);
}
}
}
公共字符串加密(字符串anUnencryptedString)引发EncryptionException{
if(anUnencryptedString==null | | anUnencryptedString.trim().length()==0){
抛出新的EncryptionException(新的InvalidKeyException(“未加密字符串为null或空”);
}
否则{
试一试{
SecretKey SecretKey=keyFactory.generateSecret(keySpec);
cipher.init(cipher.ENCRYPT_模式,secretKey);
byte[]cleartext=anUnencryptedString.getBytes(“UTF-8”);
字节[]密文=cipher.doFinal(明文);
BASE64Encoder BASE64Encoder=新的BASE64Encoder();
返回base64encoder.encode(密文);
}
捕获(InvalidKeySpecException ikse){
抛出新的EncryptionException(ikse);
}
捕获(InvalidKeyException ike){
抛出新的EncryptionException(ike);
}
捕获(不支持的编码异常uee){
抛出新的EncryptionException(uee);
}
捕获(IllegalBlockSizeException ibse){
抛出新的EncryptionException(ibse);
}
捕获(BadPaddingException bpee){
抛出新的EncryptionException(bpee);
}
}
}
公共字符串解密(字符串anEncryptedString)引发EncryptionException{

if(anEncryptedString==null | | anEncryptedString.trim().length()
com.ibm.crypto.provider.DESCipher
您正在使用ibm java,或者它只是安装在Oracle java中的某个ibm crypro提供程序?而且
sun.misc.base64解码器
非常可疑,不要使用它。顺便说一句,您的代码在clear Oracle java 1.6.32中工作得很好使用sun的JDK后,问题仍然存在。请尝试显式设置安全性r:
Cipher.getInstance(“DES”、“SunJCE”);
甚至
Cipher.getInstance(“DES/ECB/pkcs5pding”、“SunJCE”);