SHA-based encryption function in Python

Bryan Olson bryanjugglercryptographer at yahoo.com
Wed Apr 24 17:01:34 EDT 2002


Paul Rubin wrote:
> I've put together an encryption function written in Python using the
> SHA module to provide a keystream in output feedback mode.  It's
> nowhere near as good as AES, but should be a big improvement over the
> rotor module.  It's at
> 
>   http://www.nightsong.com/phr/crypto/p2.py

Not so good.  The attacker only has to guess one block to break
all subsequent blocks.  For a demo, put the code below into 
the p2.py file

--Bryan


import string

def xor_strings(s1, s2):
    char_list = map(lambda x, y: chr(ord(x) ^ ord(y)), s1, s2)
    return string.join(char_list, "")


plain = ("This is Fixed Header"
        + "And this is the the real secret message.")


cipher = p2_encrypt(plain, "this is the key")
cipher = cipher[_ivlen:] # Trim off the IV

#  Figure out the first block based on knowing the
#  Fixed header.
seed = xor_strings(cipher[:20], "This is Fixed Header")


#   Now decrypt the message
for i in range(len(cipher) / 20):
    print xor_strings(seed, cipher[i * 20 : i * 20 + 20])
    seed = sha.new(seed).digest()



More information about the Python-list mailing list