lightweight encryption of text file

Daniel Fetchinson fetchinson at googlemail.com
Sat Jan 9 20:01:33 CET 2010


>> I have a plain text file which I would like to protect in a very
>> simple minded, yet for my purposes sufficient, way. I'd like to
>> encrypt/convert it into a binary file in such a way that possession of
>> a password allows anyone to convert it back into the original text
>> file while not possessing the password one would only see the
>> following with the standard linux utility 'file':
>>
>> [fetchinson at fetch ~]$ file encrypted.data
>> encrypted.data: data
> [...]
>
> This is probably not what you want, but it is very simple and doesn't
> import any module:) I am not qualified to say how easy it is to discover
> the message without the password.
>
> def str2int(txt):
>     return reduce(lambda n, c: n*255 + ord(c), txt, 0)
>
> def int2str(n):
>     chars = []
>     while n:
>         n, o = divmod(n, 255)
>         chars.append(chr(o))
>     return ''.join(reversed(chars))
>
> def encrypt(txt, pwd):
>     return int2str(str2int(txt)*str2int(pwd))
>
> def decrypt(txt, pwd):
>     return int2str(str2int(txt)/str2int(pwd))
>
> def test(txt, pwd):
>     encrypted_txt = encrypt(txt, pwd)
>     decrypted_txt = decrypt(encrypted_txt, pwd)
>     print "text:%r" % txt
>     print "encrypted:%r" % encrypted_txt
>     print "decrypted:%r" % decrypted_txt
>
>
>>>> test("This encryption scheme is definitely unbreakable.", "secret")
> text:'This encryption scheme is definitely unbreakable.'
> encrypted:'&2\xa5\xd4\x17i+E\x01k\xfa\x94\xf80\xa8\x8f\xea.w\x128\xf1\xd9\x0f9\xf2t\xc9\r`\x90%\xd6\xf3~\x1f\x00%u&\x8a\xe4\xe0\xa7\xb8\xb0ec)S>\xcb\xf2>\xec'
> decrypted:'This encryption scheme is definitely unbreakable.'

Thanks, this looks pretty simple too, I will go with either Steven's
or with your solution.

Cheers,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list