gpt4 book ai didi

java.security.InvalidKeyException : Wrong key size

转载 作者:行者123 更新时间:2023-12-05 00:39:16 26 4
gpt4 key购买 nike

我正在 netbeans 中构建一个桌面应用程序,以便在密码键盘(设备)上加载按键。相同的代码在 android 上运行并且使用 16 字节键可以正常工作但是在桌面应用程序上它会抛出 16 字节错误但是它可以正常使用 24 字节键,错误是:

    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)

在密码键盘上加载按键的代码是:

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

“CryptoUtil”中的 encrypt3DESECB 函数是:

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字节 key 是:1A C4 F2 34 79 CD 8F 23 0B C4 9D 2C 98 C8 91 EA

使用 JDK 1.8。请帮助,提前致谢!

最佳答案

3Des 的背后实际上只是 3 个 DES 操作,每个操作都需要一个 8 字节的 key 。这需要 24 个字节的 key Material ……但是一些实现支持 16 个字节的 key ,其中前 8 个字节在幕后“自动重复”……其他人可能不支持这个。然而,您可以通过重复前 8 个字节来自己完成此操作 - 如下所示:

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

关于java.security.InvalidKeyException : Wrong key size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34737457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com