Bacon cipher: Difference between revisions

→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 461:
=={{header|Kotlin}}==
The 'full' Bacon alphabet, which has separate letters for i, j, u and v, has been used in the following:
<lang scala>//object versionBacon 1.0.6{
private val codes = mapOf(
 
'a' to "AAAAA", 'b' to "AAAAB", 'c' to "AAABA", 'd' to "AAABB", 'e' to "AABAA",
object Bacon {
private val codes = mapOf('af' to "AAAAAAABAB", 'bg' to "AAAABAABBA", 'ch' to "AAABAAABBB", 'di' to "AAABBABAAA", 'ej' to "AABAAABAAB",
'fk' to "AABABABABA", 'gl' to "AABBAABABB", 'hm' to "AABBBABBAA", 'in' to "ABAAAABBAB", 'jo' to "ABAABABBBA",
'kp' to "ABABAABBBB", 'lq' to "ABABBBAAAA", 'mr' to "ABBAABAAAB", 'ns' to "ABBABBAABA", 'ot' to "ABBBABAABB",
'pu' to "ABBBBBABAA", 'qv' to "BAAAABABAB", 'rw' to "BAAABBABBA", 'sx' to "BAABABABBB", 'ty' to "BAABBBBAAA",
'uz' to "BABAABBAAB", 'v ' to "BABABBBBAA", 'w'// touse "BABBA",' 'x' to "BABBB",denote 'y'any to "BBAAA",non-letter
)
'z' to "BBAAB", ' ' to "BBBAA") // use ' ' to denote any non-letter
 
fun encode(plainText: String, message: String): String {
Line 476:
for (c in pt)
if (c in 'a'..'z') sb.append(codes[c])
else sb.append(codes[' '])
val et = sb.toString()
val mg = message.toLowerCase() // 'A's to be in lower case, 'B's in upper case
sb.setLength(0)
var count = 0
for (c in mg)
if (c in 'a'..'z') {
if (et[count] == 'A') sb.append(c)
else sb.append(c - 32) // upper case equivalent
count++
if (count == et.length) break
} else sb.append(c)
else sb.append(c)
return sb.toString()
}
Line 496 ⟶ 495:
for (c in message)
when (c) {
in 'a'..'z' -> sb.append('A')
in 'A'..'Z' -> sb.append('B')
}
val et = sb.toString()
sb.setLength(0)
for (i in 0 until et.length step 5) {
val quintet = et.substring(i, i + 5)
Line 512 ⟶ 511:
fun main(args: Array<String>) {
val plainText = "the quick brown fox jumps over the lazy dog"
val message = "bacon's cipher is a method of steganography created by francis bacon." +
"this task is to implement a program for encryption and decryption of " +
"plaintext using the simple alphabet of the baconian cipher or some " +
"other kind of representation of this alphabet (make anything signify anything). " +
"the baconian alphabet may optionally be extended to encode all lower " +
"case characters individually and/or adding a few punctuation characters " +
"such as the space."
val cipherText = Bacon.encode(plainText, message)
println("Cipher text ->\n\n$cipherText")
val decodedText = Bacon.decode(cipherText)