What Is Cryptography and How Does It Work? A Beginner’s Explanation

Table of Contents

If you’ve ever entered a password, paid online, or chatted on WhatsApp, you’ve already used cryptography — whether you knew it or not.
 It’s the invisible lock protecting your private information from hackers and eavesdroppers.

In this beginner-friendly guide, you’ll learn what cryptography is, how it works, and see a Kotlin example in action.
 No complex math — just a clear explanation you can actually understand.

What Is Cryptography?

Cryptography is the science of securing information so only the intended people can read it.
 Think of it as putting your message in a sealed envelope, but one that only the right person has the key to open.

The term comes from the Greek words:

  • kryptos — hidden
  • graphein — writing

Put together: hidden writing.

Why Is Cryptography Important?

Without cryptography, sending data online would be like shouting your secrets in a crowded room.
 Here’s where it plays a role every day:

  • Online banking: Keeps credit card and transaction data safe.
  • Messaging apps: WhatsApp, Signal, and Telegram use strong end-to-end encryption.
  • Passwords: Stored securely so hackers can’t read them.
  • Data privacy: Protects personal files, medical records, and government documents.

The Core Principles of Cryptography

  1. Confidentiality — Only the intended person can read the message.
  2. Integrity — Ensures the message isn’t altered along the way.
  3. Authentication — Confirms the identity of sender and receiver.
  4. Non-repudiation — Prevents someone from denying they sent a message.

How Does Cryptography Work?

At a basic level, cryptography takes:

  • Plaintext — normal readable data
  • Key — a secret or public piece of information
  • Encryption algorithm — a method to scramble the plaintext

It turns plaintext into ciphertext (scrambled text).
 Only someone with the correct key can reverse the process through decryption.

Two Main Types of Cryptography

1. Symmetric Key Cryptography

  • Same key for encryption and decryption.
  • Faster but needs secure key sharing.
  • Example: AES (Advanced Encryption Standard).

2. Asymmetric Key Cryptography

  • Two keys: public (to encrypt) and private (to decrypt).
  • You can share the public key openly.
  • Example: RSA encryption.

Kotlin Example: Caesar Cipher

Let’s write a simple Caesar Cipher in Kotlin — one of the earliest encryption methods.
 It shifts letters in the alphabet by a fixed number.

Kotlin
fun encrypt(text: String, shift: Int): String {
    val result = StringBuilder()

    for (char in text) {
        if (char.isLetter()) {
            val base = if (char.isUpperCase()) 'A' else 'a'
            val shifted = ((char - base + shift) % 26 + base.code).toChar()
            result.append(shifted)
        } else {
            result.append(char) // Keep spaces and punctuation unchanged
        }
    }
    return result.toString()
}

fun decrypt(text: String, shift: Int): String {
    return encrypt(text, 26 - shift) // Reverse the shift
}

fun main() {
    val message = "Hello World"
    val shiftKey = 3
    val encryptedMessage = encrypt(message, shiftKey)
    val decryptedMessage = decrypt(encryptedMessage, shiftKey)
    println("Original: $message")
    println("Encrypted: $encryptedMessage")
    println("Decrypted: $decryptedMessage")
}

How the Kotlin Code Works

encrypt() function

  • Loops over each character.
  • If it’s a letter, shifts it forward by the shift value.
  • Keeps spaces and punctuation as they are.

decrypt() function

  • Reverses the shift to get back the original message.

Example output:

Kotlin
Original: Hello World
Encrypted: Khoor Zruog
Decrypted: Hello World

Real-Life Uses of Cryptography

  • Secure websites: HTTPS uses cryptography to protect data.
  • Digital signatures: Prove a file or message is genuine.
  • Blockchain: Relies on cryptographic hashing for security.

Conclusion

Cryptography is the backbone of digital security.
 It’s what keeps your passwords, bank details, and personal messages safe in a world where cyber threats are everywhere.
 While the math behind it can get deep, the basic idea is simple: scramble information so only the right person can read it.

Skill Up: Software & AI Updates!

Receive our latest insights and updates directly to your inbox

Related Posts

error: Content is protected !!