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

Creating a DSA PublicKey in Java from a PEM string

使用

创建了 DSA 密钥对
ssh-keygen -t dsa

然后使用

将public关键部分转换成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)

为什么我的代码不正确?

您的密钥当前使用 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)));

试试这个

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 上面已经回答了 ;)