Python: Two-way Alphanumeric Encryption

前端 未结 2 481
鱼传尺愫
鱼传尺愫 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:03

    How strict is the alphanumeric requirement?

    How about base64 encoding the result of your existing encryption function? You might end up with a few stray '=' padding characters, but you could trim these and calculate and handle the extra padding.

    def xor_crypt_string(plaintext, key):
        ciphertext = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(plaintext, cycle(key)))
        return ciphertext.encode('base64')
    
    def xor_decrypt_string(ciphertext, key):
        ciphertext = ciphertext.decode('base64')
        return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(ciphertext, cycle(key)))
    

提交回复
热议问题