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


  [ stack ]                      is encryption  (   --> s )
  [ stack ]                      is decryption  (   --> s )
 
  [ [] 95 times [ i^ join ]
    shuffle encryption put ]     is makeencrypt (   -->   )
 
  [ encryption share
    0 95 of swap 
    witheach 
      [ i^ unrot poke ] 
    decryption put ]             is makedecrypt (   -->   )
 
  [ makeencrypt makedecrypt ]    is makekeys    (   -->   )
 
  [ witheach [ char ! + emit ] ] is echokey     ( s -->   )
 
  [ encryption release
    decryption release ]         is releasekeys (   -->   )
 
  [ [] swap witheach
      [ dup char ! char ~ 1+
        within if 
          [ char ! - 
            encryption share 
            swap peek char ! + ]
        join ] ]                 is encrypt     ( $ --> $ )
 
  [ [] swap witheach
      [ dup char ! char ~ 1+
        within if 
          [ char ! - 
            decryption share
            swap peek char ! + ]
        join ] ]                 is decrypt    ( $ --> $ )
 
  randomise
  makekeys
  say "Encryption key is: " encryption share echokey cr
  say "Decryption key is: " decryption share echokey cr
  cr
  $ "Encryption matters, and it is not just for spies and philanderers."
  say "Plaintext: " dup echo$ cr
  say "Encrypted: " encrypt dup echo$ cr
  say "Decrypted: " decrypt echo$ cr
  releasekeys