Kotlinlearncs.online LogoJava
Return to List

Solve: Rot 13 Encryption

Created By: Geoffrey Challen
/ Version: 2020.9.0

Encryption is an ancient practice of trying to conceal information by scrambling it. Modern encryption techniques are incredibly strong and mathematically sound. But in the past, simpler and more primitive methods were used.

Let's implement a form of encryption known as a Caesar Cipher, sometimes also known as Rot-13 encryption. (Rot for rotation, and 13 for one amount that you might rotate.) Here is how it works. Given a String and an amount to rotate, we replace each character in the String with a new character determined by rotating the original character in a given array. For example, given the String array "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ", "ABC" rotated 3 would be "DEF", and rotated -1 would be " AB". (Note the space at the end of the character array.)

Declare and implement a function called encrypt that, given a String and an Int amount, returns the passed String "encrypted" by rotating it the given amount. ("Encrypted" is in scare quotes because this is not by any means a strong method of encryption!) If the passed String is null you should return null. Note that rotation may be negative, which will require some additional care.

fun encrypt(
input: String?,
rotation: Int,
): String? {
val characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
return input
}

Related Lessons

Stuck? You may find these lessons helpful: