Vigenère cipher: Difference between revisions
Content deleted Content added
m →{{header|Sidef}}: updated code |
Added Kotlin |
||
Line 1,229: | Line 1,229: | ||
<pre>LXFOPVEFRNHR |
<pre>LXFOPVEFRNHR |
||
ATTACKATDAWN</pre> |
ATTACKATDAWN</pre> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.1.3 |
|||
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 = "VIGENERECIPHER" |
|||
val text = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" |
|||
val encoded = vigenere(text, key) |
|||
println(encoded) |
|||
val decoded = vigenere(encoded, key, false) |
|||
println(decoded) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY |
|||
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH |
|||
</pre> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |