From ed38deaff31d7805f81707c1fbd9fbc688481189 Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:32:24 +0530 Subject: [PATCH 1/4] Adding code for Caesar encryption and decryption --- src/main/kotlin/Caesar.kt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/kotlin/Caesar.kt diff --git a/src/main/kotlin/Caesar.kt b/src/main/kotlin/Caesar.kt new file mode 100644 index 0000000..16912ee --- /dev/null +++ b/src/main/kotlin/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) +} \ No newline at end of file From 41ca88779fee5a8e28e7c09353a74011422615c6 Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:33:10 +0530 Subject: [PATCH 2/4] Creating folder for encryption --- src/main/kotlin/{ => Encryption}/Caesar.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/kotlin/{ => Encryption}/Caesar.kt (96%) diff --git a/src/main/kotlin/Caesar.kt b/src/main/kotlin/Encryption/Caesar.kt similarity index 96% rename from src/main/kotlin/Caesar.kt rename to src/main/kotlin/Encryption/Caesar.kt index 16912ee..4c9cc4d 100644 --- a/src/main/kotlin/Caesar.kt +++ b/src/main/kotlin/Encryption/Caesar.kt @@ -31,4 +31,4 @@ fun main(args: Array) { println(encoded) val decoded = Caesar.decrypt(encoded, 10) println(decoded) -} \ No newline at end of file +} From 03f85880f4240fcddd771e0d620330bcd09892fc Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:34:11 +0530 Subject: [PATCH 3/4] Adding test cases for Caesar encryption --- src/test/kotlin/CaesarEncTest.kt | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/kotlin/CaesarEncTest.kt diff --git a/src/test/kotlin/CaesarEncTest.kt b/src/test/kotlin/CaesarEncTest.kt new file mode 100644 index 0000000..7e8dd6f --- /dev/null +++ b/src/test/kotlin/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) + + } + + + + + + + +} \ No newline at end of file From 8bba3b90b052dd1aa31dbf926008dc83d84de04f Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:34:50 +0530 Subject: [PATCH 4/4] Adding test folder for Encryption --- src/test/kotlin/{ => Encryption}/CaesarEncTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/kotlin/{ => Encryption}/CaesarEncTest.kt (95%) diff --git a/src/test/kotlin/CaesarEncTest.kt b/src/test/kotlin/Encryption/CaesarEncTest.kt similarity index 95% rename from src/test/kotlin/CaesarEncTest.kt rename to src/test/kotlin/Encryption/CaesarEncTest.kt index 7e8dd6f..330446a 100644 --- a/src/test/kotlin/CaesarEncTest.kt +++ b/src/test/kotlin/Encryption/CaesarEncTest.kt @@ -32,4 +32,4 @@ class CaesarEncryptionTest{ -} \ No newline at end of file +}