2017-06-12 68 views
1

我使用三重DES来加密/解密的目的,但不知何故,它给了我以上的例外,我尝试了其他方法以及相关答案中提到的,米卡住了。我是密码学和相应的java库的新手。javax.crypto.BadPaddingException:鉴于最后的块没有正确填充

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

回答

1

尽管您的变量名称无法在您的Encrypt方法中对加密结果进行64位编码。因此,当你将它转换为字符串时,你会得到垃圾,并且当你base64在你的Decrypt方法中解码垃圾时,你会得到垃圾。

0

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

+0

这应该是一个评论而不是答案! – Yahya

+0

@Yahya你错了。这是一个答案,而不是评论。它识别OP代码中的问题。它和你确定的答案完全一样。 – EJP

相关问题