Substitution Cipher Implementation - File Encryption/Decryption



Task

Encrypt a input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file.

This type of Encryption/Decryption scheme is often called a Substitution Cipher.



Related tasks

 Caesar cipher
 Rot-13
 Vigenère Cipher/Cryptanalysis


See also

 Wikipedia: Substitution cipher


// version 1.0.6
 
object SubstitutionCipher {
    val key = "]kYV}(!7P\$n5_0i R:?jOWtF/=-pe'AD&@r6%ZXs\"v*N[#wSl9zq2^+g;LoB`aGh{3.HIu4fbK)mU8|dMET><,Qc\\C1yxJ"
 
    fun encode(s: String): String {
        val sb = StringBuilder()
        for (c in s) sb.append(key[c.toInt() - 32])
        return sb.toString()
    }
 
    fun decode(s: String): String {
        val sb = StringBuilder()
        for (c in s) sb.append((key.indexOf(c) + 32).toChar())
        return sb.toString()
    }
}
 
fun main(args: Array<String>) {
    val s = "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
    val enc = SubstitutionCipher.encode(s)
    println("Encoded:  $enc")
    println("Decoded:  ${SubstitutionCipher.decode(enc)}")
}