简体   繁体   English

Java解密期间的BadPaddingException

[英]BadPaddingException during decryption in java

I am new java to encryption. 我是加密的新Java。 Trying to implement something light weight to encrypt a string and store it somewhere and de-crypt it back before using. 尝试实现一些轻量级的功能,以加密字符串并将其存储在某个位置,然后在使用之前将其解密。

With some web search I came up with this for encryption and decryption. 通过一些网络搜索,我想到了进行加密和解密的方法。

   public static String base64Encode(byte[] bytes)
    {
        return new BASE64Encoder().encode(bytes);
    }

   public static byte[] base64Decode(String property) throws IOException
    {
        return new BASE64Decoder().decodeBuffer(property);
    }


    public static String encrypt(String mystring) throws GeneralSecurityException, UnsupportedEncodingException
       {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(mystring.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return base64Encode(pbeCipher.doFinal(mystring.getBytes("UTF-8")));
        }

    public static String decrypt(String estring) throws GeneralSecurityException, IOException
        {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(estring.toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        return new String(pbeCipher.doFinal(base64Decode(estring)), "UTF-8");
        }

I see that encryption worked but I saw a padding related exception in the decryption part, from the doFinal block. 我看到加密有效,但在doFinal块的解密部分中看到了与填充相关的异常。 Here it is... 这里是...

 encrypted string:zdrtgOKfkZMgpCOflr1ILQ==  -> Encrypted String
 exceptionjavax.crypto.BadPaddingException: Given
 final block not properly padded -> Exception from the doFinal block.

Seems like when I encrypted it, I need to do some kind of padding. 好像当我加密它时,我需要做某种填充。

Can any one tell me what went wrong and how can it be fixed? 谁能告诉我哪里出了问题以及如何解决?

Thanks 谢谢

Tas 塔斯

You are using password based encryption here. 您在此处使用基于密码的加密。 This means that the encryption key itself is based upon a password. 这意味着加密密钥本身是基于密码的。 You must use the same password for both encryption and decryption. 加密和解密必须使用相同的密码。

private static char[] ENCRYPTION_PASSWORD
     = "some password populated by configuration".toCharArray();

public static String encrypt(String mystring)
  throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(ENCRYPTION_PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return DatatypeConverter
        .printBase64Binary(pbeCipher.doFinal(mystring.getBytes("UTF-8")));
}

public static String decrypt(String string)
  throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(ENCRYPTION_PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(DatatypeConverter
                                           .parseBase64Binary(estring)), "UTF-8");
}

Note also the use of javax.xml.bind.DatatypeConverter for base64 operations. 还要注意将javax.xml.bind.DatatypeConverter用于base64操作。 No need to write your own or use third parties these days. 这些天无需自己编写或使用第三方。

You will need to first decode your input to the decrypt method. 您将需要首先将您对解密方法的输入进行解码。

Call the base64Decode method and the estring parameter in your decrypt method. 在您的decrypt方法中调用base64Decode方法和estring参数。

That should do it. 那应该做。

Change your code , 更改您的代码,

SecretKey key = keyFactory.generateSecret(new PBEKeySpec(mystring.toCharArray()));

to

SecretKey key = keyFactory.generateSecret(new PBEKeySpec("PASSWORD".toCharArray(), SALT, 20));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM