> From: owner-openssl-us...@openssl.org On Behalf Of Tovey, Dwight (LaserJet
R&D FW Eng.)
> Sent: Thursday, 24 January, 2013 10:55

> On Jan 23, 2013, at 3:56 PM, Dave Thompson <dthomp...@prinpay.com>
>  wrote:
> 
> > Most utilities, yes, although the library supports both. 
> > (The routines named RSAPublicKey do the specific PKCS#1 form, 
> > the routines named RSA_PUBKEY or just PUBKEY do the wrapped form.)
<snip>
> > Getting back to the original question, according to Wikipedia 
> > Python has builtin modular exponentiation on bignums as pow(x,e,m),
> > so probably all OP needs is extract the modulus and (public) exponent 
> > from the key, pad and convert the data and do that.
> 
> With the help of the comments here I have made some progress. 
>  For a proof-of-concept, I wrote a little C program that 
> passed the binary public key through the OpenSSL library 
> function d2i_RSAPublicKey() to get a RSA structure.  I could 
> then use this in a call to RSA_public_encrypt() to encrypt 
> the plaintext data to send back to the device, and the device 
> successfully accepted it.  So now I want to translate that C 
> program into python.
> 
> Dave - you mention using the pow() function in python to 
> extract the modulus and exponent.  Could you elaborate on 
> that?  Or did you mean that once I have the modulus and 
> exponent that I could use pow() in the encryption process?  
> It's been several years since I last looked at encryption 
> programming, so please excuse my lack of understanding.
> 
The latter. The significant content of the public key is 
two integers, the modulus m which is large and the public 
exponent e which is usually and here small. Given those 
two integers, raw RSA encryption is the bignum computation 
x ^ e mod m, which apparently Python builtin pow() can do.
I don't use Python myself; I'm going by the doc I found.

> I can use the M2Crypto python module (python wrapper for 
> OpenSSL) in our environment to do the encryption, but so far 
> I haven't been able to figure out how to get it to accept the 
> public key.  I may have to spend the weekend with my nose 
> buried in an encryption primer.
> 
Elsethread you confirm that giving the module the "wrapped" 
publickey worked. If you're happy using the module that's 
probably easiest -- OpenSSL already has the code.

If you want to do it actually in Python:
- get m and e from the public key (DER isn't hard to parse, 
you were already shown an example elsethread, and if Python 
doesn't have a direct way to convert bytestring to bignum 
which I'd expect it probably does you can just do some 
equivalent of x = 0; for i in 0..n-1 x = x*256+b[i]
- do whichever padding you used with RSA_public_encrypt 
(this is probably the hardest part, especially if it's OAEP) 
- take the padded data as a bignum (ditto) and do pow(data,e,m)
- take the result as a bytestring (perhaps implicitly)


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to