Other RSA En-/Decryption Private & Public Key: Example, please...


Licensed User
I've developed apps using Encryption library with DES which communicate with other apps via php/MySql. No problem.

What I need is a working b4a example with:

- Generate public & private key (RSA 2048/4096)
- encrypt (via public key)
- decrypt (via private key)

I only found some threads but not an example. Could someone push me in the right direction? Thanks.


Licensed User
Ok. I found some good sources on the www and I will develop and present something for b4a + php the next days. My goal is to create/handle the keys by code (creating and handling keys during runtime to communicate with different instances/apps).
  • Like
Reactions: udg
Upvote 0


Licensed User
Well... It's getting closer...

What I got so far is:

1. an working b4a example generating RSA-Keys (public and private), en- and decrypting of data is working well -> = Agrahams example
2. a batch cmd to generate the Keys via OpenSSL on the server
3. a working php script which get's the once generated keys, en- and decrypting of data is working well

What I need is how do I convert, handle and exchange the keys (from/to) b4a <-> server and in which format? I have some apps running using DES encryption with php. No problem at all but RSA (only the keys and how to get the "right" "string" to use) makes me mad.

So what do I need on both sides?

The Key it'sself (of course)
What "part" of it?
Base64 encoding? (think so)
UTF8? (think so).

@agraham : Could you please help here? (Anyone else is welcome)
Upvote 0


Licensed User
Update (on both sides the encryption decryption works like a charm, but I'm not able to use the opposites keys)

From my RSA-Server I get this public key (via httputils2):

-----BEGIN PUBLIC KEY-----              
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCt5Zekgbs34Xu+ouPIFdUMFfiqmpmDevjxmf35NspvDeaREemvZvcFc+PxSUR1Cg4Slam9m8CD2jWk7yrf09BaDVynpPkHbscGBLE5gdU+KJF3E64XwZwrMpUXxgmCRBee8wHeYVNH7clct5ZjQjEziv8MGUkrsGa2QR8MBWwl7wIDAQAB-----END PUBLIC KEY-----

In b4a I try to load the key via:

Dim kpg As KeyPairGenerator
                Dim c As Cipher
                Dim SPubKey(0) As Byte
                kpg.Initialize("RSA", 1024) '

"res" is the public key from the server as a string. I tried to convert it to Base64 (in my php script) but that did not work, too.

The keys (Server) were generated with:

openssl genrsa -out MyPrivateKey.pem 1024


java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long

So: How do I load/use the key?
Last edited:
Upvote 0


Well-Known Member
Licensed User
Erel can you help here, this will solve SSL issue to many people here, since HTTPS is not working with the SNI web sites

What we need is:
Match (Byte/Structure) OpenSSL Public Key with B4A Encryption Library Public Key.
Upvote 0