Encrypt string in Python

后端 未结 6 999
孤街浪徒
孤街浪徒 2021-01-06 11:29

I need to encrypt a small string in Python. Is it possible to use a secret key to encrypt the string? Is there a good way to do this and achieve a reasonable encryption leve

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 12:06

    I would do this with the easy to use cryptocode library (https://github.com/gdavid7/cryptocode).

    Encrypting and decrypting is relatively simple compared to other libraries:

    ## Encrypting:
    >>> import cryptocode
    >>> myEncryptedMessage = cryptocode.encrypt("I like trains", "password123")
    >>> print(myEncryptedMessage)
    M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==
    
    ## Decrypting:
    >>> import cryptocode
    >>> myDecryptedMessage = cryptocode.decrypt("M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==", "password123")
    >>> print(myDecryptedMessage)
    I like trains
    

    The only large disadvantage to this library over others is that it does not have many options to change around your string compared to other cryptography libraries. But it is perfect for a lot of people who just want an abstraction, and don't need to configure custom settings.

提交回复
热议问题