1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""completely insecure encryption"""
from cgi import escape
import base64
def encryptandescape(plaintext, key):
ciphertext = encrypt(plaintext, key)
linelength = 70
escapedtext = ""
for linestart in range(0,len(ciphertext),linelength):
line = ciphertext[linestart:linestart+linelength-1]
escapedtext += escape(line) + "\n"
return escapedtext
def sumstr(value):
c = 0
for ch in value:
c += ord(ch) % 256
return c
def encrypt(plaintext, key):
# This is not a secure encryption function, it's just to prevent prying eyes
output = ""
key = key.lower()
m = -1
iv = sumstr(key)
for n in range(len(plaintext)):
m += 1
if m >= len(key): m = 0
a = ord(plaintext[n])
b = ord(key[m])
M, N = m + 1, n + 1
c = N * (N-M+3)
z = (a + (b*c) + iv) % 256
while z < 0:
z += 256
iv = (iv + a + z) % 256
output += chr(z)
return base64.encodestring(output).strip()
def decrypt(cryptext, key):
# This is not a secure encryption function, it's just to prevent prying eyes
output = ""
key = key.lower()
if len(cryptext) > 0:
cryptext = base64.decodestring(cryptext)
m = -1
iv = sumstr(key)
for n in range(len(cryptext)):
m += 1
if m >= len(key): m = 0
a = ord(cryptext[n])
b = ord(key[m])
M, N = m + 1, n + 1
c = N * (N-M+3)
z = (a - (b*c) - iv) % 256
while z < 0:
z += 256
iv = (iv + a + z) % 256
output += chr(z)
return output.strip()
def testencryption(value,key):
ciphertext = encrypt(value,key)
plaintext = decrypt(ciphertext,key)
return value == plaintext
def test():
if not testencryption('test','test'): return 0
if not testencryption('a long value','short'): return 0
if not testencryption('short','a long value'): return 0
return 1
if __name__ == '__main__':
if test():
print "tests passed"
else:
print "tests failed"
|