通过PEM字符串在Java中创建DSA公钥

时间:2019-03-14 12:46:36

标签: java cryptography dsa

已使用创建了DSA密钥对

ssh-keygen -t dsa

然后使用

将公钥部分转换为pem格式
openssl dsa -in mytestkey -pubout

然后我尝试使用以下内容构造一个PublicKey:

            private static final String PUBLIC_KEY_PEM =
            "-----BEGIN PUBLIC KEY-----\n" +
                    "MIIBtzCCASsGByqGSM44BAEwggEeAoGBAJLJRIrDdiwbB6iWZTWowprJB8QfbLK7\n" +
                    "8OUCoj6Ot5nfEL5VDPcc0v1rCGQxmL+nijPQsxA0LTucBySlkj6AvGIdND6yY7Qx\n" +
                    "OZYeP6UErByzVhmDNG2MIhK7SL36O3uutOJKr86aTiDxP8/zSUO8JPZHqtr2K2Cm\n" +
                    "lY6LQpocGnZ/AhUAmA/eq9AcGaNAqbT6ebB3IE3qWKkCgYAuh3zCz9aTPJ8+D9to\n" +
                    "6xjjE6SEqKM4EDaA8kpV/363ad8kZRLRmtxTGDpIKV/k4ebb94+4o6VEFJKTUH7t\n" +
                    "7c5uj8Hnr+8scctFE85nXprpK/CmxvWIZRl0AWjz1/8hoaUX3ZcLz9QbOLk1deSS\n" +
                    "BksFgKZQgoaNVY3PPq0pjNfdCAOBhQACgYEAkDATEoqoCXMOy/LK+TOBqXWu3Tex\n" +
                    "kkBMTJxYHyhXqmBLvvkiwBX1wz0i58Zg0Qde4euHXrYwYzufBXeMZ9p3Wku7VIwD\n" +
                    "4PfefBAZhzCJ+Dh937T/2dI/PqNIObZb28I7TXD/SQfwXurKDnGdl16LHmMcZ7bS\n" +
                    "d6AFgUd73b+Obh8=\n" +
                    "-----END PUBLIC KEY-----";
                    KeyFactory.getInstance("DSA").generatePublic(new X509EncodedKeySpec(PUBLIC_KEY_PEM.getBytes()));

但是我收到以下消息:

Exception in thread "main" java.security.spec.InvalidKeySpecException: Inappropriate key specification: invalid key format
at sun.security.provider.DSAKeyFactory.engineGeneratePublic(DSAKeyFactory.java:119)
at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
at com.dave.testing.RandomTesting.generatePublicKey2(RandomTesting.java:60)

为什么我的代码不正确?

2 个答案:

答案 0 :(得分:1)

您的密钥当前使用base64编码。为了使用X509EncodedKeySpec进行读取,您需要对其进行解码。您可以为此使用任何Base64解码器,我现在使用了BouncyCastle提供的解码器。 为此,您需要将其从非Base64部分(分别为-----BEGIN PUBLIC KEY----------END PUBLIC KEY-----行中剥离)。

以下代码段可使用org.bouncycastle.util.encoders.Base64

final String PUBLIC_KEY_PEM = "MIIBtzCCASsGByqGSM44BAEwggEeAoGBAJLJRIrDdiwbB6iWZTWowprJB8QfbLK7\n"
                                        + "8OUCoj6Ot5nfEL5VDPcc0v1rCGQxmL+nijPQsxA0LTucBySlkj6AvGIdND6yY7Qx\n"
                                        + "OZYeP6UErByzVhmDNG2MIhK7SL36O3uutOJKr86aTiDxP8/zSUO8JPZHqtr2K2Cm\n"
                                        + "lY6LQpocGnZ/AhUAmA/eq9AcGaNAqbT6ebB3IE3qWKkCgYAuh3zCz9aTPJ8+D9to\n"
                                        + "6xjjE6SEqKM4EDaA8kpV/363ad8kZRLRmtxTGDpIKV/k4ebb94+4o6VEFJKTUH7t\n"
                                        + "7c5uj8Hnr+8scctFE85nXprpK/CmxvWIZRl0AWjz1/8hoaUX3ZcLz9QbOLk1deSS\n"
                                        + "BksFgKZQgoaNVY3PPq0pjNfdCAOBhQACgYEAkDATEoqoCXMOy/LK+TOBqXWu3Tex\n"
                                        + "kkBMTJxYHyhXqmBLvvkiwBX1wz0i58Zg0Qde4euHXrYwYzufBXeMZ9p3Wku7VIwD\n"
                                        + "4PfefBAZhzCJ+Dh937T/2dI/PqNIObZb28I7TXD/SQfwXurKDnGdl16LHmMcZ7bS\n"
                                        + "d6AFgUd73b+Obh8=\n";
KeyFactory.getInstance("DSA").generatePublic(new X509EncodedKeySpec(Base64.decode(PUBLIC_KEY_PEM)));

答案 1 :(得分:0)

尝试

public static DSAPublicKey getPublicKeyFromString(String key) throws IOException, GeneralSecurityException {
        String publicKeyPEM = key;
        publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", "");
        publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
        byte[] encoded = Base64.decodeBase64(publicKeyPEM);
        KeyFactory kf = KeyFactory.getInstance("DSA");
        DSAPublicKey pubKey = (DSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
        return pubKey;
    }

@Ben已经在上面回答了;)