将JSON字符串转换为RSA公钥

时间:2017-09-05 20:32:25

标签: java json spring-security

如何将JSON字符串转换为RSA公钥

String s = "AAA";

想要将s转换为公钥

getPublicKey(
    jArray.get(i).getAsJsonObject().get("e").getAsString(), 
    jArray.get(i).getAsJsonObject().get("n").getAsString()
)


public static PublicKey getPublicKey (String modulusB64u, String exponentB64u)
    throws NoSuchAlgorithmException, InvalidKeySpecException{

    //conversion to BigInteger. I have transformed to Hex because new BigDecimal(byte) does not work for me

    byte exponentB[] = Base64.getUrlDecoder().decode(exponentB64u);
    byte modulusB[] = Base64.getUrlDecoder().decode(modulusB64u);
    BigInteger exponent = new BigInteger(toHexFromBytes(exponentB), 16);
    BigInteger modulus = new BigInteger(toHexFromBytes(modulusB), 16);

    //Build the public key
    RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PublicKey pub = factory.generatePublic(spec);
    return pub;
}

我想将JSON字符串的值转换为公钥,而不是生成公钥​​。

0 个答案:

没有答案