RSA encrypt with base64 encoded public key in Android

后端 未结 2 1780
小蘑菇
小蘑菇 2021-02-02 01:57

How to do RSA encryption of byte array with base-64 encoded public key?

After reading the couple of articles( of google search ) on how to do RSA encryption in Java, fou

2条回答
  •  广开言路
    2021-02-02 02:35

    Your base64 string is possibly an X509EncodedKeySpec. I can only guess. If so, you should base64 decode the string to obtain a byte []. Then construct an X509EncodedKeySpec from this byte []. Then create an instance of an RSA KeyFactory, and use the generatePublic() method of this KeyFactory to obtain a PublicKey. This public key can then be passed to Cipher.init().

    Note: to perform base64 decoding use either the apache commons codec, or the Harder base64 decoder.

    UPDATE March 8, 2017: In better-late-than-never news, Java 8 now includes a Base64 encoding/decoding class, java.util.Base64

提交回复
热议问题