Advertisement
saleks28

kmzi3_8sem

Aug 12th, 2020
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.04 KB | None | 0 0
  1. from asn1 import Encoder, Decoder, Numbers
  2. from Cryptodome.Random import get_random_bytes
  3. from Cryptodome.Random.random import randint
  4. from Cryptodome.Util.number import getPrime, inverse, GCD
  5. from Cryptodome.Cipher import AES
  6. from Cryptodome.Util.Padding import pad, unpad
  7.  
  8. CONST_MENU = "Enter command number:\n" \
  9.              "1 - Encrypt file\n" \
  10.              "2 - Decrypt user's file\n" \
  11.              "3 - Exit\n"
  12. CONST_X = 88
  13.  
  14. # AES key generation
  15. def generate_aes_key():
  16.     #return get_random_bytes(32)
  17.     AES_key = b'\x8e\x10l\x0f\x11w\xbc\xb4\xef\xf3\xe2\xf8\xe1\x1f \xe5~\xa6s\x1f\xack\x06 ^KkDc\x15\xa8\x88'
  18.     return AES_key
  19.  
  20.  
  21. # Read file data
  22. def read_data(filename):
  23.     data = b''
  24.     try:
  25.         with open(filename, "rb") as inp:
  26.             for line in inp:
  27.                 data += line
  28.     except FileNotFoundError:
  29.         print("File not found\n")
  30.     return data
  31.  
  32.  
  33. # Encrypt data
  34. def encrypt_input(filename, aes_key):
  35.     iv = 16 * b' '
  36.     input_data = b''
  37.     input_data = read_data(filename)
  38.     cipher = AES.new(aes_key, AES.MODE_CBC, iv)
  39.     cipher_text = cipher.encrypt(pad(input_data, 16))
  40.     return cipher_text
  41.  
  42.  
  43. # ElGamal params
  44. def generate_params():
  45.     G = list()
  46.     a = getPrime(8, randfunc=get_random_bytes)
  47.     p = getPrime(1024, randfunc=get_random_bytes)
  48.     tmp = 1
  49.     G.append(tmp)
  50.     for i in range(0, p):
  51.         tmp *= a
  52.         if tmp > p:
  53.             break
  54.         G.append(tmp)
  55.     b = G[CONST_X]
  56.     return [a, p, G, b]
  57.  
  58.  
  59. # ElGamal already generated params
  60. # ASN1 format
  61. def write_asn1_file(cipher):
  62.     f = Encoder()
  63.     f.start()
  64.     f.enter(Numbers.Sequence)
  65.     f.enter(Numbers.Set)
  66.     f.enter(Numbers.Sequence)
  67.     f.write(b'\x80\x01\x02\x01', Numbers.OctetString)
  68.     f.write(b'Encoded file with El Gamal', Numbers.UTF8String)
  69.     f.enter(Numbers.Sequence)
  70.     f.write(cipher[3], Numbers.Integer)
  71.     f.leave()
  72.     f.enter(Numbers.Sequence)
  73.     f.write(cipher[5], Numbers.Integer)
  74.     f.write(cipher[4], Numbers.Integer)
  75.     f.leave()
  76.     f.enter(Numbers.Sequence)
  77.     f.write(cipher[1], Numbers.Integer)
  78.     f.write(cipher[0], Numbers.Integer)
  79.     f.leave()
  80.     f.leave()
  81.     f.leave()
  82.     f.enter(Numbers.Sequence)
  83.     f.write(b'\x10\x82', Numbers.OctetString)
  84.     f.write(len(cipher[6]), Numbers.Integer)
  85.     f.leave()
  86.     f.leave()
  87.     f.write(cipher[6])
  88.     with open("encrypted.txt", "wb") as enc:
  89.         enc.write(f.output())
  90.  
  91.  
  92. # ElGamal encryption
  93. def encrypt_data(filename):
  94.     aes_key = generate_aes_key()
  95.     m = int.from_bytes(aes_key, byteorder="big")
  96.     encrypted_data = encrypt_input(filename, aes_key)
  97.     #crypto_params = generate_params()
  98.     crypto_params = return_const_params()
  99.     y = 213132
  100.     counter = 2
  101.     a = crypto_params[0]
  102.     b = crypto_params[3]
  103.     while counter < y:
  104.         a = a * crypto_params[0] % crypto_params[1]
  105.         b = b * crypto_params[0] % crypto_params[1]
  106.         counter += 1
  107.     c = m * b % crypto_params[1]
  108.     cipher = list()
  109.     cipher.append(a)
  110.     cipher.append(c)
  111.     x = 1
  112.     while a != b:
  113.         a = a * crypto_params[0] % crypto_params[1]
  114.         x += 1
  115.     #print(x)
  116.     #print(c)
  117.     #print(a)
  118.     cipher.append(x)
  119.     cipher.append(crypto_params[3])
  120.     cipher.append(crypto_params[0])
  121.     cipher.append(crypto_params[1])
  122.     cipher.append(encrypted_data)
  123.     write_asn1_file(cipher)
  124.     print("File was successfully encrypted!")
  125.     return cipher
  126.  
  127.  
  128. # AES decryption
  129. def decrypt_text(aes_key, text):
  130.     iv = 16 * b' '
  131.     cipher = AES.new(aes_key, AES.MODE_CBC, iv)
  132.     plain_text = cipher.decrypt(text)
  133.     plain_text = unpad(plain_text, 16)
  134.     return plain_text
  135.  
  136. # ElGamal decryption
  137. def decrypt_data(filename):
  138.     data = read_data(filename)
  139.     f = Decoder()
  140.     f.start(data)
  141.     f.enter()
  142.     f.enter()
  143.     f.enter()
  144.     value = f.read()
  145.     value = f.read()
  146.     f.enter()
  147.     value = f.read()[1]
  148.     f.leave()
  149.     f.enter()
  150.     param = list()
  151.     value1 = f.read()[1]
  152.     value2 = f.read()[1]
  153.     param.append(value2)
  154.     param.append(value1)
  155.     f.leave()
  156.     f.enter()
  157.     c = f.read()[1]
  158.     a = f.read()[1]
  159.     f.leave()
  160.     f.leave()
  161.     f.leave()
  162.     f.enter()
  163.     length = f.read()
  164.     val = f.read()
  165.     f.leave()
  166.     f.leave()
  167.     text = f.read()[1]
  168.     i = 1
  169.     while i != CONST_X:
  170.         i += 1
  171.         a = a * param[0] % param[1]
  172.     m = c * inverse(a, param[1]) % param[1]
  173.     m = m.to_bytes((m.bit_length() // 8), byteorder="big")
  174.     plain_text = decrypt_text(m, text)
  175.     with open("decrypted.txt", "wb") as res:
  176.         res.write(plain_text)
  177.     print("File was successfully decrypted!")
  178.  
  179.  
  180. # Get user's filename
  181. def get_filename(op_type):
  182.     return input("Enter filename for " + op_type + ": ")
  183.  
  184.  
  185. # main func
  186. def main():
  187.     while 1:
  188.         user_choice = int(input(CONST_MENU))
  189.         if user_choice == 1:
  190.             encrypt_data(get_filename("encryption"))
  191.             break
  192.         elif user_choice == 2:
  193.             decrypt_data(get_filename("decryption"))
  194.             break
  195.         elif user_choice == 3:
  196.             return
  197.  
  198.  
  199. main()
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement