Other [solved]RSA ANS1 little/big endian convert

KMatle

Expert
Licensed User
Longtime User
I managed to have a B4x/OpenSSL RSA encryption (see my tutorial). To open B4x to the OpenSSL/.net world (interchangable keys) I have this situation:

I have a public key (OpenSSL pkcs8) which looks like this:

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

Inside the header/footer there is a ASN1 formated base64 string. Parsing it with a tool shows the structure and the public key's modulus/exponent:

B4X:
C:\xampp\apache\bin>openssl asn1parse -in PublicKey.pem
    0:d=0  hl=4 l= 290 cons: SEQUENCE
    4:d=1  hl=2 l=  13 cons: SEQUENCE
    6:d=2  hl=2 l=   9 prim: OBJECT            :rsaEncryption
   17:d=2  hl=2 l=   0 prim: NULL
   19:d=1  hl=4 l= 271 prim: BIT STRING

C:\xampp\apache\bin>openssl asn1parse -in publickey.pem -strparse 19
    0:d=0  hl=4 l= 266 cons: SEQUENCE
    4:d=1  hl=4 l= 257 prim: INTEGER           :B9BD35495D097B9038321FFC17F16C4E7A9231B56606533960B080369D45E66B2FE8936AF3B32935A89787A41A53474AC48E7547DADEC8A72499CB9BC9778A23B5A8E51436A47DFD3EEF1291B5E724788A4EC624A2FC2C18A3A509E7FC351199EF82AC4D97A028352197FEA55B60EDD702AC7382E223AEB7995DF66F71027CFE99A29B8AC313F46F5EA8A78FFD16862E0FE2CAAFCDF0142D4F7B32416D1336A470B73DA7CE44836EE5CE8E8BB57B330F902C0AE105A5C80034259D64C1392261EC3446EAA50EC65C8AF372C16C285FB247796431ADC8AAFFF486A2D3D8E7A05B03338FE75C26526987BA1400F6B45AEE28FF50FBBB40AFC4D8E0E578B4E21EF7
  265:d=1  hl=2 l=   3 prim: INTEGER           :010001

My problem is to convert the key data from/to little/big endian. Could someone provide some help here? I saw some examples but I don't get it. I will then post an example how to convert RSA keys (ASN1).
 

KMatle

Expert
Licensed User
Longtime User
Nice!

After conversation I go this nice ASN1-HexString:

B4X:
30820122300D06092A864886F70D01010105000382010F003082010A0282010100B0634A3A7B9EBA95ED7699D156054BB0C3CE17CF59B2A2C5EF1F5BFCCA88EE9C485370747752D1FAF845A6C3476E7C8C8C71401CD7239C5768CC1AE5BE60CAFFD5C64979C63F999FF3A41BDD6E41B8F47AC7202EB1362121F98760B072A193966EF8EFDCE37DC02E958E5B77D76D45D5B57FD490402DAB2152D723BA55033CB4E5C5006FF82C1AB6AEFAD2C44CFD6AE04DA12B4B3171838EF8C00DD7B45F5B5BF3FDE0EACBB7FFBE903B84B517D735709EE8AEE5DF8E5FF84FD3C72A7EFB63564A4ADA904B094A0777F81B8532B7F35A73D5EA1C20D6923F2B5FF89AAB27DD72EF5A44279ECC29DBEDDBACD7BEC996499FA24243E8EE1AD98DD7AEA605F01A3B0203010001

Extracting the Modulus:

B4X:
B0634A3A7B9EBA95ED7699D156054BB0C3CE17CF59B2A2C5EF1F5BFCCA88EE9C485370747752D1FAF845A6C3476E7C8C8C71401CD7239C5768CC1AE5BE60CAFFD5C64979C63F999FF3A41BDD6E41B8F47AC7202EB1362121F98760B072A193966EF8EFDCE37DC02E958E5B77D76D45D5B57FD490402DAB2152D723BA55033CB4E5C5006FF82C1AB6AEFAD2C44CFD6AE04DA12B4B3171838EF8C00DD7B45F5B5BF3FDE0EACBB7FFBE903B84B517D735709EE8AEE5DF8E5FF84FD3C72A7EFB63564A4ADA904B094A0777F81B8532B7F35A73D5EA1C20D6923F2B5FF89AAB27DD72EF5A44279ECC29DBEDDBACD7BEC996499FA24243E8EE1AD98DD7AEA605F01A3B

and the Exponent (last 6 Bytes):

B4X:
010001

After transforming it back to a byte array I was able to load the (public)key (=Modulus+Exponent).

Will write a wrapper for that. Will be useful to exchange encryption keys between all platforms :D But first I will study the ASN1 format a bit:

B4X:
30 81 9F            ;SEQUENCE (0x9f bytes = 159 bytes)
|  30 0D            ;SEQUENCE (0x0d bytes = 13 bytes)
|  |  06 09         ;OBJECT_IDENTIFIER (0x09 = 9 bytes)
|  |  2A 86 48 86   ;Hex encoding of 1.2.840.113549.1.1 = RSA
|  |  F7 0D 01 01 01
|  |  05 00         ;NULL (0 bytes)
|  03 81 8D 00      ;BIT STRING (0x8d bytes = 141 bytes)
|  |  30 81 89          ;SEQUENCE (0x89 bytes = 137 bytes)
|  |  |  02 81 81       ;INTEGER (0x81 bytes = 129 bytes)
|  |  |  00          ;leading zero of INTEGER
|  |  |  DC 67 FA
|  |  |  F4 9E F2 72 1D 45 2C B4  80 79 06 A0 94 27 50 82
|  |  |  09 DD 67 CE 57 B8 6C 4A  4F 40 9F D2 D1 69 FB 99
|  |  |  5D 85 0C 07 A1 F9 47 1B  56 16 6E F6 7F B9 CF 2A
|  |  |  58 36 37 99 29 AA 4F A8  12 E8 4F C7 82 2B 9D 72
|  |  |  2A 9C DE 6F C2 EE 12 6D  CF F0 F2 B8 C4 DD 7C 5C
|  |  |  1A C8 17 51 A9 AC DF 08  22 04 9D 2B D7 F9 4B 09
|  |  |  DE 9A EB 5C 51 1A D8 F8  F9 56 9E F8 FB 37 9B 3F
|  |  |  D3 74 65 24 0D FF 34 75  57 A4 F5 BF 55
|  |  02 03          ;INTEGER (0x03 = 3 bytes)
|  |  |  01 00 01    ;hex for 65537. see it?
 
Upvote 0
Top