javax.crypto.BadPaddingException:给定最终块未正确填充

时间:2017-06-12 10:37:59

标签: 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");
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你是base64解码然后解密,但你没有加密,然后是base64编码。