Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple encrypt/decrypt lib in python with private key

Is there a simple way to encrypt/decrypt a string with a key?

Something like:

key = '1234' string =  'hello world' encrypted_string = encrypt(key, string) decrypt(key, encrypted_string) 

I couldn't find anything simple to do that.

like image 282
aschmid00 Avatar asked Sep 28 '10 17:09

aschmid00


People also ask

Can you encrypt and decrypt with private key?

You can only encrypt with the public key, and only decrypt with the private key. If you want to apply the private key to a message, maybe you're looking for a signature, rather than encryption? This is a different cryptographic scheme that can also use RSA keys.

Can I decrypt using private key?

Public key cryptography is a method of encrypting or signing data with two different keys and making one of the keys, the public key, available for anyone to use. The other key is known as the private key. Data encrypted with the public key can only be decrypted with the private key.


2 Answers

pyDES is a DES and Triple-DES implementation completely written in python.

Here's a simple and portable example that should be secure enough for basic string encryption needs. Just put the pyDES module in the same folder as your program and try it out:

Sender's computer

>>> from pyDES import *  # pyDes if installed from pip >>> ciphertext = triple_des('a 16 or 24 byte password').encrypt("secret message", padmode=2)  #plain-text usually needs padding, but padmode = 2 handles that automatically >>> ciphertext ')\xd8\xbfFn#EY\xcbiH\xfa\x18\xb4\xf7\xa2'  #gibberish 

Recipient's computer

>>> from pyDES import * >>> plain_text = triple_des('a 16 or 24 byte password').decrypt(')\xd8\xbfFn#EY\xcbiH\xfa\x18\xb4\xf7\xa2', padmode=2) >>> plain_text "secret message" 

You might get an error in Python3 from code of Recipient's computer

ValueError: pyDes can only work with encoded strings, not Unicode.

from pyDes import *  a = b')\xd8\xbfFn#EY\xcbiH\xfa\x18\xb4\xf7\xa2' plain_text = triple_des('a 16 or 24 byte password').decrypt(a, padmode=2) print(plain_text) 

Just Add b at the beginning of the encrypted text. For clearer code, assign it to a new variable (in this case, it's a, and decrypt a in a normal way).

like image 54
Himel Das Avatar answered Sep 20 '22 16:09

Himel Das


http://www.dlitz.net/software/pycrypto/ should do what you want.

Taken from their docs page.

>>> from Crypto.Cipher import DES >>> obj=DES.new('abcdefgh', DES.MODE_ECB) >>> plain="Guido van Rossum is a space alien." >>> len(plain) 34 >>> obj.encrypt(plain) Traceback (innermost last):   File "<stdin>", line 1, in ? ValueError: Strings for DES must be a multiple of 8 in length >>> ciph=obj.encrypt(plain+'XXXXXX') >>> ciph '\021,\343Nq\214DY\337T\342pA\372\255\311s\210\363,\300j\330\250\312\347\342I\3215w\03561\303dgb/\006' >>> obj.decrypt(ciph) 'Guido van Rossum is a space alien.XXXXXX' 
like image 39
aaronasterling Avatar answered Sep 20 '22 16:09

aaronasterling