views:

50

answers:

1

hi,

is there a simple way to encrypt/decrypt a string with a key.

somthing like:

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

i couldn't find anything simple to do that.

+1  A: 

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'
aaronasterling