leo_mgt

Aplicação para Criptografia AES

Apr 15th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None | 0 0
  1. #!/usr/bin/python3.5
  2.  
  3. # - - - - - - - - - - - - - - - - - - - - - - - - -
  4. # Aplicação para Criptografia AES
  5. # Autores: Leonardo Migot e Maico A. C. Souza
  6. # Data: Out/2016
  7. #
  8. #   Capacidades:
  9. #   Modos: EBC e CBC.
  10. #   Chaves: 128, 192 e 256 bits.
  11. #   Encripta e decriptar.
  12. #   Criar vetores de inicialização (128 bits)
  13. #   Encriptar arquivos de tamanhos variados.
  14. #
  15. # - - - - - - - - - - - - - - - - - - - - - - - - -
  16.  
  17. # # # # # # # # # # # #
  18.  
  19. # - - - Imports - - - #
  20.  
  21. # # # # # # # # # # # #
  22.  
  23. from Crypto.Cipher import AES
  24. from Crypto import Random
  25. import random
  26. import easygui
  27. from struct import *
  28. import binascii
  29. import base64
  30. import os
  31.  
  32.  
  33. # # # # # # # # # #
  34.  
  35. # - - - GUI - - - #
  36.  
  37. # # # # # # # # # #
  38.  
  39. def start():
  40.     return easygui.buttonbox('O que você deseja fazer?', '', ['Encriptar', 'Decriptar', 'Sair'])
  41.  
  42. def mode():
  43.     return easygui.buttonbox('Qual modo de operação?', '', ['ECB', 'CBC'])
  44.  
  45. def k_or_p():
  46.     return easygui.buttonbox('O que você deseja utilizar?', '', ['Nova Chave', 'Chave Existente', 'Senha'])
  47.  
  48. def IV_exist():
  49.     return easygui.buttonbox('Vetor de inicialização?', '', ['Novo IV', 'IV Existente'])
  50.  
  51. def k_or_p2():
  52.     return easygui.buttonbox('O que você deseja utilizar?', '', ['Chave Existente', 'Senha'])
  53.  
  54. def key_size():
  55.     return easygui.buttonbox('Qual o tamanho da chave?', '', ['128', '192', '256'])
  56.  
  57. def key_open():
  58.     easygui.msgbox('Escolha a chave...')
  59.     return easygui.fileopenbox('Chave...')
  60.  
  61. def key_save():
  62.     easygui.msgbox('Salvar a chave...')
  63.     return easygui.filesavebox('Chave...')
  64.  
  65. def IV_save():
  66.     easygui.msgbox('Salvar o IV...')
  67.     return easygui.filesavebox('IV...')
  68.  
  69. def password():
  70.     return easygui.passwordbox('Digite a senha (16 bytes / 16 caracteres):')
  71.  
  72. def file_open():
  73.     easygui.msgbox('Escolha um arquivo...')
  74.     return easygui.fileopenbox('Arquivo...')
  75.  
  76. def IV_open():
  77.     easygui.msgbox('Escolha o vetor de inicialização...')
  78.     return easygui.fileopenbox('Arquivo...')
  79.  
  80. def file_save():
  81.     easygui.msgbox('Salvar o arquivo após a operação...')
  82.     return easygui.filesavebox('Arquivo...')
  83.    
  84. def warning():
  85.     easygui.msgbox('Atenção! \nPara decriptar o arquivo selecionado futuramente, será necessario extrai-lo como um arquivo TAR. ')
  86.  
  87. # # # # # # # # # # # #
  88.  
  89. # - - - Funções - - - #
  90.  
  91. # # # # # # # # # # # #
  92.  
  93. def random_key(Vkey_size):
  94.     Vkey_size = int(Vkey_size)
  95.     Vkey_size = int(Vkey_size / 8)
  96.     rndk = Random.new().read(Vkey_size)
  97.     return rndk
  98.  
  99. def key_input(Vkey_open):
  100.     file_k = open(Vkey_open,'r')
  101.     key = file_k.readline()
  102.     key = binascii.unhexlify(key)
  103.     return key 
  104.  
  105. def key_output(Vkey_size, Vkey_save):
  106.     key = random_key(Vkey_size)
  107.     key = binascii.hexlify(key)
  108.     key = key.decode('utf8')
  109.     k_file = open(Vkey_save,'w+')  
  110.     k_file.write(key)
  111.     k_file.close()
  112.     return key
  113.  
  114. def tkey_size(Vkey_open):
  115.     size_k = open(Vkey_open,'r')
  116.     key = file_k.readline()
  117.     key = binascii.unhexlify(key)
  118.     size_k.close()
  119.     return len(key_size)
  120.  
  121. def tar_pad(plaintext, Vfile_open):
  122.  
  123.     div = len(plaintext) % 16
  124.  
  125.     if div != 0:
  126.         os.system('tar cf pln.tar ' + Vfile_open)
  127.         Vfile_open = 'pln.tar'
  128.         warning()
  129.  
  130.     return Vfile_open
  131.  
  132.  
  133. def AES_ECB(plaintext):
  134.  
  135.     var = AES.new(key, AES.MODE_ECB)
  136.  
  137.     if Vstart == 'Encriptar':
  138.  
  139.         ciphertext = var.encrypt(plaintext)
  140.         ciphertext = binascii.hexlify(ciphertext)
  141.         ciphertext = ciphertext.decode('utf8')
  142.  
  143.     elif Vstart == 'Decriptar':
  144.  
  145.         ciphertext = binascii.unhexlify(plaintext)
  146.         ciphertext = var.decrypt(ciphertext)
  147.         ciphertext = ciphertext.decode('utf8')     
  148.  
  149.     ciphertext = binascii.unhexlify(ciphertext)
  150.     c_file = open(Vfile_save, 'wb+')
  151.     c_file.write(ciphertext)
  152.     c_file.close()
  153.  
  154. def AES_CBC(plaintext):
  155.  
  156.     var = AES.new(key, AES.MODE_CBC, IV)
  157.  
  158.     if Vstart == 'Encriptar':
  159.  
  160.         ciphertext = var.encrypt(plaintext)
  161.         ciphertext = binascii.hexlify(ciphertext)
  162.         ciphertext = ciphertext.decode('utf8')
  163.  
  164.     elif Vstart == 'Decriptar':
  165.  
  166.         ciphertext = binascii.unhexlify(plaintext)
  167.         ciphertext = var.decrypt(ciphertext)
  168.         ciphertext = ciphertext.decode('utf8')     
  169.  
  170.     ciphertext = binascii.unhexlify(ciphertext)
  171.     c_file = open(Vfile_save, 'wb+')
  172.     c_file.write(ciphertext)
  173.     c_file.close()
  174.  
  175.  
  176. ## # # # # # # # # # # # # # # #
  177.  
  178. # - - - Interação Usuário- - - #
  179.  
  180. ## # # # # # # # # # # # # # # #
  181.  
  182. while(True):
  183.  
  184.  
  185. # # # # # # # # # # # # # # # # # # #
  186.  
  187. # - - - Operação, sair e modo - - - #
  188.  
  189. # # # # # # # # # # # # # # # # # # #
  190.  
  191.     Vstart = start()
  192.     if Vstart == 'Sair':
  193.         exit()
  194.     Vmode = mode()
  195.    
  196.  
  197. # # # # # # # # # # # # # # # # # #
  198.  
  199. # - - - Instanciar variaveis - - -#
  200.  
  201. # # # # # # # # # # # # # # # # # #
  202.  
  203.     Vkey_size = 'NULL'
  204.     Vkey_open = 'NULL'
  205.     key = 'NULL'
  206.  
  207.  
  208. # # # # # # # # # # # # # # # # # # # # # # # # # #
  209.  
  210. # - - - Encriptar, Nova Chave, Decriptar... - - - #
  211.  
  212. # # # # # # # # # # # # # # # # # # # # # # # # # #
  213.  
  214.     if Vstart == 'Encriptar':
  215.         Vk_or_p = k_or_p()
  216.  
  217.         if Vk_or_p == 'Nova Chave':
  218.             Vkey_size = key_size()
  219.             Vkey_save = key_save()
  220.             Vkey_open = Vkey_save
  221.  
  222.             key_output(Vkey_size, Vkey_save)
  223.             key = key_input(Vkey_open)
  224.  
  225.         elif Vk_or_p == 'Chave Existente':
  226.             Vkey_open = key_open()
  227.             key = key_input(Vkey_open)
  228.  
  229.         elif Vk_or_p == 'Senha':
  230.             key = password()
  231.             #key = binascii.hexlify(key)
  232.  
  233.     elif Vstart == 'Decriptar':
  234.         Vk_or_p2 = k_or_p2()
  235.  
  236.         if Vk_or_p2 == 'Chave Existente':
  237.             Vkey_open = key_open()         
  238.             key = key_input(Vkey_open)
  239.  
  240.         elif Vk_or_p2 == 'Senha':
  241.             key = password()
  242.             #key = binascii.hexlify(key)
  243.  
  244.  
  245. # # # # # # # # # # # # #
  246.  
  247. # - - - Modo e IV - - - #
  248.  
  249. # # # # # # # # # # # # #
  250.  
  251.     if Vmode == 'CBC':
  252.         VIV_exist = IV_exist()
  253.         if VIV_exist == 'Novo IV':
  254.             VIV_save = IV_save()   
  255.             VIV_open = VIV_save    
  256.            
  257.             key_output(128, VIV_save)          
  258.             IV = key_input(VIV_open)
  259.  
  260.         elif VIV_exist == 'IV Existente':
  261.             VIV_open = IV_open()
  262.             IV = key_input(VIV_open)
  263.  
  264.  
  265. ## # # # # # # # # # # # # # # #
  266.  
  267. # - - - Carregar Arquivo - - - #
  268.  
  269. ## # # # # # # # # # # # # # # #
  270.  
  271.     Vfile_open = file_open()
  272.     Vfile_save = file_save()
  273.  
  274.  
  275. ## # # # # # # # # # # # # # #
  276.  
  277. # - - - Carregar texto - - - #
  278.  
  279. ## # # # # # # # # # # # # # #
  280.  
  281.     file = open(Vfile_open,'rb')
  282.    
  283.     plaintext = file.read()
  284.  
  285.     plaintext = binascii.hexlify(plaintext)
  286.  
  287.     Vfile_open = tar_pad(plaintext, Vfile_open)
  288.  
  289.     file.close()
  290.  
  291.     file = open(Vfile_open,'rb')
  292.    
  293.     plaintext = file.read()
  294.  
  295.     plaintext = binascii.hexlify(plaintext)
  296.  
  297.     dic = {'ECB':AES_ECB, 'CBC':AES_CBC}
  298.  
  299.     dic[Vmode](plaintext)
  300.  
  301.     os.system('rm pln.tar')
  302.  
  303.  
  304. # # # # # # # # # # # # # # # #
  305.  
  306. # - - - - - - FIM - - - - - - #
  307.  
  308. # # # # # # # # # # # # # # # #
Add Comment
Please, Sign In to add comment