Get a Pentest and security assessment of your IT network.

Cyber Security

Public Key Cryptography: Encryption & Decryption

TL;DR

No, you can’t use either key for both encryption and decryption in asymmetric (public-key) cryptography. The public key encrypts data, but only the corresponding private key decrypts it. Trying to use the public key to decrypt or the private key to encrypt will not work.

Understanding Asymmetric Cryptography

Asymmetric cryptography uses a pair of keys: a public key and a private key. These keys are mathematically linked, but knowing one doesn’t easily reveal the other. Think of it like a lock and key – anyone can use the lock (public key) to secure something, but only someone with the correct key (private key) can open it.

Step-by-Step: Encryption & Decryption

  1. Encryption with the Public Key: Anyone wanting to send you a secret message uses your public key. This scrambles the message into an unreadable format.
    • Example (using OpenSSL):
    • openssl enc -aes-256-cbc -in plaintext.txt -out encrypted.enc -pubkey public.pem
  2. Decryption with the Private Key: Only you, possessing your private key, can decrypt the message.
    • Example (using OpenSSL):
    • openssl enc -aes-256-cbc -in encrypted.enc -out decrypted.txt -privatekey private.pem
  3. Why it Works: The mathematical functions used ensure that data encrypted with the public key can *only* be decrypted by its corresponding private key, and vice versa.

What Happens if You Try to Use the Wrong Key?

If you attempt to decrypt using the public key instead of the private key, or encrypt with the private key instead of the public key, you will get garbage data. The decryption process won’t produce meaningful output.

Key Differences Summarised

  • Public Key: Used for encryption and verifying signatures.
    • Can be freely distributed without compromising security.
  • Private Key: Used for decryption and creating signatures.
    • Must be kept secret at all costs. Compromising the private key compromises all data encrypted with its corresponding public key.

Common Algorithms

Popular asymmetric cryptography algorithms include:

  • RSA (Rivest–Shamir–Adleman)
  • ECC (Elliptic Curve Cryptography)
  • DSA (Digital Signature Algorithm)
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation