python function for retrieving key and encryption

J

jayshree

M2Crypto package not showing the 'recipient_public_key.pem' file at
linux terminal .how do i get/connect with recipient public key.

exactly i need to check how can i open this file through linux
commands.

import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open
('recipient_public_key.pem','rb').read()) print recip; plaintext =
whatever i need to encrypt msg = recip.public_encrypt
(plaintext,RSA.pkcs1_padding) print msg;

after calling the function its not giving any output and even any
error

i also tried as 'Will' said

pk = open('public_key.pem','rb').read() print pk; rsa =
M2Crypto.RSA.load_pub_key(pk)

whats the mistake i am not getting .will somebody help me out.
is something wrong in opening 'recipient_public_key.pem'. is M2Crypto
contain this file inbuilt .from where this file taking public key of
the recipient i'. what this file contain and how it will work .should
i need to create such a file for my purpose.if it is then how can i
create this and how it will retrieve the key,where i recognize my
recipient to get his public key .is something like database at server.

please give me a quick response......
looking for your answer.
 
P

Piet van Oostrum

jayshree said:
j> M2Crypto package not showing the 'recipient_public_key.pem' file at
j> linux terminal .how do i get/connect with recipient public key.
j> exactly i need to check how can i open this file through linux
j> commands.
j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open
j> ('recipient_public_key.pem','rb').read()) print recip; plaintext =
j> whatever i need to encrypt msg = recip.public_encrypt
j> (plaintext,RSA.pkcs1_padding) print msg;
j> after calling the function its not giving any output and even any
j> error
j> i also tried as 'Will' said
j> pk = open('public_key.pem','rb').read() print pk; rsa =
j> M2Crypto.RSA.load_pub_key(pk)
j> whats the mistake i am not getting .will somebody help me out.
j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto
j> contain this file inbuilt .from where this file taking public key of
j> the recipient i'. what this file contain and how it will work .should
j> i need to create such a file for my purpose.if it is then how can i
j> create this and how it will retrieve the key,where i recognize my
j> recipient to get his public key .is something like database at server.
j> please give me a quick response......
j> looking for your answer.

Please:
1. Type your python code with newlines and proper indentation.
2. Show the error messages that your code gives when you run it.
3. Use proper capital letters at the beginning of your sentences.
4. Don't fire so many questions in rapid succession.

The recipient_public_key.pem file is the public key of the recipient
which means the person that is going to receive the encrypted message.
You should get it from the recipient him/herself or from some key store
where s/he has deposited it.
 
J

jayshree

j> M2Crypto package not showing the 'recipient_public_key.pem' file at
j> linux terminal .how do i get/connect with recipient public key.
j> exactly i need to check how can i open this file through linux
j> commands.
j> import M2Crypto def encrypt(): recip = M2Crypto.RSA.load_pub_key(open
j> ('recipient_public_key.pem','rb').read()) print recip; plaintext =
j> whatever i need to encrypt msg = recip.public_encrypt
j> (plaintext,RSA.pkcs1_padding) print msg;
j> after calling the function its not giving any output and even any
j> error
j> i also tried as 'Will' said
j> pk = open('public_key.pem','rb').read() print pk; rsa =
j> M2Crypto.RSA.load_pub_key(pk)
j> whats the mistake i am not getting .will somebody help me out.
j> is something wrong in opening 'recipient_public_key.pem'. is M2Crypto
j> contain this file inbuilt .from where this file taking public key of
j> the recipient i'. what this file contain and how it will work .should
j> i need to create such a file for my purpose.if it is then how can i
j> create this  and how it will retrieve the key,where i recognize my
j> recipient to get his public key .is something like database at server..
j> please give me a quick response......
j> looking for your answer.

Please:
1. Type your python code with newlines and proper indentation.
2. Show the error messages that your code gives when you run it.
3. Use proper capital letters at the beginning of your sentences.
4. Don't fire so many questions in rapid succession.

The recipient_public_key.pem file is the public key of the recipient
which means the person that is going to receive the encrypted message.
You should get it from the recipient him/herself or from some key store
where s/he has deposited it.
--
Piet van Oostrum <[email protected]>
URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
Private email: (e-mail address removed)- Hide quoted text -

- Show quoted text -
Here is the code that i used

import M2Crypto
from M2Crypto import RSA

