Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caesar encryption #77

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/kotlin/Encryption/Caesar.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
val encoded = Caesar.encrypt("InteliJ IDEA Community Edition", 10)
println(encoded)
val decoded = Caesar.decrypt(encoded, 10)
println(decoded)
}
35 changes: 35 additions & 0 deletions src/test/kotlin/Encryption/CaesarEncTest.kt
Original file line number Diff line number Diff line change
@@ -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)

}







}