blob: 69ee7b94337f707c4e2d8006474b79700bb3921e (
plain)
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
|
# ------------------------------------------------------------------------------
#
# SHA-512-BASED FEISTEL CIPHER
# by Toni Mattis
#
# Feistel Function: SHA-512(Block || Key)
# Key Size: Fully Dynamic
# Block Size: 1024 Bits
# Rounds: User-Specified
#
# ------------------------------------------------------------------------------
from hashlib import sha512
BPOS = tuple(range(64))
def enc_block(block, key, rounds = 16):
x = block[:64]
y = block[64:]
for i in xrange(rounds):
h = sha512(x + key).digest()
y = ''.join([chr(ord(y[k]) ^ ord(h[k])) for k in BPOS])
h = sha512(y + key).digest()
x = ''.join([chr(ord(x[k]) ^ ord(h[k])) for k in BPOS])
return x + y
def dec_block(block, key, rounds = 16):
x = block[:64]
y = block[64:]
for i in xrange(rounds):
h = sha512(y + key).digest()
x = ''.join([chr(ord(x[k]) ^ ord(h[k])) for k in BPOS])
h = sha512(x + key).digest()
y = ''.join([chr(ord(y[k]) ^ ord(h[k])) for k in BPOS])
return x + y
|