给定最终块未从 Cipher.doFinal DES 正确填充 BadPaddingException
Given final block not properly padded BadPaddingException from Cipher.doFinal DES
注意:根据接受的答案,这只是提供错误密钥的问题。如果对其他人有用,我在下面留下了代码和初始问题。
=======
大约一年半前我加密了一些文本,现在我无法解密它。它当时有效(我知道字符串本身没有改变),但现在我得到一个 "Given final block not properly padded" BadPaddingException.
这是一个带有加密字符串、密钥和我当时使用的代码的独立程序:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DoDecode {
private static final String DES_TYPE = "DES";
// private static final String DES_TYPE = "DES/CBC/NoPadding";
// private static final String DES_TYPE = "DES/CBC/PKCS5Padding";
// private static final String DES_TYPE = "DES/ECB/NoPadding";
// private static final String DES_TYPE = "DES/ECB/PKCS5Padding"; //Use this
// private static final String DES_TYPE = "DESede/CBC/NoPadding";
// private static final String DES_TYPE = "DESede/CBC/PKCS5Padding";
// private static final String DES_TYPE = "DESede/ECB/NoPadding";
// private static final String DES_TYPE = "DESede/ECB/PKCS5Padding";
public synchronized static String encode(String unencodedString, String key) {
String ret = null;
try {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey skey = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Encoder base64encoder = new BASE64Encoder();
byte[] cleartext = unencodedString.getBytes("UTF8");
Cipher cipher = Cipher.getInstance(DES_TYPE);
cipher.init(Cipher.ENCRYPT_MODE, skey);
ret = base64encoder.encode(cipher.doFinal(cleartext));
} catch (Exception ex) {
System.err.println("Encode exception: "+ex.getMessage());
}
return ret;
}
public static String decode(String encodedString, String key) {
String ret = null;
try {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey skey = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encodedString);
Cipher cipher = Cipher.getInstance(DES_TYPE);
cipher.init(Cipher.DECRYPT_MODE, skey);
byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
ret = new String(plainTextPwdBytes);
} catch (Exception ex) {
System.err.println("Decode exception: " + ex.getMessage());
}
return ret;
}
private static final String wasValidStr = "h1JTFcRjW6vveQUrQqPUgnjGXo3NEZKDnBThZQN7uLfzPEpeFFONV4mvL71cT/xQb1mz5Xa/XZ/aW2GawZNumgO0reUZSDh30F7NfK0S/rMWM8FxcjBCkfFWAbLZHcyDJ5wW3F1yl5g=";
public static void main(String[] args) {
System.out.println(DoDecode.decode(wasValidStr, "invpwd~~"));
String encoded = DoDecode.encode("This has worked in the past!", "invpwd~~");
System.out.println(encoded);
System.out.println(DoDecode.decode(encoded, "invpwd~~"));
}
}
给我输出:
Decode exception: Given final block not properly padded
null
U3ruztxHelQegTLyyA3IfMaGgVtmbP5na43S9JQmIc8=
This has worked in the past!
请注意,我恢复了我使用的代码,因为它可能是罪魁祸首。我目前没有使用 Base64 的 sun.misc 包,我得到了同样的错误 (java.util.Base64).
我已经在 Linux、一台 Mac 和一台 PC 上试过,结果相同。我使用了多个 JDK 版本回到 Java 1.6u45。我还在 运行 示例代码中添加了每个不同的 DES_TYPEs 在 class.
的顶部
非常感谢任何帮助!
我找到了答案。秘密密钥实际上是不正确的。错误消息是如此令人困惑,以至于我一直在寻找异常的更复杂的原因。当我终于崩溃并尝试其他历史钥匙时,一把神奇地打开了一切。 :(
因此(正如其他帖子所强调的那样),此错误可能只是意味着您提供了错误的密钥。
注意:根据接受的答案,这只是提供错误密钥的问题。如果对其他人有用,我在下面留下了代码和初始问题。
=======
大约一年半前我加密了一些文本,现在我无法解密它。它当时有效(我知道字符串本身没有改变),但现在我得到一个 "Given final block not properly padded" BadPaddingException.
这是一个带有加密字符串、密钥和我当时使用的代码的独立程序:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DoDecode {
private static final String DES_TYPE = "DES";
// private static final String DES_TYPE = "DES/CBC/NoPadding";
// private static final String DES_TYPE = "DES/CBC/PKCS5Padding";
// private static final String DES_TYPE = "DES/ECB/NoPadding";
// private static final String DES_TYPE = "DES/ECB/PKCS5Padding"; //Use this
// private static final String DES_TYPE = "DESede/CBC/NoPadding";
// private static final String DES_TYPE = "DESede/CBC/PKCS5Padding";
// private static final String DES_TYPE = "DESede/ECB/NoPadding";
// private static final String DES_TYPE = "DESede/ECB/PKCS5Padding";
public synchronized static String encode(String unencodedString, String key) {
String ret = null;
try {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey skey = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Encoder base64encoder = new BASE64Encoder();
byte[] cleartext = unencodedString.getBytes("UTF8");
Cipher cipher = Cipher.getInstance(DES_TYPE);
cipher.init(Cipher.ENCRYPT_MODE, skey);
ret = base64encoder.encode(cipher.doFinal(cleartext));
} catch (Exception ex) {
System.err.println("Encode exception: "+ex.getMessage());
}
return ret;
}
public static String decode(String encodedString, String key) {
String ret = null;
try {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey skey = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encodedString);
Cipher cipher = Cipher.getInstance(DES_TYPE);
cipher.init(Cipher.DECRYPT_MODE, skey);
byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
ret = new String(plainTextPwdBytes);
} catch (Exception ex) {
System.err.println("Decode exception: " + ex.getMessage());
}
return ret;
}
private static final String wasValidStr = "h1JTFcRjW6vveQUrQqPUgnjGXo3NEZKDnBThZQN7uLfzPEpeFFONV4mvL71cT/xQb1mz5Xa/XZ/aW2GawZNumgO0reUZSDh30F7NfK0S/rMWM8FxcjBCkfFWAbLZHcyDJ5wW3F1yl5g=";
public static void main(String[] args) {
System.out.println(DoDecode.decode(wasValidStr, "invpwd~~"));
String encoded = DoDecode.encode("This has worked in the past!", "invpwd~~");
System.out.println(encoded);
System.out.println(DoDecode.decode(encoded, "invpwd~~"));
}
}
给我输出:
Decode exception: Given final block not properly padded
null
U3ruztxHelQegTLyyA3IfMaGgVtmbP5na43S9JQmIc8=
This has worked in the past!
请注意,我恢复了我使用的代码,因为它可能是罪魁祸首。我目前没有使用 Base64 的 sun.misc 包,我得到了同样的错误 (java.util.Base64).
我已经在 Linux、一台 Mac 和一台 PC 上试过,结果相同。我使用了多个 JDK 版本回到 Java 1.6u45。我还在 运行 示例代码中添加了每个不同的 DES_TYPEs 在 class.
的顶部非常感谢任何帮助!
我找到了答案。秘密密钥实际上是不正确的。错误消息是如此令人困惑,以至于我一直在寻找异常的更复杂的原因。当我终于崩溃并尝试其他历史钥匙时,一把神奇地打开了一切。 :(
因此(正如其他帖子所强调的那样),此错误可能只是意味着您提供了错误的密钥。