如何从base64编码的字符串构造java.security.PublicKey对象?

时间:2012-06-05 15:56:19

标签: java security rsa

我有来自外部源(Android Store)的bse64encoded字符串公钥,我需要使用它来验证签名内容。如何将字符串转换为java.security.PublicKey接口的实例。如果这有所不同,我在Java 6上。

关键是(可能)使用标准的java lib生成而不是弹性城堡(它来自远程团队所以我不确定)。他们的示例代码说使用Security.generatePublicKey(base64EncodedPublicKey);但是标准java中的Security对象没有这样的方法。

6 个答案:

答案 0 :(得分:43)

上述答案的代码

public static PublicKey getKey(String key){
    try{
        byte[] byteKey = Base64.decode(key.getBytes(), Base64.DEFAULT);
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");

        return kf.generatePublic(X509publicKey);
    }
    catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

答案 1 :(得分:22)

好笑咧嘴笑......试试这个

  • base64解码密钥数据以获取字节数组(byte [])
  • 使用字节数组
  • 创建新的X509EncodedKeySpec
  • 使用KeyFactory.getInstance(“RSA”)获取KeyFactory实例,假设此处为RSA
  • 使用X509EncodedKeySpec
  • 调用方法generatePublic(KeySpec)
  • 结果/应该/是您使用的公钥。

答案 2 :(得分:1)

试试这个......

PublicKey getPublicKey(byte[] encodedKey) throws NoSuchAlgorithmException, InvalidKeySpecException
{
    KeyFactory factory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(encodedKey);
    return factory.generatePublic(encodedKeySpec);
}

答案 3 :(得分:0)

使用海绵城堡

public static PublicKey getPublicKeyFromString(String key) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
    org.spongycastle.asn1.pkcs.RSAPublicKey pkcs1PublicKey
            = org.spongycastle.asn1.pkcs.RSAPublicKey.getInstance(decodeB64(key));
    RSAPublicKeySpec keySpec
            = new RSAPublicKeySpec(pkcs1PublicKey.getModulus(), pkcs1PublicKey.getPublicExponent());
    PublicKey publicKey = keyFactory.generatePublic(keySpec);
    return publicKey;
}

答案 4 :(得分:0)

您可以尝试以下解决方案:

添加此依赖项:

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.61</version>

并使用此方法:

private Key parsePublicKey(String publicKey) throws IOException {
    PEMParser pemParser = new PEMParser(new StringReader(publicKey));
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemParser.readObject());
    return converter.getPublicKey(publicKeyInfo);
}

答案 5 :(得分:0)

这是一个代码片段:

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;

import org.apache.commons.codec.binary.Base64;
try {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    BigInteger modulus = new BigInteger(1, Base64.decodeBase64(this.stringValue("n")));
    BigInteger exponent = new BigInteger(1, Base64.decodeBase64(this.stringValue("e")));
    return kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (InvalidKeySpecException var4) {
    throw new InvalidPublicKeyException("Invalid public key", var4);
} catch (NoSuchAlgorithmException var5) {
    throw new InvalidPublicKeyException("Invalid algorithm to generate key", var5);
}