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
| import itertools as it
import hashlib as hl
table = ''.join(chr(x) for x in range(256))
def vi_code(table, clear, code):
'''=> <str> one coded character
args :
table <- <str> 256 characters
clear <- <str> one character
code <- <str> one character'''
return table[ ( table.index(clear) + table.index(code) ) % 256 ]
def vi_decode(table, crypted, code):
'''=> <str> one decoded character
args :
table <- <str> 256 characters
crypted <- <str> one character
code <- <str> one character'''
return table[ ( table.index(crypted) - table.index(code) ) % 256 ]
def _pwd_min_len(text_len):
'''=> <int>
args :
text_len <- <int> length of the text to cipher'''
d, m = divmod(text_len, 64)
return d+bool(m)
def _pwd_advised_len(text_len):
return _pwd_min_len(text_len) * 32
def _pwd_normalize(required_len, pwd):
return pwd * ( (required_len // len(pwd)) + 1 )
def _create_key(text_len, pwd):
if _pwd_advised_len(text_len) > len(pwd) :
pwd = _pwd_normalize(_pwd_advised_len(text_len),pwd)
hash_times = _pwd_min_len(text_len)
return ''.join(hl.sha512(pwd[i::hash_times]).digest() for i in range(hash_times))
def encode_text(text, pass1, pass2):
'''=> <str> encoded text
args :
text <- <str> clear text
pass1 <- <str> password 1
pass2 <- <str> password 2'''
table2 = chr(sum(ord(c) for c in pass2) % 256)
table2 = ''.join(vi_code(table, c, table2) for c in table)
text_len = len(text)
hash1 = _create_key(text_len, pass1)
hash2 = _create_key(text_len, pass2)
h1_cycle = it.cycle(hash1)
h2_cycle = it.cycle(hash2)
first_pass = ''.join(vi_code(table, c, h1_cycle.next()) for c in text)
second_pass = ''.join(vi_code(table2, c, h2_cycle.next()) for c in first_pass)
return second_pass
def decode_text(text, pass1, pass2):
'''=> <str> decoded text
args :
text <- <str> coded text
pass1 <- <str> password 1
pass2 <- <str> password 2'''
table2 = chr(sum(ord(c) for c in pass2) % 256)
table2 = ''.join(vi_code(table, c, table2) for c in table)
text_len = len(text)
hash1 = _create_key(text_len, pass1)
hash2 = _create_key(text_len, pass2)
h1_cycle = it.cycle(hash1)
h2_cycle = it.cycle(hash2)
second_pass = ''.join(vi_decode(table2, c, h2_cycle.next()) for c in text)
first_pass = ''.join(vi_decode(table, c, h1_cycle.next()) for c in second_pass)
return first_pass |
Partager