java.security.InvalidKeyException:无效的密钥长度:8个字节

时间:2018-07-12 07:30:33

标签: java 3des

我有一个六角扳手1122334455667788的要求, 十六进制消息2962A83E5D3D5187进行3des解码。但是,当我尝试对其进行解码时,出现错误“ java.security.InvalidKeyException:无效的密钥长度:8个字节”。请有人可以帮助吗?

我要解密的代码

public class TripleDES {

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }


public static void main(String[] args) throws Exception {

    byte[] keyBytes=hexStringToByteArray("1122334455667788");
    byte[] message=hexStringToByteArray("2962A83E5D3D5187");
     final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        decipher.init(Cipher.DECRYPT_MODE, key, iv);

        // final byte[] encData = new
        // sun.misc.BASE64Decoder().decodeBuffer(message);
        final byte[] plainText = decipher.doFinal(message);    
    }
}

3 个答案:

答案 0 :(得分:0)

三重DES密钥的长度为24个字节;参见https://docs.oracle.com/javase/7/docs/api/javax/crypto/spec/DESedeKeySpec.html

(我会尝试将字节数组填充为24个字节,在开头或结尾处填充零个字节,然后查看对您的密文起作用的内容。)


下一个问题...

  

我试图将密钥更改为

  String desKey = "000000000000000000000000000000001122334455667788";
     

但是我现在收到“给最终块未正确填充”异常。

我认为这是指邮件未正确填充。尝试使用“ NoPadding”代替“ PKCS5Padding”。

请参见

答案 1 :(得分:0)

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.xml.bind.DatatypeConverter;

public class DESede {

    private static Cipher encryptCipher;
    private static Cipher decryptCipher;

    private static byte[] encryptData(String data) throws IllegalBlockSizeException, BadPaddingException {
        System.out.println("Data Before Encryption :" + data);
        byte[] dataToEncrypt = data.getBytes();
        byte[] encryptedData = encryptCipher.doFinal(dataToEncrypt);
        System.out.println("Encryted Data: " + encryptedData);
        return encryptedData;
    }

    private static void decryptData(byte[] data) throws IllegalBlockSizeException, BadPaddingException {
        byte[] textDecrypted = decryptCipher.doFinal(data);
        System.out.println("Decryted Data: " + new String(textDecrypted));
    }

    public static void main(String[] args) throws InvalidKeySpecException {
        try {

            String desKey = "0123456789abcdef0123456789abcdef0123456789abcdef"; // Key from user
            byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
            System.out.println((int) keyBytes.length);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
            SecretKey key = factory.generateSecret(new DESedeKeySpec(keyBytes));

            encryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key); // throwing Exception
            byte[] encryptedData = encryptData("Confidential Data");
            decryptCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            byte iv[] = encryptCipher.getIV(); 
            IvParameterSpec dps = new IvParameterSpec(iv);
            decryptCipher.init(Cipher.DECRYPT_MODE, key, dps);
            decryptData(encryptedData);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

DESede需要24个字节的密钥。试试这个程序。

输出

Data Before Encryption :Confidential Data
Encryted Data: [B@6fadae5d
Decryted Data: Confidential Data

答案 2 :(得分:0)

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TripleDES {

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static void main(String[] args) throws Exception {
        byte[] keyBytes = hexStringToByteArray("1122334455667788");
        byte[] message = hexStringToByteArray("2962A83E5D3D5187");
        final SecretKey key = new SecretKeySpec(keyBytes, "DES");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher decipher = Cipher.getInstance("DES/ECB/NoPadding");
        decipher.init(Cipher.DECRYPT_MODE, key);
        final byte[] plainText = decipher.doFinal(message);
        System.out.println("Decrypted Data :: "+new String(plainText));
    }
}

输出

Decrypted Data :: FB