simple encrypt/decrypt lib in python with private key

前端 未结 5 859
[愿得一人]
[愿得一人] 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:42

    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"
    

提交回复
热议问题