diff --git a/src/main/kotlin/Encryption/Caesar.kt b/src/main/kotlin/Encryption/Caesar.kt new file mode 100644 index 0000000..4c9cc4d --- /dev/null +++ b/src/main/kotlin/Encryption/Caesar.kt @@ -0,0 +1,34 @@ +package encryption +object Caesar { + fun encrypt(s: String, key: Int): String { + val offset = key % 26 + if (offset == 0) return s + var d: Char + val chars = CharArray(s.length) + for ((index, c) in s.withIndex()) { + if (c in 'A'..'Z') { + d = c + offset + if (d > 'Z') d -= 26 + } + else if (c in 'a'..'z') { + d = c + offset + if (d > 'z') d -= 26 + } + else + d = c + chars[index] = d + } + return chars.joinToString("") + } + + fun decrypt(s: String, key: Int): String { + return encrypt(s, 26 - key) + } +} + +fun main(args: Array) { + val encoded = Caesar.encrypt("InteliJ IDEA Community Edition", 10) + println(encoded) + val decoded = Caesar.decrypt(encoded, 10) + println(decoded) +} diff --git a/src/test/kotlin/Encryption/CaesarEncTest.kt b/src/test/kotlin/Encryption/CaesarEncTest.kt new file mode 100644 index 0000000..330446a --- /dev/null +++ b/src/test/kotlin/Encryption/CaesarEncTest.kt @@ -0,0 +1,35 @@ +import encryption.Caesar +import org.junit.Test + +class CaesarEncryptionTest{ + + @Test + fun testWithKotlinStringAndKey8() { + val string = "Kotlin is a powerful programming language" + val encoded = Caesar.encrypt(string, 8) + assert(Caesar.decrypt(encoded,8)== string) + + } + @Test + fun testWithIntellIjStringAndKey10() { + val string = "InteliJ IDEA Community Edition" + val encoded = Caesar.encrypt(string, 10) + assert(Caesar.decrypt(encoded,10)== string) + + } + + @Test + fun testWithAlgorithmjStringAndKey3() { + val string = "Algorithm-Repo" + val encoded = Caesar.encrypt(string, 3) + assert(Caesar.decrypt(encoded,3)== string) + + } + + + + + + + +}