2021-09-20 11:32:55 +02:00
|
|
|
import hashlib
|
|
|
|
import math
|
2021-09-21 09:14:31 +02:00
|
|
|
import os
|
|
|
|
import random
|
2021-09-20 11:32:55 +02:00
|
|
|
|
|
|
|
# Shark is a sha256+xor based encryption.
|
|
|
|
# I made it because I want to try to break it.
|
|
|
|
# (Precisely: Show it does not provide semantic security, because it is not IND-CPA-secure)
|
|
|
|
# This will work iff I succeed in building a PPT-discriminator for sha256 from randomness
|
|
|
|
# As my first approach this discriminator will be based on an LSTM-network.
|
|
|
|
|
2021-09-21 09:14:31 +02:00
|
|
|
bs = int(256/8)
|
|
|
|
|
2021-09-20 11:32:55 +02:00
|
|
|
def xor(ta,tb):
|
|
|
|
return bytes(a ^ b for a, b in zip(ta, tb))
|
|
|
|
|
2021-09-22 09:14:23 +02:00
|
|
|
def genIV():
|
|
|
|
return random.randint(0, 2**(bs-1)).to_bytes(bs, byteorder='big')
|
|
|
|
|
2021-09-20 11:32:55 +02:00
|
|
|
def enc(plaintext, key, iv):
|
|
|
|
ciphertext = bytes()
|
2021-09-21 09:14:31 +02:00
|
|
|
for i in range(math.ceil(len(plaintext)/bs)):
|
2021-09-20 11:32:55 +02:00
|
|
|
m = hashlib.sha256()
|
|
|
|
m.update(xor(key, iv + i.to_bytes(bs, byteorder='big')))
|
|
|
|
k = m.digest()
|
2021-09-21 15:54:29 +02:00
|
|
|
iv = (int.from_bytes(iv, byteorder='big')+1).to_bytes(bs, byteorder='big')
|
2021-09-22 09:14:23 +02:00
|
|
|
ciphertext += xor(k, plaintext[bs*i:][:bs].ljust(bs, b'0'))
|
2021-09-21 09:14:31 +02:00
|
|
|
return ciphertext
|
2021-09-20 11:32:55 +02:00
|
|
|
|
|
|
|
def dec(ciphertext, key, iv):
|
|
|
|
return enc(ciphertext, key, iv)
|
2021-09-21 09:14:31 +02:00
|
|
|
|
|
|
|
def getSample(length, src=None, key=b'VerySecureKeyMustKeepSecretDontTellAnyone'):
|
|
|
|
if src==None:
|
|
|
|
src = random.random() > 0.5
|
|
|
|
if not src:
|
|
|
|
r = os.urandom(length*bs)
|
|
|
|
return (r, 0)
|
|
|
|
else:
|
2021-09-22 09:14:23 +02:00
|
|
|
iv = genIV()
|
2021-09-21 09:14:31 +02:00
|
|
|
b = bytes(length*bs)
|
|
|
|
return (enc(b, key, iv), 1)
|