def decrypt( sender_pubkey: str, receiver_privkey: Union[str, bytes], msg: bytes, nonce: bytes, ) -> bytes: """ Decrypt with receiver's secp256k1 private key Parameters ---------- sender_pubkey: bytes The sender's public key receiver_privkey: Union[str, bytes] Receiver's private key (hex str or bytes) msg: bytes Data to decrypt nonce: bytes The nonce(16 bytes) used in the aes encryption Returns ------- bytes Plain text """ sender_pubkey = hex2pub(sender_pubkey) if isinstance(receiver_privkey, str): private_key = hex2prv(receiver_privkey) elif isinstance(receiver_privkey, bytes): private_key = PrivateKey(receiver_privkey) else: raise TypeError("Invalid secret key type") shared_key = decapsulate(sender_pubkey, private_key) return aes_decrypt(shared_key, msg, nonce)
def shared_secret(sk, ciphertext): if isinstance(sk, str): private_key = hex2prv(sk) elif isinstance(sk, bytes): private_key = PrivateKey(sk) else: raise TypeError("Invalid secret key type") pubkey = ciphertext[0:65] ephemeral_public_key = PublicKey(pubkey) aes_key = decapsulate(ephemeral_public_key, private_key) return aes_key
def decrypt( sender_pubkey: str, receiver_privkey: Union[str, bytes], msg: bytes, nonce: bytes, ) -> bytes: """Decrypt with receiver's secp256k1 private key :param sender_pubkey: :param receiver_privkey: :param msg: message to decrypt :param nonce: 16 byte nonce used in the aes decryption :return: decrypted data """ sender_pubkey = hex2pub(sender_pubkey) if isinstance(receiver_privkey, str): private_key = hex2prv(receiver_privkey) elif isinstance(receiver_privkey, bytes): private_key = PrivateKey(receiver_privkey) else: raise TypeError("Invalid secret key type") shared_key = decapsulate(sender_pubkey, private_key) return aes_decrypt(shared_key, msg, nonce)
def decrypt(receiver_prvhex: str, msg: bytes) -> bytes: """ Decrypt with eth private key Parameters ---------- receiver_pubhex: str Receiver's ethereum private key hex string msg: bytes Data to decrypt Returns ------- bytes Plain text """ pubkey = msg[0:65] # pubkey's length is 65 bytes encrypted = msg[65:] sender_public_key = hex2pub(pubkey.hex()) private_key = hex2prv(receiver_prvhex) aes_key = derive(private_key, sender_public_key) return aes_decrypt(aes_key, encrypted)
def encrypt( sender_privkey: str, receiver_pubkey: Union[str, bytes], msg: bytes, nonce: bytes, ) -> bytes: """Encrypt with receiver's secp256k1 public key :param sender_privkey: :param receiver_pubkey: :param msg: message to encrypt :param nonce: 16 byte nonce used in the aes encryption :return: encrypted data """ sender_privkey = hex2prv(sender_privkey) if isinstance(receiver_pubkey, str): receiver_pubkey = hex2pub(receiver_pubkey) elif isinstance(receiver_pubkey, bytes): receiver_pubkey = PublicKey(receiver_pubkey) else: raise TypeError("Invalid public key type") shared_key = encapsulate(sender_privkey, receiver_pubkey) cipher_text = aes_encrypt(shared_key, msg, nonce) return cipher_text
def encrypt( sender_privkey: str, receiver_pubkey: Union[str, bytes], msg: bytes, nonce: bytes, ) -> bytes: """ Encrypt with receiver's secp256k1 public key Parameters ---------- sender_privkey: bytes The senders secret key in byte form receiver_pubkey: Union[str, bytes] Receiver's public key (hex str or bytes) msg: bytes Data to encrypt nonce: bytes The nonce(16 bytes) used in the aes encryption Returns ------- bytes Encrypted data """ sender_privkey = hex2prv(sender_privkey) if isinstance(receiver_pubkey, str): receiver_pubkey = hex2pub(receiver_pubkey) elif isinstance(receiver_pubkey, bytes): receiver_pubkey = PublicKey(receiver_pubkey) else: raise TypeError("Invalid public key type") shared_key = encapsulate(sender_privkey, receiver_pubkey) cipher_text = aes_encrypt(shared_key, msg, nonce) return cipher_text