示例#1
0
文件: crypto.py 项目: remyers/lnproxy
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)
示例#2
0
    def __init__(self, server_sk=None, server_pk=None):
        if server_sk is not None:
            self.server_sk = PrivateKey.from_pem(server_sk)

        self.server_sk = server_sk
        self.server_pk = None

        if server_pk is not None:
            if isinstance(server_pk, str):
                self.server_pk = hex2pub(server_pk)
            elif isinstance(server_pk, bytes):
                self.server_pk = PublicKey(server_pk)
            else:
                raise TypeError("Invalid public key type")
示例#3
0
def encrypt(receiver_pubhex: str, msg: bytes) -> bytes:
    """
    Encrypt with eth public key

    Parameters
    ----------
    receiver_pubhex: str
        Receiver's ethereum public key hex string
    msg: bytes
        Data to encrypt

    Returns
    -------
    bytes
        Encrypted data
    """
    disposable_key = generate_key()
    receiver_pubkey = hex2pub(receiver_pubhex)
    aes_key = derive(disposable_key, receiver_pubkey)
    cipher_text = aes_encrypt(aes_key, msg)
    return disposable_key.public_key.format(False) + cipher_text
示例#4
0
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)
示例#5
0
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)
示例#6
0
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
示例#7
0
文件: crypto.py 项目: remyers/lnproxy
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