[solved]RSA Encryption: B4J & OpenSSL: No prob, VB.net -> ??

KMatle

Expert
Licensed User
Longtime User
As you may know I've written some tutorial about using RSA in B4x & OpenSSL (php). It's all about converting the keys (adding/removing headers).

Now I took a look to VB .net. I'm able to create keys and use it but I need to convert the B4x / OpenSSL keys to exchange them. Has anyone tried this? (example would be great).
 

KMatle

Expert
Licensed User
Longtime User
Solution:

OpenSSL creates keys like this:

B4X:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx9TXkkAnS/OvEXIhFBjY
Q0jwzpPgqAexhPjZ83cQli9WfRt228tzwjpClGmy9XndIUqbU2AA0A8MWI9VRB9D
WAVu8tomnzupWCQpouhjpjRVaH8+6wgqyPvVrNDEwA6yjz8tKQgQbrmNu38ZEhhi
X7gOUzi6uhxqVhJm7oFIEIXWBE+q07i1DIyfuNFF8XLpfVjTsyBi3MSo/TG4LrmQ
jm48Kyecn20ik7n1Al4F328gKMtwoCfGHZIVvFjuoTPH76mDyebXNkIM7Hm2kjIJ
HRPhZwvQycDeOpA8yYEsgTrPxV+B89UAXDqSbR0607KNckBipIBKUq/HoOwzV2G7
EQIDAQAB
-----END PUBLIC KEY-----

In B4x we strip the headers , re-format it to a simple string and after converting it to a byte array it can be loaded and used (Encryption Library)... and vice versa

B4X:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx9TXkkAnS/OvEXIhFBjYQ0jwzpPgqAexhPjZ83cQli9WfRt228tzwjpClGmy9XndIUqbU2AA0A8MWI9VRB9DWAVu8tomnzupWCQpouhjpjRVaH8+6wgqyPvVrNDEwA6yjz8tKQgQbrmNu38ZEhhiX7gOUzi6uhxqVhJm7oFIEIXWBE+q07i1DIyfuNFF8XLpfVjTsyBi3MSo/TG4LrmQjm48Kyecn20ik7n1Al4F328gKMtwoCfGHZIVvFjuoTPH76mDyebXNkIM7Hm2kjIJHRPhZwvQycDeOpA8yYEsgTrPxV+B89UAXDqSbR0607KNckBipIBKUq/HoOwzV2G7EQIDAQAB

.net uses the RSA Modulus and Exponent directly. So how do we get it?

As you can see, we have a Base64 string here. So just convert it and you get a byte array, convert it to hex and remove the "-" between

B4X:
Dim srkB As Byte() = Convert.FromBase64String(ServerPubKeyString)
pkHex = BitConverter.ToString(srkB) '-> this converts to hex in .net
pkHex = pkHex.Replace("-", "")

Now we have:

B4X:
"30820122300D06092A864886F70D01010105000382010F003082010A0282010100C7D4D79240274BF3AF1172211418D84348F0CE93E0A807B184F8D9F37710962F567D1B76DBCB73C23A429469B2F579DD214A9B536000D00F0C588F55441F4358056EF2DA269F3BA9582429A2E863A63455687F3EEB082AC8FBD5ACD0C4C00EB28F3F2D2908106EB98DBB7F191218625FB80E5338BABA1C6A561266EE81481085D6044FAAD3B8B50C8C9FB8D145F172E97D58D3B32062DCC4A8FD31B82EB9908E6E3C2B279C9F6D2293B9F5025E05DF6F2028CB70A027C61D9215BC58EEA133C7EFA983C9E6D736420CEC79B69232091D13E1670BD0C9C0DE3A903CC9812C813ACFC55F81F3D5005C3A926D1D3AD3B28D724062A4804A52AFC7A0EC335761BB110203010001"

Dumping this with a ASN1 parser:

B4X:
SEQUENCE(2 elem)
Offset: 0
Length: 4+290
(constructed)
Value:
(2 elem)
SEQUENCE(2 elem)
OBJECT IDENTIFIER1.2.840.113549.1.1.1 = RSA
NULL
BIT STRING(1 elem)
SEQUENCE(2 elem)
INTEGER(2048 bit) 252263790273805568130579663725125793287774395741524297641345133678024…
INTEGER65537

will show the structure with 2 (large) integers:

Modulus: 252263790273805568130579..... (2048 Bit)
Exponent: 65537

Now we only have to convert the hex values back to bytes and use the key...

Right now I get all the values in a crappy code. Will post a real ASN1 parser for RSA the next weeks.
 
Top