1

我正在尝试将公钥的字符串表示形式转换为用于加密数据的 publicKey java 对象,但我不断收到以下错误:

   java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF
    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:96)
    at com.josh.crypto.CryptoUtil.encryptData(CryptoUtil.java:78)
    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: IOException: Detect premature EOF
    at sun.security.x509.X509Key.decode(X509Key.java:380)
    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

这是代码片段

private PublicKey convertKeytoKeyObject(String publicKey)
            throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
        byte[] key = Base64.encodeBase64(publicKey.getBytes());
        x509KeySpec = new X509EncodedKeySpec(key);
        keyFactory = KeyFactory.getInstance(Keys.RSA);
        return keyFactory.generatePublic(x509KeySpec);

}

调用 generatePublic 方法时出现错误。

我正在使用的关键

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

1023 37 76983149213904336854114697871156413897597333013960300274862161645944637611303244321149406756964434094764396730933825927553585120754675001147670015123442685209165940390638704889262818745065195784329243512386761389890962501775294435871155079992876425567019740053306097979712960040526841676998224241040015724649 rsa-key-20130520
4

1 回答 1

0

密钥文件看起来不错。如果您将整个文件作为字符串提供给您的方法,那么该方法将尝试对已经是 Base64 编码的密钥进行编码。

您可以像这样创建(和使用)密钥:

SecureRandom rand = new SecureRandom();
KeyPairGenerator gen = null;
try {
    gen = KeyPairGenerator.getInstance( "DSA" );
    }
catch ( NoSuchAlgorithmException e ) {
    System.err.println( e.getMessage());
}
gen.initialize ( 1024, rand );
KeyPair keys = gen.generateKeyPair ();

//----- Write keys into export files
PublicKey pubkey = keys.getPublic ();
writeKeyToFile ( pubkey, FILE_PUBLIC_KEY );
     System.out.println ( pubkey.toString ());
PrivateKey privkey = keys.getPrivate ();
writeKeyToFile ( privkey, FILE_PRIVATE_KEY );
于 2013-05-22T21:47:16.803 回答