w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag when importing RSA public key

X509EncodedKeySpec expects DER-encoded data, not PEM-encoded. Your code will work fine if you simply discard the "BEGIN" and "END" and base64-decode the public keys contents.

final String key2 =
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdQudusozLmogBfU2LCO+WcM59"
    + "ycup9SxMsBNCku23PxrPMO6u//QjtWPz7istE9vkQfa6tQn1Or+SDxeHLMxEesF0"
    + "xiBEgFUhg7vjOF2SnFQQEADgUyizUIBBn1UgKNA8eP24Ux0P0M2aHMn78HIHsRcu"
    + "pNGUNW7p51HOVoIPJQIDAQAB";

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(
    new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(key2)));




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.