Talib
Talib

Reputation: 1164

java.security.InvalidKeyException: Wrong key size

I am building a desktop app in netbeans in order to load the keys on the pinpad (device) . The same code runs on the android and works fine with 16 byte keys but on the desktop app it throws me error 16 bytes but it works fine with 24 byte keys, error is :

    java.security.InvalidKeyException: Wrong key size
    at com.sun.crypto.provider.DESedeCrypt.init(DESedeCrypt.java:69)

    at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:93)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:458)
    at com.sun.crypto.provider.DESedeCipher.engineInit(DESedeCipher.java:166)
    at javax.crypto.Cipher.implInit(Cipher.java:797)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
    at javax.crypto.Cipher.init(Cipher.java:1229)

    at javax.crypto.Cipher.init(Cipher.java:1166)
    at payokeygen.CryptoUtil.encrypt3DESECB(CryptoUtil.java:62)
    at payokeygen.UseDatecs.loadKeys(UseDatecs.java:135)
    at payokeygen.mainscreen.loadPPEKeysActionPerformed(mainscreen.java:435)
    at payokeygen.mainscreen.access$300(mainscreen.java:34)
    at payokeygen.mainscreen$4.actionPerformed(mainscreen.java:186)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.OutputStream.write(OutputStream.java:75)
    at com.datecs.pinpad.Pinpad.cryptoExchangeECBKey(Pinpad.java:1362)
    at payokeygen.UseDatecs.loadKeys(UseDatecs.java:135)
    at payokeygen.mainscreen.loadPPEKeysActionPerformed(mainscreen.java:435)
    at payokeygen.mainscreen.access$300(mainscreen.java:34)
    at payokeygen.mainscreen$4.actionPerformed(mainscreen.java:186)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

code to load the keys on pinpad is :

 pinpad.cryptoExchangeECBKey(HMK_INDEX, KEK_PIN_INDEX, Pinpad.KEY_KEK_PIN, 0, CryptoUtil.encrypt3DESECB(HMK, KEK_PIN));

encrypt3DESECB function in "CryptoUtil" is :

public static byte[] encrypt3DESECB(byte[] keyBytes, byte[] dataBytes) {
    try {
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey);
        System.out.println("function called");
        return cipher.doFinal(dataBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}           

16 bytes Key is : 1A C4 F2 34 79 CD 8F 23 0B C4 9D 2C 98 C8 91 EA

Using JDK 1.8. please help, thanks in advance !

Upvotes: 3

Views: 5145

Answers (1)

Ebbe M. Pedersen
Ebbe M. Pedersen

Reputation: 7488

Behind the scene 3Des is actually just 3 single DES operations, that each require a 8 byte key. This gives the need for 24 bytes of key material .. however some implementations supports 16 bytes keys where the first 8 bytes are "automatically repeated" behind the scene .. others might not support this. Howevere you can do this yourself by repeating the first 8 bytes - something like this:

public static byte[] encrypt3DESECB(byte[] keyBytes, byte[] dataBytes) {
    try {
        if (keyBytes.length == 16) { // short key ? .. extend to 24 byte key 
            byte[] tmpKey = new byte[24];
            System.arraycopy(keyBytes, 0, tmpKey, 0, 16);
            System.arraycopy(keyBytes, 0, tmpKey, 16, 8);
            keyBytes = tmpKey;
        }

        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "DESede");
        ...
        ...

Upvotes: 2

Related Questions