0

大家好,当我尝试复制和粘贴我生成的 ssh2 公钥时出现此错误。

这是错误的完整堆栈跟踪:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
    at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
    at com.josh.crypto.CryptoUtil.convertKeytoKeyObject(CryptoUtil.java:114)
    at com.josh.crypto.CryptoUtil.encryptData(CryptoUtil.java:80)
    at com.josh.ui.EncryptionView$2.actionPerformed(EncryptionView.java:112)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6382)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6147)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4744)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:663)
    at java.awt.EventQueue$2.run(EventQueue.java:661)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:677)
    at java.awt.EventQueue$3.run(EventQueue.java:675)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.security.InvalidKeyException: invalid key format
    at sun.security.x509.X509Key.decode(X509Key.java:370)
    at sun.security.x509.X509Key.decode(X509Key.java:386)
    at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:66)
    at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:281)
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:184)
    ... 40 more

这是我用来转换为字节数组的公钥纯文本

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20130520"
AAAAB3NzaC1yc2EAAAABJQAAAIEAsE8aR2CIWuQgkeOsPhHDuGo+Rokr2cT+KS5L
sJFbEkB0R3XYXnOT3DU0CFrmHUX1PpcfTOSdxCIfeSXFHCGGWEXm4qx7ptNpm4vP
Scuzmlr/fjuQdb7lBQ0+OEP2LKuRHxt5oEVZvq/EvwENS5T2BiVUSvTwXUS6SKCh
ERydjXE=
---- END SSH2 PUBLIC KEY ----

这是转换为字节数组并使用 x509 keyspec 的代码

byte[] key = Base64.encodeBase64(publicKey.getBytes());
x509KeySpec = new X509EncodedKeySpec(key);
        keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(x509KeySpec);
4

1 回答 1

0

encodeBase64编码为base 64。我认为您想解码公钥,而不是对其进行编码。当然,如果你手动解码,你应该先去掉页眉和页脚。最后,SSH 使用它自己的密钥格式,因此您应该使用读取 SSH 公钥的库,而不仅仅是用于X509KeySpec.

于 2013-05-21T01:05:08.683 回答