lightweight encryption of text file

Daniel Fetchinson fetchinson at googlemail.com
Sat Jan 9 05:49:38 EST 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
>  >
>  > and the effort required to convert the file back to the original text
>  > file without the password would be equivalent to guessing the
>  > password.
>  >
>  > I'm fully aware of the security implications of this loose
>  > specification, but for my purposes this would be a good solution.
>  >
>  > What would be the simplest way to achieve this using preferably stock
>  > python without 3rd party modules? If a not too complex 3rd part
>  > module made it really simple that would be acceptable too.
>
>
>
>
> Daniel,
>
> Here's what looks like another thread veering off into package-ology,
> leaving a stumped OP behind.
>
> "Don't use a random generator for encryption purposes!" warns the
> manual, of which fact I was reminded in no uncertain terms on this forum
> a few years ago when I proposed the following little routine in response
> to a post very similar to yours. One critic challenged me to encode my
> credit card data and post it. Which I did. Upon which another critic
> conjured up the horror vision of gigahertzes hacking my pathetic little
> effort to pieces as I was reading his message. Of the well-meaning kind,
> he urged me to put an immediate stop to this foolishness. I didn't.
>
> No unplanned expenditures ensued.
>
> Or to quote ... I forget who: Fools and innovators are people who don't
> care much about what one is not supposed to do.
>
> So, take or leave what follows for what it is worth or not worth, I am
> confident it works and would serve your purpose, which, as I understand,
> is not to write a text book on cryptology.
>
> Regards
>
> Frederic
>
>
> ##############################
>
>
> import random
>
>
> def crypt_string (string, key, floor = 0, size_of_set = 255):
>
>     # key is a number. The sign of that number controls which way the
> process
>     # goes. If the number is positive, the result is an encryption. A
> negative
>     # number orders a decryption.
>
>    if key == 0: return string   # No processing
>
>    import random
>
>    MAX_CHUNK_SIZE  = 32
>    MIN_CHUNK_SIZE  = 16
>
>    def process_sequence (sequence):
>       s = ''
>       for c in sequence:
>          r = random.randint (0, size_of_set - 1)
>          s += chr (((ord (c) - floor + r * sign) % size_of_set) + floor)
>       return s
>
>    def shuffle_sequence (sequence):
>       random.shuffle (sequence)
>       return sequence
>
>    sign = (key > 0) * 2 - 1
>    random.seed (key * sign)
>
>    s = ''
>
>    if sign > 0:    # forward
>
>       i = 0
>       while i < len (string):
>          random_size_of_chunk = random.randint (MIN_CHUNK_SIZE,
> MAX_CHUNK_SIZE)
>          clear_chunk_shuffled = shuffle_sequence (list (string
> [i:i+random_size_of_chunk]))
>          code_chunk_shuffled = process_sequence (clear_chunk_shuffled)
>          s += code_chunk_shuffled
>          i += len (code_chunk_shuffled)
>
>    else:           # backward
>
>       i = 0
>       while i < len (string):
>          random_size_of_chunk = random.randint (MIN_CHUNK_SIZE,
> MAX_CHUNK_SIZE)
>          code_chunk_shuffled = list (string [i:i+random_size_of_chunk])
>          real_size_of_chunk = len (code_chunk_shuffled)
>          unshuffling_template = shuffle_sequence (range
> (real_size_of_chunk))  # 1. same ...
>          clear_chunk_shuffled = process_sequence
> (code_chunk_shuffled)         # 2. ... order
>          clear_chunk = real_size_of_chunk * [None]
>          for ii in range (real_size_of_chunk):
>             clear_chunk [unshuffling_template[ii]] =
> clear_chunk_shuffled [ii]
>          s += ''.join (clear_chunk)
>          i += real_size_of_chunk
>
>    return s
>
>
>
> def _crypt_file (in_file, out_file, key):
>
>    BUFFER_SIZE = 1024
>    while 1:
>       s = in_file.read (BUFFER_SIZE)
>       if s == '': break
>       out_file.write (crypt_string (s, key))
>
>
> def crypt_file (in_file_name, out_file_name, key):
>
>     in_file = open (in_file_name, 'rb')
>     out_file = open (out_file_name, 'wb')
>     _crypt_file (in_file, out_file, key)
>     out_file.close ()

Thanks a lot!

Your description is a good fit for my purposes, indeed I don't plan on
writing a text book on encryption :) Also, I don't plan on encrypting
credit card numbers either, all I need is that a file doesn't look
obviously full of ascii characters but something that is generic data.
And since it will not be an entire system, with lots of files of this
type, only we are talking about a single file, there is no incentive
to decipher my algorithm.

I'll take a look at your code, thanks a lot,
Daniel


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



More information about the Python-list mailing list