Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert String publickey to RSA publickey

Tags:

java

android

rsa

I'm trying to convert string public key to publickey with modulus and exponent. but how come it doesnt works?

this is my code

     public void toPubKey(String filename,String sms) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, IOException{
    byte[]keyBytes=sms.getBytes();
    byte[]decode = Base64.encode(keyBytes, Base64.DEFAULT);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decode);
    PublicKey pubKey2 = (PublicKey)fact.generatePublic(x509KeySpec);
    saveToFile(filename,pubKey2);

any problem with my code?

like image 459
androidBeginer Avatar asked Dec 30 '25 18:12

androidBeginer


1 Answers

Perhaps you meant your code to be:

byte[]decode = Base64.decode(keyBytes, Base64.DEFAULT);

Change encode to decode.

like image 93
Jason Clawson Avatar answered Jan 02 '26 06:01

Jason Clawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!