Encryption Key Not Working in Android.

I’ve been trying to use this code to create a public key that I can use in an Android app.

[code] Dim privateKey As String
Dim publicKey As String
Dim DER As MemoryBlock

If Crypto.RSAGenerateKeyPair( 1024, privateKey, publicKey ) Then

if Crypto.RSAVerifyKey(publicKey) then
  DER = Crypto.DEREncodePublicKey(publicKey)
  publicKey = EncodeBase64(DER)
end

txtPublic.Text = "PUBLIC KEY:" + EndOfLine + (publicKey)
txtPrivate.Text = "PRIVATE KEY:" + EndOfLine + (privateKey)

end[/code]

The public key that this creates throws an invalid key exception in Android. I know it’s a long shot but is there anyone in here that could give me some advice? Thanks.

What does the android app use the key for? I suspect you’re missing the header & footer… see http://blog.xojo.com/2015/06/01/privatepublic_key_interoperability/

It will use the key to encrypt a message for a Xojo app. I haven’t got that far because I can’t get Android to accept the key that is generated by Xojo so I can try to use it. The KeyFactory as it’s called in Java can’t seem to create the public key from the Xojo format. I’m using X509EncodedKeySpec which is supposed to want DER. Although I have tried using just the RSA key encoded to base 64 and that doesn’t work either. Thanks for the reply.

I was thinking of trying a PEM key next anyways. Thanks.

@Greg O’Lone I was just advised that I need to generate a key using the SubjectPublicKeyInfo format (see RFC 5280). Can you tell me how to do that in Xojo? Thanks!

You can’t. You’ll have to do that conversion on the Android side. Something like this:

http://stackoverflow.com/questions/14052485/converting-a-public-key-in-subjectpublickeyinfo-format-to-rsapublickey-format-ja

Thanks I was looking on stackoverflow myself for a similar solution. At least I know what’s wrong now.

@Greg O’Lone I finally got the public Key generated by Xojo converted in Android into a public key it could use. I encrypted a test message with it and now Xojo is giving me an error:

Does this mean Xojo is looking for this algorithm or does it mean my ciphertext is encrypted with this algorithm?

Which algorithm combination should I be using for Crypto.RSADecrypt? Thanks!

Nevermind I got it working.

Would be nice to tell how, so other members searching the same topic would have a solution.

Really? You know I sure am tired of being scolded on the internet. If someone wanted to know how to do this all they had to do was ask. Here’s the thread from stackoverflow where I took another scolding yesterday just for asking a valid question.

http://stackoverflow.com/questions/33488365/decrypting-android-rsa-invalid-ciphertext-exception

It wasn’t a Xojo solution, it was an Android issue, but if I could have got an answer to my last post on here, about what the error meant I was getting in Xojo, it would have made it a lot easier to figure out.

[quote=226377:@Ryan Haynes]Really? You know I sure am tired of being scolded on the internet. If someone wanted to know how to do this all they had to do was ask. Here’s the thread from stackoverflow where I took another scolding yesterday just for asking a valid question.

http://stackoverflow.com/questions/33488365/decrypting-android-rsa-invalid-ciphertext-exception

It wasn’t a Xojo solution, it was an Android issue, but if I could have got an answer to my last post on here, about what the error meant I was getting in Xojo, it would have made it a lot easier to figure out.[/quote]

Aren’t we a bit unfair here ? I have seen time and again people try their best to assist you in the forum. Maybe you spent too much time in SO …

With such reactions, you are certainly not encouraging others to help you anymore.

[/quote][quote=226414:@Michel Bujardet]Aren’t we a bit unfair here ? I have seen time and again people try their best to assist you in the forum. Maybe you spent too much time in SO …

With such reactions, you are certainly not encouraging others to help you anymore.[/quote]
Are you looking for a fight or something? I’ve been posting on here since the old forums, look up my posts on there waveuponwave and I have answered many questions, and asked many questions. I posted the answer you asked me for didn’t I? I don’t recall anyone making you the forum police. That’s my only point. Nothing more. Let’s just chill out now. K?

[quote=226415:@Ryan Haynes][/quote]
Are you looking for a fight or something? I’ve been posting on here since the old forums, look up my posts on there waveuponwave and I have answered many questions, and asked many questions. I posted the answer you asked me for didn’t I? I don’t recall anyone making you the forum police. That’s my only point. Nothing more. Let’s just chill out now. K?[/quote]

No need for drama.

@Michel Bujardet

For my part in the “drama” I apologize.

[quote=226459:@Ryan Haynes]@Michel Bujardet

For my part in the “drama” I apologize.[/quote]

I am sorry you took my query the wrong way.