XOR encryption

I recently needed a quick & easy encryption scheme. I wanted it to be super portable, and I wanted to spend as little time as possible putting it together. So I threw together a little utility called XCRYPT, in python, that just does an XOR against a source string and Base-64 encodes the result. Or visa-versa to decrypt it.

I was going to replicate it in Java, but ran out of time and interest.

It just does a fast XOR against a random string to encrypt and decrypt the bytes. I don’t think I put in logic to roll around the end of the random string, so you’re limited to short strings — but that’s all I needed it for. The security is only as good as you are able to secure the random string, but the little encrypted snippets should be almost (emphasis on “almost”) as secure as one-time-pad encryption, provided you don’t let the random key string get out.

I don’t think it’s particularly useful, with all the standard encryption libraries around, but it was fun to write.

from random import getrandbits, randint
from base64 import standard_b64encode, standard_b64decode
from itertools import izip
import struct
import array
import sys

def _generate_keys() :
    ''' generate a new keys string '''
    bits = (getrandbits(8) for x in xrange(0,57 * 101))
    a = array.array('B',bits)
    encoded = standard_b64encode(a).strip()
    return encoded
def generate_keys_file():
    ''' embed the generated keys string into a python statement '''
    keys = _generate_keys()
    print 'KEYS_B64="%s"' %keys

def _xor(string,keys,key):
    ''' xor the string against the KEY string, using key as the offset '''
    z = (chr(ord(x) ^ ord(y)) for (x,y) in izip(string,keys[key:]))
    return ''.join(z)
def xcrypt(string, key):
    ''' run an xor on the string using the key.
    If the key is None, create one and base64 encode the result.
    If the key is provided, base64 decode the string and use the key. '''

    # you have to create a key string and put it in xcrypt_keys.py first
    import xcrypt_keys
    keys = standard_b64decode(xcrypt_keys.KEYS_B64)

    if key is None :
        key = randint(0,len(keys) - len(string) - 1)
        string = standard_b64decode(string).strip()
    return xored,key

if __name__ == '__main__' :
    ''' main function '''
    if len(sys.argv) > 1 and 'generate' == sys.argv[1] :
        if len(sys.argv) > 1 :
            input = sys.argv[1]
        else :
            input = sys.stdin.read()
        a,k = xcrypt(input,None)
        b,k = xcrypt(a,k)
        print k,a,b


Leave a comment

Filed under utility

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s