Advertisement
fransafu

AES Cifrado limpio

Aug 28th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. import base64
  3. import os
  4.  
  5. def encryption(privateInfo):
  6.     BLOCK_SIZE = 16
  7.     PADDING = '{'
  8.     pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
  9.     EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
  10.     secret = os.urandom(BLOCK_SIZE)
  11.     secret = bytes(secret)
  12.     cipher = AES.new(secret)
  13.     encoded = EncodeAES(cipher, privateInfo)
  14.     respuesta = [secret, encoded] # key y mensaje cifrado
  15.  
  16.     return respuesta
  17.  
  18.  
  19. def decryption(encryptedString, key):
  20.     PADDING = '{'
  21.     DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  22.     encryption = encryptedString
  23.     cipher = AES.new(key)
  24.     decoded = DecodeAES(cipher, encryption)
  25.  
  26.     return decoded
  27.  
  28. def main():
  29.     mensaje = raw_input("Ingrese su mensaje: ")
  30.  
  31.     respuesta = encryption(mensaje)
  32.    
  33.     print respuesta[1] # mensaje cifrado
  34.     print respuesta[0] # key
  35.  
  36.     print decryption(respuesta[1], respuesta[0])
  37.  
  38. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement