keepcalculator

06 Numbers and Powers

Public and Private Keys & Hashing Explained

Card No. 06·53282 · Updated · Standard formula, shown below

Public and private keys are a pair of cryptographic keys used to encrypt and decrypt data, sign documents, and prove identity, while hashing is a one-way mathematical function that converts any input into a fixed-size string of characters. This article explains how both mechanisms work, how they differ, and why they are fundamental to modern digital security.

What Are Public and Private Keys?

Public and private keys are an asymmetric key pair used in public-key cryptography. The public key is shared openly, while the private key is kept secret. Data encrypted with the public key can only be decrypted with the corresponding private key, and vice versa. This enables secure communication over insecure channels, digital signatures, and authentication.

For example, when you visit a secure website (HTTPS), your browser uses the server’s public key to encrypt a session key, which only the server’s private key can decrypt. Similarly, when you sign an email, your private key creates a unique digital signature that anyone with your public key can verify.

How Public-Key Cryptography Works

Public-key cryptography relies on mathematical functions that are easy to compute in one direction but hard to reverse without the private key. The most common algorithm is RSA, which uses the product of two large prime numbers. The public key consists of the product (n) and an exponent (e), while the private key includes the prime factors and a decryption exponent (d).

Encryption: C = M^e mod n (where M is the plaintext message, e is the public exponent, n is the modulus, and C is the ciphertext).

Decryption: M = C^d mod n (where d is the private exponent).

The security relies on the difficulty of factoring large numbers. For example, RSA-2048 uses a 2048-bit modulus, which is computationally infeasible to factor with current technology. Another popular algorithm is Elliptic Curve Cryptography (ECC), which offers similar security with shorter keys, making it ideal for mobile devices and IoT.

How Hashing Works

A hash function takes an input (or ‘message’) and returns a fixed-size string of bytes, typically a hexadecimal number. The output is called a hash, digest, or checksum. Key properties of a cryptographic hash function include:

  • Deterministic: The same input always produces the same hash.
  • Fixed output size: Regardless of input length, the hash has a constant length (e.g., 256 bits for SHA-256).
  • Preimage resistance: Given a hash, it is infeasible to find the original input.
  • Second preimage resistance: Given an input, it is infeasible to find a different input with the same hash.
  • Collision resistance: It is infeasible to find two different inputs that produce the same hash.

For example, the SHA-256 hash of “Hello” is 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969. Even a tiny change, like “hello” (lowercase), produces a completely different hash: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.

Common Uses of Hashing

Hashing is used in numerous applications:

  • Password storage: Instead of storing plaintext passwords, systems store hashes. When you log in, the entered password is hashed and compared to the stored hash. This way, even if the database is breached, passwords remain protected.
  • Data integrity: Downloading a file often shows a checksum (e.g., SHA-256) to verify the file hasn’t been corrupted or tampered with.
  • Digital signatures: Instead of signing the entire message, which could be huge, the message is hashed and the hash is signed. This is faster and more efficient.
  • Blockchain: Each block contains the hash of the previous block, creating an immutable chain.
  • Deduplication: Systems can detect duplicate files by comparing hashes.

Public Keys, Private Keys, and Hashing Together

In practice, these technologies often work together. For example, in a digital signature:

  1. The sender hashes the message to create a digest.
  2. The sender encrypts the digest with their private key, creating the signature.
  3. The recipient decrypts the signature using the sender’s public key, obtaining the digest.
  4. The recipient hashes the received message independently.
  5. If both digests match, the signature is valid and the message hasn’t been altered.

This combines the efficiency of hashing (small data to encrypt) with the authenticity of public-key cryptography.

Common Problems and Misconceptions

Misconception: Hashing is encryption. Encryption is reversible (with the key), hashing is not. Hashing is a one-way function.

Misconception: Longer hash means more secure. While longer hashes are generally more resistant to brute-force collisions, the algorithm’s design matters more. SHA-256 (256-bit) is considered secure, while MD5 (128-bit) is broken.

Problem: Hash collisions. Although extremely unlikely, two different inputs can produce the same hash. Cryptographic hash functions are designed to make this computationally infeasible, but it’s not impossible. For example, the SHA-1 algorithm has known collision attacks, so it’s deprecated.

Problem: Password hashing without salt. If two users have the same password, they’ll have the same hash. Adding a unique ‘salt’ (random data) to each password before hashing prevents this and makes rainbow table attacks ineffective.

Choosing the Right Algorithm

For hashing, current best practices recommend:

  • SHA-256 or SHA-3 for general-purpose integrity checks.
  • bcrypt, scrypt, or Argon2 for password storage, as they are deliberately slow to resist brute-force attacks.
  • Avoid MD5 and SHA-1 for security-sensitive applications due to known vulnerabilities.

For public-key cryptography, RSA-2048 or ECC with a 256-bit curve (like secp256k1) are widely used. The choice depends on your performance needs and compatibility.

Safety and Best Practices

  • Never share your private key. Anyone with it can impersonate you or decrypt your data.
  • Use strong, unique passwords for key stores and wallets.
  • Keep software and cryptographic libraries up to date to avoid known vulnerabilities.
  • Use hardware security modules (HSMs) or secure enclaves for high-value keys.
  • When storing passwords, always use a salted, slow hash function.
  • Verify checksums when downloading software to ensure integrity.

Frequently Asked Questions

Can a public key be used to derive the private key?

No, not with current algorithms. The security of public-key cryptography relies on the mathematical difficulty of reversing the one-way function. For RSA, that means factoring the modulus; for ECC, it’s solving the discrete logarithm problem. With sufficiently large key sizes, this is computationally infeasible.

What is the difference between symmetric and asymmetric encryption?

Symmetric encryption uses the same key for both encryption and decryption (like AES). Asymmetric encryption uses a key pair (public and private). Symmetric is faster but requires a secure way to share the key; asymmetric solves the key distribution problem but is slower.

Why is hashing not reversible?

Hash functions are designed to be one-way. They discard information during computation, so multiple inputs can map to the same output (though collisions are rare). Additionally, the output is fixed size, so it’s impossible to reconstruct a variable-length input from a fixed-length hash.

What is a salt in password hashing?

A salt is a random string added to a password before hashing. It ensures that identical passwords produce different hashes, preventing attackers from using precomputed tables (rainbow tables) and making it harder to crack multiple passwords at once.

Is SHA-256 still secure?

Yes, as of now, SHA-256 is considered cryptographically secure. It’s widely used in TLS, blockchain, and digital signatures. However, for long-term security, SHA-3 is also a good choice. Always follow current recommendations from security experts.

What happens if I lose my private key?

If you lose your private key, you lose access to anything encrypted with your public key, and you cannot sign documents. There is no recovery mechanism without a backup. This is why it’s crucial to store private keys securely and have backup copies in safe locations.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →