def crypt(action, data, iv_random=True): try: # key, it has to be 128, 192, o 256 bits, check configuration key = '147896523' # Initialization vector. It has the first 16 bytes in the message. # it is used to have the same message encrypted but with different result # CBCMode de AES if iv_random: iv = os.urandom(16 * 1024)[0:16] else: # This case should be for the emails iv = ' ' * 16 # The information of the message have to be multiple of 16 (AES block size), for this reason PADDING. # PADDING Guarantees that the message is multiple of the block padding = ' ' pad = lambda s: s + (16 - len(s) % 16) * padding if action == 'encrypt': return base64.b64encode( iv + AES.new(key, AES.MODE_CBC, iv).encrypt(pad(data))) elif action == 'decrypt': return AES.new(key, AES.MODE_CBC, data[:16]).decrypt( base64.b64decode(data).rstrip(padding))[16:] except Exception as e: HTTP(str(e))
def crypt(action, data, iv_random=True): try: import os from config import AppConfig config = AppConfig()['crypt'] if not config['is_active']: return data # Clave, debe de ser de 128, 192, o 256 bits, check configuration key = config['key'] # Initialization vector. Almacenado en los 16 primeros bytes del mensaje. # Utilizado para para tener el mismo mensaje encritado con distinto texto. # CBCMode de AES if iv_random: iv = os.urandom(16 * 1024)[0:16] else: # This case should be for the emails iv = ' ' * 16 # La longitud de la informacion a cifrar debe ser multiple de 16 (tamaño de bloque de AES), por eso PADDING. # Garantiza que el valor a cifrar sea multiple del tamaño de bloque padding = ' ' pad = lambda s: s + (16 - len(s) % 16) * padding import gluon.contrib.aes as AES import base64 if action == 'encrypt': return base64.b64encode(iv + AES.new(key, AES.MODE_CBC, iv).encrypt(pad(data))) elif action == 'decrypt': return AES.new(key, AES.MODE_CBC, data[:16]).decrypt(base64.b64decode(data).rstrip(padding))[16:] except Exception as e: logger.error(str(e))
def decrypt_item(nome_item): from gluon.contrib import aes as AES import base64 #chave padrão para criar a sequencia de criptografia, ela usa a sequencia alfabetica dessa chave para criar a criptografia secret_key = 'yma2578kyma2578kyma2578k' cipher = AES.new(secret_key,AES.MODE_ECB) decoded = cipher.decrypt(base64.b64decode(nome_item)).strip() return decoded
def crypt_item(nome_item): from gluon.contrib import aes as AES import base64 #chave padrão para criar a sequencia de criptografia, ela usa a sequencia alfabetica dessa chave para criar a criptografia msg_text = str(nome_item).rjust( 32) secret_key = 'yma2578kyma2578kyma2578k' cipher = AES.new(secret_key,int(AES.MODE_ECB)) encoded = base64.b64encode(cipher.encrypt(str(msg_text))) return encoded.strip()
def teste_crypt(): from gluon.contrib import aes as AES import base64 msg_text = '*****@*****.**'.rjust( 32) secret_key = 'yma2578kyma2578kyma2578k' cipher = AES.new(secret_key,AES.MODE_ECB) encoded = base64.b64encode(cipher.encrypt(msg_text)) decoded = cipher.decrypt(base64.b64decode(encoded)).strip() print decoded.strip() return 'decoded:' + decoded.strip() + '// coded : ' + encoded.strip()
def AES_new(key, IV=None): """ Returns an AES cipher object and random IV if None specified """ if IV is None: IV = fast_urandom16() return AES.new(key, AES.MODE_CBC, IV), IV
def aes_new(key, iv=None): """ Returns an AES cipher object and random IV if None specified """ if iv is None: iv = Enc.fast_urandom16() return AES.new(str(key), AES.MODE_CBC, iv), iv