M2Crypto - import keys from a non-standard file?

I have a file with a public metric and a module in it. They are not in pem or xml or der format, they are just the values โ€‹โ€‹recorded at their offsets.

How can I make a public key of them with M2Crypto? I also have a private key in the same format. I managed to use the code that someone posted here on Stackoverflow to create a PEM file with php, but this seems like a very funny way to do it.

This is also not a one-time thing, I need to be able to read the public indicator and module from files in this format in order to verify the signature.

0
source share
1 answer

: http://blog.oddbit.com/2011/05/09/signing-data-with-ssh-agent/

e - Python long . n - Python long .

, , :

import M2Crypto
key = M2Crypto.RSA.new_pub_key((
    M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hex(e)[2:])),
    M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hex(n)[2:])),
    ))

hex 0xA45E, 0x.

, . :

import M2Crypto
from binascii import hexlify 
e = f.read(4)
n = f.read(0x80)
key = M2Crypto.RSA.new_pub_key((
    M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hexlify(e))),
    M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hexlify(n))),
    ))

!

new_pub_key, ,

OpenSSL MPINT - 4- - -,

, , ( ) 00010001 000003010001. , , . 0x00. , .

edit: , .

, . ( ) , , , .

:

Unformatted:
\x23\x24\x25\x26
Formatted:
\x00\x00\x00\x04\x23\x24\x25\x26
Explanation:
String left as is and count of bytes packed in

Unformatted:
\x00\x23\x55\x35
Formatted:
\x00\x00\x00\x03\x23\x55\x35
Explanation:
leading zero byte removed, byte count now 3

Unformatted:
\x80\x43\x55\x27
Formatted:
\x00\x00\x00\x05\x00\x80\x43\x55\x27
Explanation:
leading zero byte added because \x80 is negative

Unformatted:
\x00\xff\x43\x23
Formatted:
\x00\x00\x00\x04\x00\xff\x43\x23
Explanation:
Leading zero byte left because \xff is negative

Unformatted:
\x23\x53\66\x00
Formatted:
\x00\x00\x00\x04\x23\x53\66\x00
Explanation:
Trailing zero byte left in string
+1

Source: https://habr.com/ru/post/1781147/


All Articles