Question: Encode / Decode Program This program uses a rolling substitution method to encode and decode a text file. The program will translate each character in

Encode/Decode Program
This program uses a rolling substitution method to encode and decode a text file. The program will translate each character in a file to either the original character or a new character.
The program will prompt the user for a key. The program will create 1 cipher set for each character in the key. The program will use the cipher sets as it decodes or encodes each character of the input file.
Program Input
The program will use the following character set:
charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" charSet +="~`!@\#$%^&*()_+1234567890-={}|[]\\:\";\'<>?,./"
The program will prompt the user for the following (illustration demonstrates encoding a file):
"Enter e to ENCODE a file or d to DECODE a file: e"
"Enter the text file name to encode: myfile.txt"
" NOTE: A key is needed to encode myfile.txt.
" The key length must be between 2 and 20 characters."
"Enter the key to be used: HE"
Program Calculation
The program shift the character set appropriately for cipher sets using the user key. Program Output
Shown below is the program output and a snapshot of the Media folder after the program execution:
The program will display a welcome message followed by the output file name. Illustration shows encoding a file.
"Welcome to Encode/Decode Program"
"The output file is: encode-results.txt"
The output file will be named the following:
Encoded file name: encode-results.txt
Decoded file name: decode-results.txt
Programmer Notes
For this program it is very helpful to develop functions for the tasks at hand.
My program included the following functions: o main() prompts the user and calls the appropriate functions o buildBlocks() creates the cipher blocks o getCipherBlock() returns the next cipher block o codeFile() encodes or decodes a file
The program should loop on each prompt for correct user input
o e or d is entered to encode or decode a file
o the file name entered exists
o the length of the key provided by the user is between 2-20 characters
Arrays are passed by reference in Ruby. An array is created as shown:
myArray = Array.new(SIZE)
where SIZE is the length of the user provided key
String slicing can be accomplished like in Python to shift the character set in the creation of cipher block.
Static variables are supported by Ruby within functions. As an example blockPos is a static variable here:
@blockPos ||=0 # If not set, set it to 0
To increment the variable: @blockPos +=1
When reading a file, dont forget to chomp the newline character and watch for the end of the line. Here is a snippet of reading a file:
File.readlines(fileName).each do |line|
line.each_char do |oneChar|
if oneChar.chomp ==""
next
end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!