def _tf_decrypt(key, iv, data): cipher = skein.threefish(derive_key(key, iv, 512), _tf_tweak_ctr(0)) output = b'' for k in range(0, len(data), 64): cipher.tweak = _tf_tweak_ctr(k) output += cipher.decrypt_block(data[k : k + 64]) return output.strip()
def fencrypt(filen, password, tweak=0, mode='twofish'): f=open(filen,'r') smstr=f.read() f.close() if mode=='twofish': # splitting it to blocks with 16-bytes len if len(smstr)%16: nstr=str(smstr+'%'*(16-len(smstr)%16)).encode('utf-8') else: nstr=smstr.encode('utf-8') psswd=Twofish(password) encredstr=b'' # ENCRyptED STRing # encrypting blocks for x in range(int(len(nstr)/16)): encredstr+=psswd.encrypt(nstr[x*16:(x+1)*16]) elif mode=='threefish': # splitting it to blocks with 128-bytes len if len(smstr)%128: nstr=str(smstr+'%'*(128 - len(smstr)%128)).encode('utf-8') else: nstr=smstr.encode('utf-8') psswd=threefish(password,tweak) encredstr=b'' # ENCRyptED STRing # encrypting blocks for x in range(int(len(nstr)/128)): encredstr+=psswd.encrypt_block(nstr[x*128:(x+1)*128]) # writing it to file f=open(filen,'wb') f.write(encredstr) f.close()
def test_roundtrip(self): for n in range(1, 101): key = bytes(random.randint(0, 255) for _ in range(self.KEYLEN)) tweak = bytes(random.randint(0, 255) for _ in range(16)) plain = bytes(random.randint(0, 255) for _ in range(self.KEYLEN)) t = skein.threefish(key, tweak) self.assertEqual(t.decrypt_block(t.encrypt_block(plain)), plain) print("\n{0} random Threefish-{1} roundtrip tests succeeded.".format( n, self.KEYLEN * 8))
def _tf_encrypt(key, iv, data): # adso always uses JSON, so we pad the message with JSON whitespace. while len(data) % 64 != 0: data += b' ' cipher = skein.threefish(derive_key(key, iv, 512), _tf_tweak_ctr(0)) output = b'' for k in range(0, len(data), 64): cipher.tweak = _tf_tweak_ctr(k) output += cipher.encrypt_block(data[k : k + 64]) return output
def testPRNGStateInspection(self): r = skein.Random(b"x", hasher=self.HASHER) # check initial state state = r._state d = self.HASHER(bytes(self.STATE_BITS // 8) + b"x").digest() self.assertEqual(state, d) # check state after random() call r.random() t = skein.threefish(state, bytes(15) + bytes([63])) d = t.encrypt_block(bytes(self.STATE_BITS // 8)) self.assertEqual(r._state, d)
def fdecrypt(filen, password, tweak=0, mode='twofish'): # reading file in byte mode f=open(filen,'rb') smstr=f.read() f.close() if mode=='twofish': psswd=Twofish(password) decredstr=b'' # decrypting blocks for x in range(int(len(smstr)/16)): decredstr+=psswd.decrypt(smstr[x*16:(x+1)*16]) elif mode=='threefish': psswd=threefish(password,tweak) decredstr=b'' # decrypting blocks for x in range(int(len(smstr)/128)): decredstr+=psswd.decrypt_block(smstr[x*128:(x+1)*128]) return decode(decredstr,'utf-8').strip('%')
def setUp(self): self.t = skein.threefish(bytes(range(self.KEYLEN)), bytes(range(16))) # create a distractor object for strange string buffer false positives: skein.threefish(bytes(range(self.KEYLEN)), bytes(range(11, 27)))
from flask import Flask, render_template, jsonify from skein import threefish # Se definen la llave y el tweak como bloques que se van a usar para # encriptar, el tweak debe ser de 16 bytes y la llave puede tomar # valores de 32, 64 o 128, luego lo que se quiera encriptar debe tener # el mismo largo que la llave t = threefish(b'key of 32,64 or 128 bytes lenght', b'tweak: 16 bytes ') app = Flask(__name__) @app.route('/', methods=["GET", "POST"]) def index(): # se pasa como bloque lo que se quiere encriptar y la funcion # encrpypt_block realiza el enpriptado y se guarda el valor en fish fish = t.encrypt_block(b'block of data,same length as key') # Se hace un print para observar en la consola como queda print("\nbloque encriptado\n") print(fish) print("block size and block bits\n") print(t.block_size, t.block_bits) temp = fish # Dado que el codigo de javascript admite utf8 y no en bloques # se envia fish en utf8 fish = fish.decode('cp1251').encode('utf8') print("\nMensaje encriptado:\n") print(fish)