Python: Two-way Alphanumeric Encryption

前端 未结 2 436
鱼传尺愫
鱼传尺愫 2021-02-04 08:53

I am using Python 2.7. I have an alphanumeric string, on which I want to perform a encryption/decryption. Whatever I do should remain 2-way and the result shoul

2条回答
  •  温柔的废话
    2021-02-04 09:22

    If you want serious encryption (read unbreakable) then I'd use AES from pycrypto something like this.

    >>> from Crypto.Cipher import AES
    >>> from Crypto import Random
    >>> key = b'Sixteen byte key'
    >>> iv = Random.new().read(AES.block_size)
    >>> cipher = AES.new(key, AES.MODE_CFB, iv)
    >>> msg = iv + cipher.encrypt(b'Attack at dawn')
    >>> msg.encode("hex")
    'e10e096aabff9db382abe8d704404995a7b64d72a4e1b9e5208912d206c4'
    

    That is your ascii message. Now decode the message like this

    >>> recv='e10e096aabff9db382abe8d704404995a7b64d72a4e1b9e5208912d206c4'
    >>> cipher.decrypt(recv.decode("hex"))[len(iv):]
    'Attack at dawn'
    >>> 
    

    Any encryption method you make up yourself will be easily breakable by an expert and the one you've shown above falls into that category.

提交回复
热议问题