Sub CryptWithPublicKey(input As String) As String
Dim cipher_ As Cipher
Dim key_ As KeyPairGenerator
Dim msg(0) As Byte
Dim Bconv As ByteConverter
key_.Initialize("RSA",128)
key_.PublicKeyFromBytes(Main.PUBLIC_KEY) ' Here's the exception
cipher_.Initialize("RSA")
msg = cipher_.Encrypt(input.GetBytes("UTF8"),key_,False)
Return Bconv.StringFromBytes(msg,"UTF8")
End Sub
The exception is :
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
@Klaus Matle : still not working, i'm testing it in Java right now with code :
B4X:
InputStream instream = new FileInputStream(filePath); // Opening the certificate
byte[] encodedKey = new byte[instream.available()];
instream.read(encodedKey); // Getting bytes from it
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pkPublic = kf.generatePublic(publicKeySpec); // Finally get our Public Key
Finally I think I found the answer. The key was in wrong format. Right is DER but the one which I tested before wasn't working, and with another DER file it works. The code I post just before is right.