-1

I am trying to implement a 3DES encryption/decryption with CBC mode in java.

The initialisation vector is 0000000000000000
Encrypted string is DD446C051A83BFD98144C348935C61D81398CF29CCFE1CCD
Key for decryption is DCBA4F836E45BAB04FAB2937454075D9

I am receiving below error. Any help will be much appreciated. Thanks.

java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 8 bytes long

Code:

import java.security.Key; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Base64; 

public class DES3 { 

    private static final String IV = "01234567"; 
    private static final String CHARSET = "utf-8"; 

    public static String decrypt(String iv, String secretKey, String encryptText) throws Exception { 
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes()); 
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); 
        Key deskey = keyfactory.generateSecret(spec); 
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding"); 
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes()); 
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips); 
        byte[] decryptData = cipher.doFinal(Base64.decodeBase64(encryptText)); 
        return new String(decryptData, CHARSET); 
    } 

    public static String decrypt(String secretKey, String encryptText) throws Exception { 
        return decrypt(IV, secretKey, encryptText); 
    } 

    public static void main(String[] args) {

        try {
            String iv="0000000000000000";
            System.out.println("iv size"+iv.getBytes("UTF8").length);
            decrypt(iv, "DCBA4F836E45BAB04FAB2937454075D9", "DD446C051A83BFD98144C348935C61D81398CF29CCFE1CCD");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}
Preetam
  • 49
  • 2
  • 8
  • 1. It is time to learn binary and encodings such as hexadecimal (hex), Base64, ASCII and UTF-8. 2. An IV of all zeros (0x00) is not secure, instead use a random IV, just prefix the encrypted data with the IV for use in decryption, it does not need to be secret. – zaph Apr 11 '18 at 18:23

1 Answers1

2

You problem is here:

 String iv="0000000000000000";

which shows your IV is hex string. But in your decrypt function you use:

iv.getBytes()

for this IV. This is obviously over 16 bytes long. You need to convert hex string to byte, not only use getBytes() function. getBytes() convert each chat to corresponding ASCII code, so you will get an array of 16 bytes each 0x30, rather than getting an array of 8 bytes each 0.

By the way, you have same problem for your secret key and your input too.

You can check hexStringToByteArray() here.

Afshin
  • 8,839
  • 1
  • 18
  • 53
  • Thanks. I kept this because I believe 3DES decryption uses 16 bytes IV. I have decrypted the values in my code in an online decrypter. It uses 16 bytes. Any thoughts please? Thanks Please check the link: http://tripledes.online-domain-tools.com/link/11da349gxUIcgKonRz/ – Preetam Apr 12 '18 at 15:32
  • @Preetam you are wrong. both DES and 3DES have block and IV size equal to 8, not 16. An small search shows this: https://en.wikipedia.org/wiki/Triple_DES 3DES is just using DES for 3 times, it does not change IV or block size. In addition, in link that you sent me IV is 8 bytes too...<. – Afshin Apr 12 '18 at 15:37