2017-01-02 56 views
0

一個DSA公鑰比方說,我有一個看起來像這樣的DSA公鑰:我做openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem裝載在Java中

產生這個關鍵

-----BEGIN PUBLIC KEY----- 
MIIBuDCCASwGByqGSM44BAEwggEfAoGBAOwYAcAzXpuw+XCXuNp5zhAzKdhrRguI 
uI5kLia8fhRb+1EnFPNpXt4fUS2c/0P0nvzH/TvApizzMkRYJea6rRSW5B+MDjv6 
lvrxv+5xBM15kdug033mgSL7wEJIrTLwbe5/djz2oe+pr1KLqs/fvgyKcQyttUWb 
5SmwZ+UVx3zfAhUAu0kA2L6VgbvEwpD9sTj5tLyB6Y0CgYEA5GjC+KsPsAH3HZKl 
2IwTjX47iNVHyuzr4ZcyXceJ/pi3WR6bQJ6tpf1I2jIE0DOMPlNUwYh0aWBGvoY2 
t4d5cwZaW90OS8IAIRFkQS0ywpmJyb7KXqRHwAYdMID88GW0d/KsVB3if0j/9QOo 
jhGOrO+kJcZBxUSxINgIIEYFAlEDgYUAAoGBALnHTAZlpoLJZuSBVtnMuRM3cSX4 
3IkE9w9FveDV1jX5mmfK7yBVpQFV8eVJfk91ERQ4Dn6ePLUv2dRIt4a0S0qHqadg 
zyoFyqkmmUi1kNLyixtRqh+m2gXx0t63HEpZDbEPppdpnlppZquVQh7TyrKSXW9M 
TzUkQjFI9UY7kZeK 
-----END PUBLIC KEY----- 

我想能夠加載這個在Java中的關鍵,但我不清楚如何。

我會用RSA做的是...我想base64將字符串解碼爲字節數組,然後將該字節數組傳遞給org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance()。此時,您可以將個人參數傳遞到java.security.spec.RSAPrivateKeySpec,並從那裏做鑰匙。

但我該如何做與DSA?據我所知,沒有org.bouncycastle.asn1.pkcs.RSAPrivateKey班。下面是我嘗試導入時出現的錯誤:

import org.bouncycastle.asn1.pkcs.DSAPublicKey; 
           ^
    symbol: class DSAPublicKey 
    location: package org.bouncycastle.asn1.pkcs 
1 error 

任何想法?

回答

1

的充氣城堡庫包含PEM閱讀器將解析你的關鍵,java.security.interfaces.DSAPublicKey

PEMReader reader = new PEMReader(new FileReader("id_dsa.pub")); 
DSAPublicKey publicKey = (DSAPublicKey) reader.readObject(); 
// What implementation are we dealing with? 
System.out.println(publicKey.getClass()); 

實現類顯然是org.bouncycastle.jce.provider.JDKDSAPublicKey

完整的示例:

import org.bouncycastle.jce.provider.BouncyCastleProvider; 
import org.bouncycastle.openssl.PEMReader; 

import java.io.StringReader; 
import java.security.Security; 
import java.security.interfaces.DSAPublicKey; 

public class DSA { 
    public static void main(String[] args) throws Exception { 
     String key = 
      "-----BEGIN PUBLIC KEY-----\n" + 
      "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAOwYAcAzXpuw+XCXuNp5zhAzKdhrRguI\n" + 
      "uI5kLia8fhRb+1EnFPNpXt4fUS2c/0P0nvzH/TvApizzMkRYJea6rRSW5B+MDjv6\n" + 
      "lvrxv+5xBM15kdug033mgSL7wEJIrTLwbe5/djz2oe+pr1KLqs/fvgyKcQyttUWb\n" + 
      "5SmwZ+UVx3zfAhUAu0kA2L6VgbvEwpD9sTj5tLyB6Y0CgYEA5GjC+KsPsAH3HZKl\n" + 
      "2IwTjX47iNVHyuzr4ZcyXceJ/pi3WR6bQJ6tpf1I2jIE0DOMPlNUwYh0aWBGvoY2\n" + 
      "t4d5cwZaW90OS8IAIRFkQS0ywpmJyb7KXqRHwAYdMID88GW0d/KsVB3if0j/9QOo\n" + 
      "jhGOrO+kJcZBxUSxINgIIEYFAlEDgYUAAoGBALnHTAZlpoLJZuSBVtnMuRM3cSX4\n" + 
      "3IkE9w9FveDV1jX5mmfK7yBVpQFV8eVJfk91ERQ4Dn6ePLUv2dRIt4a0S0qHqadg\n" + 
      "zyoFyqkmmUi1kNLyixtRqh+m2gXx0t63HEpZDbEPppdpnlppZquVQh7TyrKSXW9M\n" + 
      "TzUkQjFI9UY7kZeK\n" + 
      "-----END PUBLIC KEY-----"; 

     Security.addProvider(new BouncyCastleProvider()); 
     PEMReader reader = new PEMReader(new StringReader(key)); 
     DSAPublicKey decoded = (DSAPublicKey) reader.readObject(); 
     System.out.println(decoded.getClass()); 
    } 
} 
+0

我得到一個空指針異常。我的代碼:http://pastebin.com/pMH5F445 – neubert

+0

'----- BEGIN PUBLIC KEY -----'和'----- END PUBLIC KEY -----'分隔符是PEM格式,就像Base 64編碼一樣。只要給讀​​者提供openssl的PEM輸出即可。我用一個完整的例子更新了答案。 – teppic

+0

工作 - 謝謝! – neubert