Junior — Middle
Can you explain what is meant by asymmetric encryption and how it works?
sobes.tech AI
Answer from AI
Asymmetric encryption is a cryptographic method that uses a pair of keys: a public key and a private key. The public key can be freely distributed, while the private key is kept secret.
How it works:
- The sender encrypts the message using the recipient's public key.
- The recipient decrypts the message with their private key.
This ensures data transmission security, as the message cannot be decrypted without the private key.
Example in Python using the cryptography library:
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
# Generate keys
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
message = b"Secret message"
# Encryption
ciphertext = public_key.encrypt(
message,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
)
# Decryption
plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None)
)
print(plaintext.decode()) # Will output: Secret message