0
0
Computer Networksknowledge~6 mins

Asymmetric encryption (RSA) in Computer Networks - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to send a secret message to a friend without anyone else reading it, but you can't meet to share a secret key first. This problem needs a way to lock and unlock messages using different keys so that only the right person can read them.
Explanation
Public and Private Keys
Asymmetric encryption uses two keys: one public and one private. The public key is shared openly and used to lock (encrypt) messages. The private key is kept secret and used to unlock (decrypt) messages. This way, anyone can send a secret message using the public key, but only the owner of the private key can read it.
Two different keys work together: one to lock messages and one to unlock them.
How RSA Works
RSA creates the public and private keys using two large prime numbers. These numbers are multiplied to form a key that is easy to share but hard to reverse without the private key. The security depends on how difficult it is to factor this large number back into primes.
RSA’s security relies on the difficulty of factoring large numbers made from two primes.
Encryption and Decryption Process
When someone wants to send a secret message, they use the receiver’s public key to encrypt it. The encrypted message looks like random data to anyone else. The receiver then uses their private key to decrypt and read the original message.
Encryption uses the public key, and decryption uses the private key to keep messages secure.
Digital Signatures
RSA can also prove who sent a message by reversing the process. The sender encrypts a message or its summary with their private key. Anyone with the sender’s public key can decrypt it to verify the sender’s identity and that the message was not changed.
RSA allows verifying message origin and integrity using digital signatures.
Real World Analogy

Imagine a locked mailbox where anyone can drop letters inside using a slot (public key), but only the mailbox owner has the key to open it and read the letters (private key). For sending proof of identity, the owner can put a special stamp only they can make, which others can check to confirm it’s genuine.

Public and Private Keys → Mailbox slot for dropping letters (public key) and mailbox key for opening it (private key)
How RSA Works → The mailbox’s unique lock made from a complex combination that’s easy to use but hard to copy
Encryption and Decryption Process → Dropping letters through the slot (encrypting) and opening mailbox to read letters (decrypting)
Digital Signatures → Special stamp on letters proving they came from the mailbox owner
Diagram
Diagram
┌───────────────┐        ┌───────────────┐
│ Sender        │        │ Receiver      │
│               │        │               │
│ Message       │        │               │
│               │        │               │
└──────┬────────┘        └──────┬────────┘
       │ Encrypt with Public Key      │
       │────────────────────────────>│
       │                             │
       │ Encrypted Message           │
       │                             │
       │ Decrypt with Private Key    │
       │<────────────────────────────│
       │                             │
       │ Original Message            │
       │                             │
       └─────────────────────────────┘
This diagram shows how a sender encrypts a message with the receiver’s public key and the receiver decrypts it with their private key.
Key Facts
Asymmetric EncryptionEncryption method using two different keys: public for encrypting and private for decrypting.
Public KeyA key shared openly to encrypt messages.
Private KeyA secret key used to decrypt messages encrypted with the matching public key.
RSAA widely used asymmetric encryption algorithm based on large prime number multiplication.
Digital SignatureA way to prove message origin and integrity by encrypting with a private key.
Code Example
Computer Networks
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

# Generate RSA keys
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()

# Encrypt message with public key
encryptor = PKCS1_OAEP.new(RSA.import_key(public_key))
message = b'Hello, RSA!'
ciphertext = encryptor.encrypt(message)

# Decrypt message with private key
decryptor = PKCS1_OAEP.new(RSA.import_key(private_key))
plaintext = decryptor.decrypt(ciphertext)
print(plaintext.decode())
OutputSuccess
Common Confusions
Believing the public key can decrypt messages.
Believing the public key can decrypt messages. The public key only encrypts messages; only the private key can decrypt them.
Thinking RSA encrypts data faster than symmetric encryption.
Thinking RSA encrypts data faster than symmetric encryption. RSA is slower and usually used to securely exchange symmetric keys, which then encrypt data faster.
Summary
Asymmetric encryption uses two keys: a public key to encrypt and a private key to decrypt messages securely.
RSA relies on the difficulty of factoring large numbers made from two prime numbers to keep keys safe.
RSA also supports digital signatures to verify who sent a message and ensure it was not changed.