Android Question [Solved]Encryption Lib (RSA via OpenSSL/php) -> decryption fails in OpenSSL

KMatle

Expert
Licensed User
Longtime User
What works:

- Generate RSA keys via php & OpenSSL
- store the keys in a MySQL table
- retrieve the keys and encrypt/decrypt data

On the B4A side:

- retrieve the *same* public key (via php)
- load the key
- encrypt & send a message

Problem:

- I can't decrypt the message via openssl_private_decrypt (message is just "can't decrypt")

B4X:
mess="Hallo"                     
MessageBytes = Bconv.StringToBytes(mess, "UTF8")
                      
MessageBytesEncrypted = c.encrypt(MessageBytes, ForeignKPG.PublicKey, False)
MessageStringEncrypted=Bconv.StringFromBytes(MessageBytesEncrypted,"UTF8")
MessageStringEncrypted=su.EncodeBase64(MessageBytesEncrypted)
                      
Dim DeCryptM As HttpJob
DeCryptM.Initialize("DeCryptM", Me)
DeCryptM.Download2("http://192.168.178.21/rsa/RSASql.php", _
Array As String("Action", "DeCryptM", "Mess", MessageStringEncrypted ))

Conclusion:

I can en-/decrypt on the serverside. The server-key can be used in b4a's encryption lib. So this must be a encoding/padding thing.

Any ideas?
 

KMatle

Expert
Licensed User
Longtime User
So now I'm able to load and use both keys generated by OpenSSL into b4a. The secret is (however) that the encryption lib needs a pkcs#8 formated private key.

I can now en-/and decrypt messages (which means the keys are compatible).

Still not possible: Decrypt the message from b4a on my server (will try some things this evening).

Anyway: Any help would be nice. Must be a encoding thing :mad:
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
So now it works :D:D:D.The solution needs some explanation. If someone is interested, I will write a complete tutorial. Anyone?

This will include:

- Create the keys with a script on the server (Xampp)
- Store the keys in a MySql table
- retrieve the keys in b4a via httputils
- use both keys (for a test - private key must be kept secret)
- en-/decript messages
- send them to your server (php script)
- en-/decrypt
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top