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


' FB 1.05.0 Win64
 
' uses same alphabet and key as Ada language example
Const string1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Const string2 = "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
 
Sub process(inputFile As String, outputFile As String, encrypt As Boolean)
   Open inputFile For Input As #1
   If err > 0 Then
     Print "Unable to open input file"
     Sleep
     End
   End If
   Dim As String alpha, key 
   If encrypt Then
     alpha = string1 : key = string2
   Else
     alpha = string2 : key = string1
   End If     
   Open outputFile For Output As #2
   Dim s As String
   Dim p As Integer
   While Not Eof(1)
     Line Input #1, s
     For i As Integer = 0 To Len(s) - 1  
       If (s[i] >= 65 AndAlso s[i] <= 90) OrElse (s[i] >= 97 AndAlso s[i] <= 122) Then
         p =  Instr(alpha, Mid(s, i + 1, 1)) - 1
         s[i] = key[p]         
       End If      
     Next 
     Print #2, s
   Wend
   Close #1 : Close #2
End Sub
 
process "plain.txt", "encrypted.txt", true
process "encrypted.txt", "decrypted.txt", false
Print
Print "Press any key to quit"
Sleep