Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Block cipher
- #Feistel cipher
- import sys
- import os
- from _blake2 import blake2b
- def f(R, K):
- #Irreversible function
- #R is integer
- #K is character
- K = ord(K)
- size = len(bin(R)[2:])
- #must return of the same size (bits) as input
- var = (str(K)+str(R))
- return int(blake2b(var.encode()).hexdigest()[::-1],16)^int('1'*size,2)
- def xor(x, y):
- return x^y
- def Feistel_enc(L0, R0, K):
- for i in range(0,len(K)):
- L_next = R0
- R_next = xor(L0, f(R0, K[i]))
- L0 = L_next
- R0 = R_next
- return R0,L0
- def Feistel_dec(R0, L0, K):
- "K is the same as enc"
- K = K[::-1]
- return Feistel_enc(R0, L0, K)
- def split(string):
- half = len(string)//2
- return string[:half], string[half:]
- char_adjust = 8
- def message2int(message):
- fin = []
- for char in message:
- value = str(ord(char))
- adjust = char_adjust-len(value)
- fin.append(('0'*adjust)+value)
- fins = "".join(fin)
- fini = int(fins)
- return fini
- def int2message(integer):
- s = str(integer)
- fin = []
- counter = 0
- finl = [s[i:i+char_adjust] for i in range(0, len(s), char_adjust)]
- if len(finl[-1])<char_adjust:
- s = (char_adjust-len(finl[-1]))*"0" + s
- finl = [s[i:i+char_adjust] for i in range(0, len(s), char_adjust)]
- finl = list(map(int,finl))
- fin = list(map(chr,finl))
- return "".join(fin)
- def main(args):
- if 'enc' in args:
- enc = True
- dec=False
- elif 'dec' in args:
- dec = True
- enc=False
- else:
- dec=False
- enc=False
- K = args[1]
- if enc:
- message = " ".join(args[2:])
- m1,m2 = split(message)
- m1i = message2int(m1)
- m2i = message2int(m2)
- l,r = Feistel_enc(m1i,m2i,K)
- #cipher = int2message(int(str(l)+str(r)))
- cipher = str(l)+str(r)
- return cipher
- print("Ciphertext:\n\n{}".format(cipher))
- elif dec:
- cipher = args[2]
- c1,c2 = split(cipher)
- #c1i = message2int(c1)
- #c2i = message2int(c2)
- c1i = int(c1)
- c2i = int(c2)
- l,r = Feistel_dec(c1i,c2i,K)
- plain = int2message(l)+int2message(r)
- #plain = int2message(int(str(l)+str(r)))
- return plain
- print("Plaintext:\n\n{}".format(plain))
- else:
- print("Something went wrong. Got\a\n{}".format(args))
- input()
- def helping():
- print("Please use command line syntax:")
- print("""
- fc enc KEY FILEPATH
- Encryption
- fc dec KEY FILEPATH
- Decryption
- Files must be plaintext. No binary data.""")
- if __name__ == "__main__":
- args = sys.argv
- if len(args)<3:
- helping()
- sys.exit()
- #print(args)
- args = args[1:]
- if args[0]=="enc":
- ext = ".enc"
- elif args[0]=="dec":
- ext = ".dec"
- output = args[2]+ext
- try:
- with open(args[2], "r") as file:
- text = file.read()
- except OSError:
- print("Failed to open file: ", args[2])
- sys.exit()
- args[2] = text
- result = main(args)
- if output[-8:-4]==".enc":
- output = output[:-8]+output[-4:]
- elif output[-8:-4]==".dec":
- output = output[:-8]+output[-4:]
- try:
- with open(output, "w+") as out:
- out.write(result)
- except OSError:
- print("Failed to write to file: ", output)
- sys.exit()
- print("Executed successfully.")
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement