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


 
# Project : Substitution Cipher
 
plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext = "ZEBRASCDFGHIJKLMNOPQTUVWXY"
test = "flee at once. we are discovered!"
encrypt = "SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!"
 
see "Plaintext : " + plaintext + nl
see "Ciphertext : " + ciphertext + nl
see "Test : " + test + nl
see "Encoded : "
encodetext = encode(test)
see encodetext + nl
see "Decoded : "
decodetext = decode(encodetext)
see decodetext + nl
 
func encode(test)
        str = ""
        for n = 1 to len(test)
              pos = substr(plaintext, upper(test[n]))
              if test[n] = " "
                 str = str + " "
              elseif test[n] = "!"
                 str = str + "!"
              elseif test[n] = "."
                 str = str + "."
              else
                 str = str + substr(ciphertext, pos, 1)
              ok
        next
        return str
 
func decode(test)
        str = ""
        for n = 1 to len(encodetext)
              pos = substr(ciphertext, upper(encodetext[n]))
              if test[n] = " "
                 str = str + " "
              elseif test[n] = "!"
                 str = str + "!"
              elseif test[n] = "."
                 str = str + "."
              else
                 str = str + lower(substr(plaintext, pos, 1))
              ok
        next
        return str
 