Skip to content

Vigenere encryption #82

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

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
24 changes: 24 additions & 0 deletions src/main/kotlin/Encryption/VigenereEncryption.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fun vigenere(text: String, key: String, encrypt: Boolean = true): String {
val t = if (encrypt) text.toUpperCase() else text
val sb = StringBuilder()
var ki = 0
for (c in t) {
if (c !in 'A'..'Z') continue
val ci = if (encrypt)
(c.toInt() + key[ki].toInt() - 130) % 26
else
(c.toInt() - key[ki].toInt() + 26) % 26
sb.append((ci + 65).toChar())
ki = (ki + 1) % key.length
}
return sb.toString()
}

fun main(args: Array<String>) {
val key = "LEMON"
val text = "ATTACKATDAWN"
val encoded = vigenere(text, key)
println(encoded)
val decoded = vigenere(encoded, key, false)
println(decoded)
}
29 changes: 29 additions & 0 deletions src/test/kotlin/Encryption/VigEncTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import org.junit.Test
import java.util.*

class VigEncTest {
@Test
fun testWithAttackAtDawnString() {
val secretText = "ATTACKATDAWN"
val key = "LEMON"
val encoded = vigenere(secretText, key)
assert(vigenere(encoded, key, false)==secretText)

}
@Test
fun testWithIntellIjString() {
val secretText = "INTELLIJJIDEA"
val key = "CIPHER"
val encoded = vigenere(secretText, key)
assert(vigenere(encoded, key, false)==secretText)

}
@Test
fun testWithAlgorithmjString() {
val secretText = "ALGORITHMREPO"
val key = "LEMON"
val encoded = vigenere(secretText, key)
assert(vigenere(encoded, key, false)==secretText)

}
}