def read_public_key(key_text): """ Reads a FinCrypt public key. Returns a dictionary of all public key values. Raises exception if key is malformed or unreadable. The ASN.1 specification for a FinCrypt public key resides in asn1spec.py :param key_text: Key text (string) :return: Dictionary of all key ASN.1 values """ key_header, key_text = strip_headers(key_text) if key_header is None or key_header != 'BEGIN FINCRYPT PUBLIC KEY': raise ValueError b64_decoded = base64.urlsafe_b64decode(key_text.encode('utf-8')) rsc = reedsolomon.RSCodec(30) b64_decoded = bytes(rsc.decode(b64_decoded)[0]) key, _ = decode_ber(b64_decoded, asn1Spec=FinCryptPublicKey()) key = encode_native(key) return { 'kx': key['kx'], 'ky': key['ky'], 'name': key['name'], 'email': key['email'] }
def decrypt_and_verify(message, sender_key, private_key): """ Decrypts and verifies a message using a sender's public key name Looks for the sender's public key in the public_keys/ directory. Looks for your private key as private_key/private.asc The ASN.1 specification for a FinCrypt message resides in asn1spec.py Raises exceptions if key files are not found, or are malformed. :param message: Message to decrypt (bytes) :param private_key: Decrypter's private key (file like object) :param sender_key: Sender's public key (file like object) :return: Tuple (decrypted message (bytes), whether the message was verified (boolean)) If message was unable to be decrypted, the tuple will be (None, False) """ try: decryption_key = read_private_key(private_key.read()) except Exception: raise FinCryptDecodingError('Private key file is malformed.') try: sender_key = read_public_key(sender_key.read()) except Exception: raise FinCryptDecodingError('Sender key file is malformed.') try: rsc = reedsolomon.RSCodec(8) message = bytes(rsc.decode(message)[0]) decoded, _ = decode_ber(message, asn1Spec=FinCryptMessage()) decoded = encode_native(decoded) except Exception: return None, False try: decrypted_message = decrypt_message(decryption_key['k'], decoded['key'], decoded['message']) except Exception: decrypted_message = None try: authenticated = authenticate_message(sender_key['kx'], sender_key['ky'], decrypted_message, decoded['signature']) except Exception: authenticated = False return decrypted_message, authenticated
def encrypt_and_sign(message, recipient_key, signer_key): """ Encrypts and signs a message using a recipient's public key name Looks for the recipient's public key in the public_keys/ directory. Looks for your private key as private_key/private.asc The ASN.1 specification for a FinCrypt message resides in asn1spec.py Raises exceptions if key files are not found, or are malformed. :param message: Message to encrypt (bytes) :param recipient_key: Recipient's public key (file like object) :param signer_key: Signer's private key (file like object) :return: Bytes of encrypted and encoded message and signature. """ try: recipient_key = read_public_key(recipient_key.read()) except Exception: raise FinCryptDecodingError('Recipient keyfile was malformed.') try: signer_key = read_private_key(signer_key.read()) except Exception: raise FinCryptDecodingError('Private key file is malformed.') try: encrypted_key, encrypted_blocks = encrypt_message( recipient_key['kx'], recipient_key['ky'], message) except Exception: raise FinCryptDecodingError( 'Unknown error encountered when encrypting message.') signature = sign_message(signer_key['k'], message) encrypted_message = FinCryptMessage() encrypted_message['message'] = encrypted_blocks encrypted_message['key'].extend(encrypted_key) encrypted_message['signature'].extend(signature) encoded_message = encode_der(encrypted_message) rsc = reedsolomon.RSCodec(8) encoded_message = bytes(rsc.encode(encoded_message)) return encoded_message
def gen_key_files(*, key_name, key_email): """ Generates keys. Public keys contain an encryption key for messages and a decryption key for signatures. Private keys contain a decryption key for messages and an encryption key for signatures. :param key_name: User's name :param key_email: User's email :return: Public key string, private key string """ private = ecc.ECPrivateKey.generate(ecc.CURVE) public = private.pubkey pub_key = FinCryptPublicKey() priv_key = FinCryptPrivateKey() pub_key['kx'] = public.point.x pub_key['ky'] = public.point.y pub_key['name'] = key_name pub_key['email'] = key_email priv_key['k'] = private.scalar priv_key['name'] = key_name priv_key['email'] = key_email pub_key_bytes = encode(pub_key) priv_key_bytes = encode(priv_key) rsc = reedsolomon.RSCodec(30) pub_key_bytes = bytes(rsc.encode(pub_key_bytes)) public = base64.urlsafe_b64encode(pub_key_bytes).decode('utf-8') private = base64.urlsafe_b64encode(priv_key_bytes).decode('utf-8') public_string = ' BEGIN FINCRYPT PUBLIC KEY '.center(76, '-') + '\n' public_string += '\n'.join([public[i:i + 76] for i in range(0, len(public), 76)]) public_string += '\n' + ' END FINCRYPT PUBLIC KEY '.center(76, '-') private_string = ' BEGIN FINCRYPT PRIVATE KEY '.center(76, '-') + '\n' private_string += '\n'.join([private[i:i + 76] for i in range(0, len(private), 76)]) private_string += '\n' + ' END FINCRYPT PRIVATE KEY '.center(76, '-') return public_string, private_string
import cv2 import numpy as np import wavelets as wl import video_manager as vm import reedsolomon as rs import sys video = cv2.VideoCapture("Lady-Gaga-Test.mp4") if video.isOpened(): key = input() codec = rs.RSCodec(8) key_ASCII = [] for character in key: print(ord(character)) key_ASCII.append(ord(character)) code = codec.encode(key_ASCII) if len(key) > 3: sys.exit("Too much characters. Please try one more time.") elif len(key) == 0: sys.exit("No characters was typed. Please try one more time.") else: vm.encodeVideo(video, code) #vm.encodeVideo(video, key_ASCII)