javax.crypto.BadPaddingException : Given final block not properly padded

标签 java encryption tripledes javax.crypto

我正在使用 Triple DES 来实现加密/解密目的,但不知何故它给了我上述异常,我尝试了相关答案中提到的其他方法,但我陷入了困境。我对密码学和相应的 java 库很陌生。

private static byte[] Key = new byte[] {
        0x42, 0x45, 0x49, 0x30, 0x12, 0x22, 0x35, 0x48, 0x33, 0x24, 0x28, 0x51,
        0x48, 0x24, 0x30, 0x21, 0x44, 0x31, 0x14, 0x19, 0x45, 0x34, 0x47, 0x25 };

Cipher c;

public EncryptionHelper() throws Exception {
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 32);
    SecretKey key = new SecretKeySpec(Key, 0, Key.length, "DESede");
    c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
}

public String Encrypt(String S) throws Exception {
    byte[] base64EncryptedText = S.getBytes("UTF-8");
    byte EncryptedText[] = c.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(EncryptedText);
}

public String Decrypt(String S) throws Exception {
    Cipher c2 = null;
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 24);
    SecretKey key = new SecretKeySpec(Key,0, Key.length, "DESede");
    c2 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c2.init(Cipher.DECRYPT_MODE, key);
    byte[] base64EncryptedText = Base64.getEncoder().encode(S.getBytes());
    byte[] textDecrypted = c2.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(textDecrypted, "UTF-8");
}

编辑:

终于解决了这个问题,我只是放错了组件,还指定了核心逻辑。

public class EncryptionHelper {

private static byte[] Key = new byte[] {
    0x42, 0x45, 0x49, 0x30, 0x12, 0x22, 0x35, 0x48, 0x33, 0x24, 0x28, 0x51,
    0x48, 0x24, 0x30, 0x21, 0x44, 0x31, 0x14, 0x19, 0x45, 0x34, 0x47, 0x25 };

static Cipher c;

public EncryptionHelper() throws Exception {
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 32);
    SecretKey key = new SecretKeySpec(Key, 0, Key.length, "DESede");
    c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
}

public static String Encrypt(String S) throws Exception {
    byte[] base64EncryptedText = S.getBytes("UTF-8");
    byte EncryptedText[] = c.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(Base64.getEncoder().encode(EncryptedText));
}

// LOGIC:
// for encryption: string -> utf-8 byte array,
        // encrypt and return a base 64 encoded string
// for decryption: String -> base64 -> decode base 64 array,
        // decrypt and return utf-8 string

public static String Decrypt(String S) throws Exception {
    Cipher c2 = null;
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 24);
    SecretKey key = new SecretKeySpec(Key, "DESede");
    c2 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c2.init(Cipher.DECRYPT_MODE, key);
    byte[] base64EncryptedText = Base64.getDecoder().decode(S.getBytes());
    byte[] textDecrypted = c2.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(textDecrypted, "UTF-8");
}

最佳答案

尽管变量上有名称,但您未能在 Encrypt 方法中对加密结果进行 Base64 编码。因此,当您将其转换为 String 时,您会得到垃圾,而当您在 Decrypt 方法中对该垃圾进行 Base64 解码时,您会得到垃圾2

关于javax.crypto.BadPaddingException : Given final block not properly padded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44497375/

相关文章:

java - 尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.clearTextFilter()'

encryption - 三重DES。指定的填充模式对此算法无效

c++ - openssl 3des 与 c++

php - 有 "MD5-based block cipher"的 Go 版本吗?

Java AES加解密过程及Initialization Vector的使用

javascript - 在 Titanium 移动项目中使用 "CryptoJS"库来解密短信 [Triple DES]

JAVA XML 删除节点 - 仅删除第一次出现

java - 为什么当操作系统代表时java会承担线程切换的开销

java - 是否有可能将内部联接选择结果转换为嵌套列表 JSON?

python - python中的Rijndael加密