Playfair cipher: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 878: Line 878:
}
}


class Playfair(val keyword: String, val pfo: PlayfairOption) {
class Playfair(keyword: String, val pfo: PlayfairOption) {
private val table: Array<CharArray> = Array(5, { CharArray(5) }) // 5 x 5 char array
private val table: Array<CharArray> = Array(5, { CharArray(5) }) // 5 x 5 char array


Line 943: Line 943:
}
}


public fun encode(plainText: String): String {
fun encode(plainText: String): String {
val cleanText = getCleanText(plainText)
val cleanText = getCleanText(plainText)
var cipherText = ""
var cipherText = ""
var length = cleanText.length
val length = cleanText.length
for (i in 0 until length step 2) {
for (i in 0 until length step 2) {
val (row1, col1) = findChar(cleanText[i])
val (row1, col1) = findChar(cleanText[i])
Line 960: Line 960:
}
}


public fun decode(cipherText: String): String {
fun decode(cipherText: String): String {
var decodedText = ""
var decodedText = ""
var length = cipherText.length
val length = cipherText.length
for (i in 0 until length step 3) { // cipherText will include spaces so we need to skip them
for (i in 0 until length step 3) { // cipherText will include spaces so we need to skip them
val (row1, col1) = findChar(cipherText[i])
val (row1, col1) = findChar(cipherText[i])
Line 971: Line 971:
else -> table[row1][col2].toString() + table[row2][col1]
else -> table[row1][col2].toString() + table[row2][col1]
}
}
if (i < length - 1) decodedText += " "
if (i < length - 1) decodedText += " "
}
}
return decodedText
return decodedText
}
}


public fun printTable() {
fun printTable() {
println("The table to be used is :\n")
println("The table to be used is :\n")
for (i in 0..4) {
for (i in 0..4) {