Converts a string public key to an RSA public key… here is a solution to the problem.
Converts a string public key to an RSA public key
I’m trying to convert a string public key to a public key with modulus and exponential.
But why doesn’t it work?
Here 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);
Is there a problem with my code?
Solution
Maybe you mean your code is:
byte[]decode = Base64.decode(keyBytes, Base64.DEFAULT);
Change encode
to decode
.