Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from asn1 import Encoder, Decoder, Numbers
- from Cryptodome.Random import get_random_bytes
- from Cryptodome.Random.random import randint
- from Cryptodome.Util.number import getPrime, inverse, GCD
- from Cryptodome.Cipher import AES
- from Cryptodome.Util.Padding import pad, unpad
- CONST_MENU = "Enter command number:\n" \
- "1 - Encrypt file\n" \
- "2 - Decrypt user's file\n" \
- "3 - Exit\n"
- CONST_X = 88
- # AES key generation
- def generate_aes_key():
- #return get_random_bytes(32)
- AES_key = b'\x8e\x10l\x0f\x11w\xbc\xb4\xef\xf3\xe2\xf8\xe1\x1f \xe5~\xa6s\x1f\xack\x06 ^KkDc\x15\xa8\x88'
- return AES_key
- # Read file data
- def read_data(filename):
- data = b''
- try:
- with open(filename, "rb") as inp:
- for line in inp:
- data += line
- except FileNotFoundError:
- print("File not found\n")
- return data
- # Encrypt data
- def encrypt_input(filename, aes_key):
- iv = 16 * b' '
- input_data = b''
- input_data = read_data(filename)
- cipher = AES.new(aes_key, AES.MODE_CBC, iv)
- cipher_text = cipher.encrypt(pad(input_data, 16))
- return cipher_text
- # ElGamal params
- def generate_params():
- G = list()
- a = getPrime(8, randfunc=get_random_bytes)
- p = getPrime(1024, randfunc=get_random_bytes)
- tmp = 1
- G.append(tmp)
- for i in range(0, p):
- tmp *= a
- if tmp > p:
- break
- G.append(tmp)
- b = G[CONST_X]
- return [a, p, G, b]
- # ElGamal already generated params
- # ASN1 format
- def write_asn1_file(cipher):
- f = Encoder()
- f.start()
- f.enter(Numbers.Sequence)
- f.enter(Numbers.Set)
- f.enter(Numbers.Sequence)
- f.write(b'\x80\x01\x02\x01', Numbers.OctetString)
- f.write(b'Encoded file with El Gamal', Numbers.UTF8String)
- f.enter(Numbers.Sequence)
- f.write(cipher[3], Numbers.Integer)
- f.leave()
- f.enter(Numbers.Sequence)
- f.write(cipher[5], Numbers.Integer)
- f.write(cipher[4], Numbers.Integer)
- f.leave()
- f.enter(Numbers.Sequence)
- f.write(cipher[1], Numbers.Integer)
- f.write(cipher[0], Numbers.Integer)
- f.leave()
- f.leave()
- f.leave()
- f.enter(Numbers.Sequence)
- f.write(b'\x10\x82', Numbers.OctetString)
- f.write(len(cipher[6]), Numbers.Integer)
- f.leave()
- f.leave()
- f.write(cipher[6])
- with open("encrypted.txt", "wb") as enc:
- enc.write(f.output())
- # ElGamal encryption
- def encrypt_data(filename):
- aes_key = generate_aes_key()
- m = int.from_bytes(aes_key, byteorder="big")
- encrypted_data = encrypt_input(filename, aes_key)
- #crypto_params = generate_params()
- crypto_params = return_const_params()
- y = 213132
- counter = 2
- a = crypto_params[0]
- b = crypto_params[3]
- while counter < y:
- a = a * crypto_params[0] % crypto_params[1]
- b = b * crypto_params[0] % crypto_params[1]
- counter += 1
- c = m * b % crypto_params[1]
- cipher = list()
- cipher.append(a)
- cipher.append(c)
- x = 1
- while a != b:
- a = a * crypto_params[0] % crypto_params[1]
- x += 1
- #print(x)
- #print(c)
- #print(a)
- cipher.append(x)
- cipher.append(crypto_params[3])
- cipher.append(crypto_params[0])
- cipher.append(crypto_params[1])
- cipher.append(encrypted_data)
- write_asn1_file(cipher)
- print("File was successfully encrypted!")
- return cipher
- # AES decryption
- def decrypt_text(aes_key, text):
- iv = 16 * b' '
- cipher = AES.new(aes_key, AES.MODE_CBC, iv)
- plain_text = cipher.decrypt(text)
- plain_text = unpad(plain_text, 16)
- return plain_text
- # ElGamal decryption
- def decrypt_data(filename):
- data = read_data(filename)
- f = Decoder()
- f.start(data)
- f.enter()
- f.enter()
- f.enter()
- value = f.read()
- value = f.read()
- f.enter()
- value = f.read()[1]
- f.leave()
- f.enter()
- param = list()
- value1 = f.read()[1]
- value2 = f.read()[1]
- param.append(value2)
- param.append(value1)
- f.leave()
- f.enter()
- c = f.read()[1]
- a = f.read()[1]
- f.leave()
- f.leave()
- f.leave()
- f.enter()
- length = f.read()
- val = f.read()
- f.leave()
- f.leave()
- text = f.read()[1]
- i = 1
- while i != CONST_X:
- i += 1
- a = a * param[0] % param[1]
- m = c * inverse(a, param[1]) % param[1]
- m = m.to_bytes((m.bit_length() // 8), byteorder="big")
- plain_text = decrypt_text(m, text)
- with open("decrypted.txt", "wb") as res:
- res.write(plain_text)
- print("File was successfully decrypted!")
- # Get user's filename
- def get_filename(op_type):
- return input("Enter filename for " + op_type + ": ")
- # main func
- def main():
- while 1:
- user_choice = int(input(CONST_MENU))
- if user_choice == 1:
- encrypt_data(get_filename("encryption"))
- break
- elif user_choice == 2:
- decrypt_data(get_filename("decryption"))
- break
- elif user_choice == 3:
- return
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement