simple encrypt/decrypt lib in python with private key

前端 未结 5 861
[愿得一人]
[愿得一人] 2021-01-31 04:07

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

Something like:

key = \'1234\'
string =  \'hello world\'
encrypted_string = encrypt(key, st         


        
5条回答
  •  面向向阳花
    2021-01-31 04:54

    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 "", 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'
    

提交回复
热议问题