def encrypt():
recip = M2Crypto.RSA.load_pub_key(open
('recipient_public_key.pem','rb').read())
#recip = M2Crypto.RSA.load_pub_key('recipient_public_key.pem')
print recip;
plaintext =" whatever i need to encrypt"
msg = recip.public_encrypt(plaintext,RSA.pkcs1_padding)
print msg;


encrypt()


error coming like - IOError: [Errno 2] No such file or directory:
'recipient_public_key.pem'

Is this not the inbuilt file.
How should i create such type of file.
 
P

Piet van Oostrum

jayshree said:
[...]

j> error coming like - IOError: [Errno 2] No such file or directory:
j> 'recipient_public_key.pem'
j> Is this not the inbuilt file.
j> How should i create such type of file.

You don't create it. See above. If you understand what is is you know
why it can't be builtin. If you don't understand it is better if you
first learn about OpenSSL, otherwise you run the risk to make serious
errors.

If you are just experimenting to create a message for yourself then you
have to create the public key because you are then the recipient
yourself. That has been answered on
http://stackoverflow.com/questions/1169798/m2crypto-package
 
J

jayshree

jayshree <[email protected]> (j) wrote:
The recipient_public_key.pem file is the public key of the recipient
which means the person that is going to receive the encrypted message..
You should get it from the recipient him/herself or from some key store
where s/he has deposited it.
[...]

j> error coming like - IOError: [Errno 2] No such file or directory:
j> 'recipient_public_key.pem'
j> Is this not the inbuilt file.
j> How should i create such type of file.

You don't create it. See above. If you understand what is is you know
why it can't be builtin. If you don't understand it is better if you
first learn about OpenSSL, otherwise you run the risk to make serious
errors.

If you are just experimenting to create a message for yourself then you
have to create the public key because you are then the recipient
yourself. That has been answered onhttp://stackoverflow.com/questions/1169798/m2crypto-package

import M2Crypto
from M2Crypto import RSA,SSL
def encrypt():
pk = open('my_key.public.pem', 'rb').read()
rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub
object.
plaintext = 4545479545655576767767686688782344
msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding)
print msg;
encrypt()

This is code i am refering.
The Problem is coming with .pem file.
I also asked this questions at http://stackoverflow.com/questions/1176864/problem-with-the-pem-file-closed
which has not been answered .
please help me out....

thanks
 
P

Piet van Oostrum

jayshree said:
j> import M2Crypto
j> from M2Crypto import RSA,SSL
j> def encrypt():
j> pk = open('my_key.public.pem', 'rb').read()
j> rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub
j> object.
j> plaintext = 4545479545655576767767686688782344
j> msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding)
j> print msg;
j> encrypt()
j> This is code i am refering.
j> The Problem is coming with .pem file.

You never tell what the problem is. Which error message do you get? Why
can't you just copy the error messages in your post instead of letting
us guess what is happening?

There are indeed problems with the code above. One warning: If you are
going to write cryptographic code while you really don't understand the
subject enough there is a big chance that your programs will have
security problems.

One more question: Do you have now a valid my_key.public.pem file? And
is it in the same directory where you run this program?

Here are the problems in your code (and there may be more!)

import M2Crypto
from M2Crypto import RSA,SSL

You never use the imports from the line above so you can leave it out.
SSL isn't used at all, and RSA is used as M2Crypto.RSA so it uses the
first import. Cleanliness is next to godliness.

def encrypt():
pk = open('my_key.public.pem', 'rb').read()
rsa = M2Crypto.RSA.load_pub_key(pk) #return a M2Crypto.RSA.RSA_pub object.

load_pub_key requires a file name, not the contents of the file.
So use rsa = M2Crypto.RSA.load_pub_key('my_key.public.pem') and leave
the open line out.

plaintext = 4545479545655576767767686688782344

That's not text, it is a number. rsa.public_encrypt will not accept a
number. So you would have to use
plaintext = "4545479545655576767767686688782344"
or convert the number to a byte array.

msg = rsa.public_encrypt(plaintext,RSA.pkcs1_padding)
print msg;
encrypt()

The above line should be shifted to the left. It is not part of the
function otherwise you get an endless recursion.
And to answer another question from stackoverflow.com: RSA.pkcs1_padding
is a good parameter, if the decryption side will also use it.

See also my posting on the subject `RSA cryptography between Python and Java'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top