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
EncodeDecode 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 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 ~@#$&:;
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 and 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 EncodeDecode Program"
"The output file is: encoderesults.txt
The output file will be named the following:
Encoded file name: encoderesults.txt
Decoded file name: decoderesults.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 characters
Arrays are passed by reference in Ruby. An array is created as shown:
myArray Array.newSIZE
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 # If not set, set it to
To increment the variable: @blockPos
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.readlinesfileNameeach do line
line.eachchar 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
