← Back to list

ROT13 Cipher: A Practical Guide with Kotlin

Sometimes, the best way to understand complex cryptography is to start with the absolute basics. Today, we’re going to look at one of the…

Halil Özel · 2026-05-26 18:27 · 0 claps · 2.5 min read paywalled
#rot13 #encryption #cybersecurity #kotlin #hacking
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 📱 · Mobile Development 🔒 · Cybersecurity

ROT13 Cipher: A Practical Guide with Kotlin

Rot13 Cipher

Rot13 Cipher

Sometimes, the best way to understand complex cryptography is to start with the absolute basics. Today, we’re going to look at one of the oldest and simplest tricks in the book: the ROT13 cipher. Whether you’re building a fun easter egg into your next Android app or just brushing up on algorithmic thinking, implementing this in Kotlin is a great exercise.

What is ROT13?

ROT13 (short for “rotate by 13 places”) is a substitution cipher that replaces a letter with the 13th letter after it in the Latin alphabet. Because there are 26 letters in the basic Latin alphabet, ROT13 is its own inverse. This means that to undo the cipher, you simply apply the exact same algorithm again.

In mathematical terms, ROT13 is an involution function, where applying the function twice returns the original input:

f(f(x))=x

It provides zero actual security by today’s standards — it was famously used on early internet forums to hide punchlines, movie spoilers, or puzzle solutions from a casual glance.

The Algorithm Breakdown

The logic is straightforward:

  1. Iterate through each character in a given string.
  2. Check if the character is a letter (A-Z or a-z).
  3. If it is, shift it forward by 13 positions.
  4. If the shift pushes the letter past ‘Z’ (or ‘z’), loop back around to the start of the alphabet.
  5. If the character is a number, symbol, or space, leave it exactly as is.

Elegant Kotlin Implementations

As Kotlin developers, we love concise, readable, and expressive code. Let’s look at a few ways to implement this.

1. The Classic Approach

Here is a straightforward function that relies on basic ASCII value manipulation.

fun rot13Classic(input: String): String {
    val result = StringBuilder()

    for (char in input) {
        when {
            char in 'a'..'z' -> {
                val shifted = if (char + 13 > 'z') char - 13 else char + 13
                result.append(shifted)
            }
            char in 'A'..'Z' -> {
                val shifted = if (char + 13 > 'Z') char - 13 else char + 13
                result.append(shifted)
            }
            else -> result.append(char) // Leave spaces and punctuation alone
        }
    }

    return result.toString()
}

2. The Idiomatic Extension Function

Why write a standalone function when you can make it a powerful extension on the String class? This approach utilizes Kotlin's functional APIs like map and joinToString for a much more expressive feel.

fun String.rot13(): String {
    return this.map { char ->
        when {
            char.isLowerCase() -> 'a' + (char - 'a' + 13) % 26
            char.isUpperCase() -> 'A' + (char - 'A' + 13) % 26
            else -> char
        }
    }.joinToString("")
}

// Usage:
// val secretMessage = "Hello from Istanbul!".rot13() 
// println(secretMessage) // Uryyb sebz Vfgnaohy!

Notice how using the modulo operator (% 26) cleans up the boundary logic beautifully.

3. Bonus: Using it in Jetpack Compose

If you’re implementing this inside a modern Android application, you might want to wire this up to a UI. Here’s a quick Jetpack Compose snippet showing how you could use our extension function in a simple screen:

@Composable
fun Rot13Screen() {
    var inputText by remember { mutableStateOf("") }

    Column(
        modifier = Modifier.padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        OutlinedTextField(
            value = inputText,
            onValueChange = { inputText = it },
            label = { Text("Enter text to encrypt/decrypt") }
        )

        Spacer(modifier = Modifier.height(16.dp))

        Text(
            text = "Result: ${inputText.rot13()}",
            style = MaterialTheme.typography.headlineSmall
        )
    }
}

Conclusion

ROT13 might not be protecting any state secrets anytime soon, but it remains a fantastic, lightweight way to practice string manipulation. By utilizing Kotlin’s extension functions, we can reduce a clunky algorithm into a clean, readable few lines of code.


메타데이터
post_id
959af75ebd59
slug
rot13-cipher-a-practical-guide-with-kotlin-959af75ebd59
url
https://medium.com/@halilozel1903/rot13-cipher-a-practical-guide-with-kotlin-959af75ebd59
canonical_url
https://medium.com/@halilozel1903/rot13-cipher-a-practical-guide-with-kotlin-959af75ebd59
author_url
https://medium.com/@halilozel1903
status
ok
fetched_at
2026-06-09 15:37:30