我在windows上做的加密到linux上解密总是失败,报java.lang.Exception: pad block corrupted 

解决方案 »

  1.   

    如果代码不保密的话,请贴出相关 DES 加密和解密的代码,以及测试用的原文数据、加密密钥,以及你在 Windows 上用该密钥和原文进行 DES 加密后的密文。
      

  2.   

     
     
    import java.security.Key;
    import java.security.SecureRandom;import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;public class DesEncrypt {
    private static Key key;
    private final static String KEY_STRING = "cssc2010cssc2010zcec";
    private static DesEncrypt instance = null;

    public static DesEncrypt getInstance(){
    if(instance == null){
    instance = new DesEncrypt();
    }
    return instance;
    }

    public DesEncrypt(String str) {
    //String keyString ="ACDAFAAD";
    getKey(str);// 生成密匙
    } private DesEncrypt(){
    getKey(KEY_STRING);// 生成密匙
    }

    /**
     * 根据参数生成KEY
     */
    public void getKey(String strKey) {
    try {
    KeyGenerator _generator = KeyGenerator.getInstance("DES");
    _generator.init(new SecureRandom(strKey.getBytes()));
     key = _generator.generateKey();
    _generator = null;
    } catch (Exception e) {
    throw new RuntimeException(
    "Error initializing SqlMap class. Cause: " + e);
    }
    } /**
     * 加密String明文输入,String密文输出
     */
    public String getEncString(String strMing) {
    byte[] byteMi = null;
    byte[] byteMing = null;
    String strMi = "";
    BASE64Encoder base64en = new BASE64Encoder();
    try {
    byteMing = strMing.getBytes("UTF8");
    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;
    } /**
     * 解密 以String密文输入,String明文输出
     * 
     * @param strMi
     * @return
     */
    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, "UTF8");
    } catch (Exception e) {
    throw new RuntimeException(
    "Error initializing SqlMap class. Cause: " + e);
    } finally {
    base64De = null;
    byteMing = null;
    byteMi = null;
    }
    return strMing;
    } /**
     * 加密以byte[]明文输入,byte[]密文输出
     * 
     * @param byteS
     * @return
     */
    public static 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;
    } /**
     * 解密以byte[]密文输入,以byte[]明文输出
     * 
     * @param byteD
     * @return
     */
    public static 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;
    } public static void main(String args[]) {
    DesEncrypt des = DesEncrypt.getInstance();
    String str1 = "ASDDasdadadadada你的明 aaasda啊大大大大的dadadq使用手册欠妥人ASDADA";
    // DES加密
            String str2 = des.getEncString(str1);
    str2="sPtE/3vQPyiRYXfyUIVFPx0T9Ya3Uhzg0bNQ+4f4ZvTeOII6aE7+aVchiC3s67T0C8cklLjIEfxiXE3O3gXrwg==";
    System.out.println("根据密钥解密后的明文:" + des.getDesString(str2));
    }

      


    }
      

  3.   

    你这个方法有问题!/**
     * 根据参数生成KEY
     */
    public void getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(strKey.getBytes()));
            key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }使用 KeyGenerator 生成的密钥是随机密钥,并不是你传入的密钥,更改一下:public Key toKey(byte[] key) throws Exception {
        KeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        return keyFactory.generateSecret(dks);
    }DES 属于对称加密算法的块加密,密钥应该是 8 个字节。private final static String KEY_STRING = "cssc2010cssc2010zcec";这样的密钥在 DES 中是不合法的。