lightweight encryption of text file

Robert Kern robert.kern at gmail.com
Fri Jan 8 17:09:27 EST 2010


On 2010-01-08 15:47 PM, Daniel Fetchinson wrote:
>>>> 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 party
>>>> module made it really simple that would be acceptable too.
>>>
>>> Paul Rubin's p3.py algorithm is probably the most straightforward way to
>>> meet
>>> these requirements. It's not a standard crypto algorithm by any means,
>>> but
>>> Paul
>>> knows his stuff and has devised it with these deployment restrictions in
>>> mind.
>>>
>>>     http://www.nightsong.com/phr/crypto/p3.py
>>
>> Thanks a lot, currently I'm having trouble using this code on python
>> 2.6 but probably some small tweaking will fix it.
>
> Actually, it also doesn't work with python 2.5 and currently I don't
> have access to anything older. array.array raises a
>
> ValueError: string length not a multiple of item size
>
> Does anyone recall a change to array.array?
>
> The full traceback is
>
> Traceback (most recent call last):
>    File "p3.py", line 163, in<module>
>      _test()
>    File "p3.py", line 143, in _test
>      c1 = e(plain,key)
>    File "p3.py", line 69, in p3_encrypt
>      xkey = _expand_key(k_enc, n+4)
>    File "p3.py", line 41, in _expand_key
>      return array ('L', j)
> ValueError: string length not a multiple of item size

Are you on a 64-bit platform? Unfortunately, array's integer typecodes are 
platform-specific, but p3.py requires a 32-bit integer and was written on a 
32-bit platform. It's reasonably straightforward to fix. Put this bit of 
(untested) code at the top of the file and replace occurrences of 'L' with uint32:

# Find the typecode of a 32-bit unsigned integer.
for typecode in 'IL':
     if len(array(typecode, [0]).tostring()) == 4:
         uint32 = typecode
         break
else:
     raise RuntimeError("Neither 'I' nor 'L' are unsigned 32-bit integers.")

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list