简体   繁体   English

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

[英]How can I construct a java.security.PublicKey object from a base64 encoded string?

I have a bse64encoded string Public key from external source (Android Store) and I need to use it to verify signed content.我有一个来自外部源(Android 商店)的 bse64encoded 字符串公钥,我需要使用它来验证签名内容。 How can I convert the string into an instance of the java.security.PublicKey interface.如何将字符串转换为 java.security.PublicKey 接口的实例。 I am on Java 6 if that makes a difference.如果这有所作为,我将使用 Java 6。

The key is (probably) generated using standard java lib and not bouncy castle (its from a remote team so I am not sure).密钥(可能)是使用标准 java lib 生成的,而不是充气城堡(它来自远程团队,所以我不确定)。 Their sample code says to use Security.generatePublicKey(base64EncodedPublicKey);他们的示例代码说使用 Security.generatePublicKey(base64EncodedPublicKey); but the Security object in standard java has no such method.但是标准 java 中的 Security 对象没有这样的方法。

Code for the above answer上面答案的代码

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;
}

Ok for grins ... try this好的咧嘴笑......试试这个

  • base64 decode the key data to get a byte array (byte[]) base64解码关键数据得到一个字节数组(byte[])
  • Create a new X509EncodedKeySpec using the byte array使用字节数组创建一个新的 X509EncodedKeySpec
  • Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here使用 KeyFactory.getInstance("RSA") 获取 KeyFactory 的实例,假设 RSA 在这里
  • call the method generatePublic(KeySpec) with the X509EncodedKeySpec使用 X509EncodedKeySpec 调用方法 generatePublic(KeySpec)
  • Result /should/ be a public key for your usage.结果 /should/ 是您使用的公钥。

Try this....试试这个....

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

Using spongy castle使用海绵城堡

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;
}

you can try this solution:你可以试试这个解决方案:

add this dependency:添加此依赖项:

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

and use this method:并使用此方法:

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);
}

Here's a code snippet:这是一个代码片段:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM