A simple string xoring program in Python

In cryptography, the simple XOR cipher is a type of additive cipher.
As the XOR operation is common in programming languages. It's pretty simple to implement it and it's also less expensive on computers.

The cipher is sometimes used for hiding information or used in cases where no particular security is required.

But, before we dive into code keep in mind that this cypher can easily be crack using a frequency analysis . But wait !, this is easy because most of the time they use the same key for xoring and if we simply generates random keys its actually pretty hard than to crack it.

So, below is the implementation of xoring in python 3.

#!/usr/bin/env python3

from os import urandom as keygen

def xor_str(data, key):
    return "".join(chr(ord(a) ^ b) for a, b in zip(data, key))

def run():
    # Note: string in Python 2.x is a bytestring
    msg = input("Enter your message: ")
    key = keygen(len(msg))

    xored = xor_str(msg, key)
    # this is how you can decrypt the data
    un_xored = xor_str(xored, key)

    # Verify that data
    if un_xored == msg:

if __name__ == "__main__":

No comments :

Post a Comment