Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import codecs
- import itertools
- import os
- import binascii
- import random
- def to_bits(s):
- result = []
- for c in s:
- bits = bin(ord(c))[2:]
- bits = '00000000'[len(bits):] + bits
- result.extend([int(b) for b in bits])
- return result
- def from_bits(bits):
- chars = []
- length = int(len(bits) / 8)
- for b in range(length):
- byte = bits[b * 8:(b + 1) * 8]
- chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))
- return ''.join(chars)
- def chunks(array, chunk_size):
- for i in range(0, len(array), chunk_size):
- yield array[i:i + chunk_size]
- def xor(a, b):
- block = []
- for i in range(len(a)):
- block.append(int(bool(a[i]) ^ bool(b[i])))
- return block
- def int_to_bits(value, width=32):
- bits = list("{0:b}".format(value).zfill(width))
- bits = [int(i) for i in bits]
- return bits
- def word_to_int(word):
- value = 0
- exp = 1
- for i in range(0, 8):
- if word[i] == 1:
- value += exp
- exp *= 2
- return value
- def halve(block):
- size = int(len(block))
- half = int(size / 2)
- return [block[0:half], block[half: size]]
- class Blowfish:
- def generate_IV(self):
- bits = []
- bytes = os.urandom(8)
- for i in range(0, len(bytes)):
- bits += int_to_bits(bytes[i], 8)
- self.IV = bits
- def set_IV(self, source):
- bits = to_bits(source)
- if len(bits) < 64:
- bits += int_to_bits((64 - len(bits)) * [0])
- if len(bits) > 64:
- bits = bits[0:64]
- self.IV = bits
- def set_key(self):
- key = self.key
- # Stretch the key
- while len(key) < 576:
- key = key + self.key
- key = to_bits(key)
- # XOR the subkeys with stretched key
- for i in range(0, 18):
- self.subkeys[i] = word_to_int(xor(int_to_bits(self.subkeys[i]), key[i * 32: (i + 1) * 32]))
- # Zero initial vector
- current_block = int_to_bits(0) + int_to_bits(0)
- # Rearranging p-values(subkeys)
- for i in range(0, 18, 2):
- current_block = self.encrypt_block(current_block)
- self.subkeys[i] = word_to_int(halve(current_block)[0])
- self.subkeys[i + 1] = word_to_int(halve(current_block)[1])
- # Rearranging S-boxes
- for i in range(0, 4):
- for j in range(0, 256, 2):
- current_block = self.encrypt_block(current_block)
- self.s_boxes[i][j] = word_to_int(halve(current_block)[0])
- self.s_boxes[i][j + 1] = word_to_int(halve(current_block)[0])
- def set_blocks(self, source):
- bits = to_bits(self.plaintext)
- blocks = list(chunks(bits, 64))
- print(len(blocks[-1]))
- if len(blocks[-1]) < 64:
- blocks[-1] += (64 - len(blocks[-1]))*[0]
- return blocks
- def __init__(self, key, string):
- self.s_boxes = [s0, s1, s2, s3]
- self.subkeys = p
- self.key = key
- self.IV = None
- self.plaintext = string
- self.ciphertext = None
- self.cipher_blocks = []
- self.blocks = []
- self.blocks = self.set_blocks(self.plaintext)
- self.set_key()
- encrypted_block = self.encrypt_block(self.blocks[0])
- encrypted_block = self.encrypt_block(self.blocks[0])
- def encrypt(self, pt, IV = None):
- if not IV:
- self.generate_IV()
- else:
- self.set_IV(IV)
- self.plaintext = pt
- self.cipher_blocks = []
- self.blocks = self.set_blocks(self.plaintext)
- current_block = self.encrypt_block(xor(self.blocks[0], self.IV))
- self.cipher_blocks.append(current_block)
- for i in range(1, len(self.blocks)):
- current_block = self.encrypt_block(xor(self.blocks[i], current_block))
- self.cipher_blocks.append(current_block)
- return "".join([from_bits(x) for x in self.cipher_blocks])
- def decrypt(self, ct, IV = None):
- if IV:
- self.set_IV(IV)
- self.ciphertext = ct
- self.cipher_blocks = []
- self.blocks = []
- self.cipher_blocks = self.set_blocks(self.ciphertext)
- for i in range(len(self.cipher_blocks) - 1, 0, -1):
- current_block = self.decrypt_block(self.cipher_blocks[i])
- current_block = xor(current_block, self.cipher_blocks[i - 1])
- self.blocks.append(current_block)
- current_block = self.decrypt_block(self.cipher_blocks[0])
- current_block = xor(current_block, self.IV)
- self.blocks.append(current_block)
- self.blocks.reverse()
- return ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement