(* ************************************************************************ *** Nicholas Erho *** CMPT 141 ***** ID: 200613 *** November 3, 2004 *** ************************************************************************ Problem 8.13 # 57-58: Write a program module that will read a file, ask the user for a password, and then encrypt the file using the password and then write it to a new file. Then write the complement to the program to decrypt the file using the password. Compare to the original. Problem Suitability: A computer would be very useful in performing encryption and decryption in long a complicated files and messages, because although possible by hand it is far to tedious and in situations with complicated encryption would take a long time. As the fact that a computer can do many more opperations to complicate an encryption code means that it is much better to use for it give the ablity to encrypt using algorithms that would take alot of time to crack even using another computer. Problem Restated: Desgin a program to encrypt a file and then decrypt it, using a password as a key. Encrytion: Input: Sequence File. Output: Encrypted sequence file. Process: Encrypt the data. Decryption: Input: Encrypted sequence File. Output: Sequence file decrypted. Process: Decrypt the data. Problem Libraries: STextIO is used to write prompts on the screen and get some decisions from the user. Strings is used to work with the file names, password, and command line arguments. To open, read and write files StreamFile was implemented and ReadResults was used to determine the end of a file and to prevent errors. ProgramArgs was used to enable the reading of console arguments. Finally TextIO and IOResults were used when writing and reading data to files and checking for errors in this process. Problem Refinement 1: 1. Get command line arguments and if data provide okay then: a. Encrypt i. Read file and get ASCII value for each char. ii. Get the ascii value for each letter of the password. iii. Add the two values together and write an ASCII char to the outfile. b. decrypt i. Read encrypted file and get ASCII value for each char. ii. Get the ASCII value for each consecutive password character. iii. Add the values form i and ii together and print the ASCII char to a outfile. c. Print header info. Problem Refinement 2: 1. Retrive command line arguments. 2. Test if given file is okay to open. 3. Test if password is present. 4. See if command line arguments are present or a c or d is present. a. If no agruments are give then print header information inclosed in a procedure. b. If the argument c is given then run the encrypt procedure. c. If the argument d is given then run the decrypt procedure. b. Encrypt: 1. Open file and or create file that the new data is to be stored in (outfile). 2. Test if the file opened okay. 3. Read a char from the intial opened file (infile) and get the ordinal value of it. 4. Read a char form the outfile and get the ordinal value of it. 5. Add these values together plus 32 and turn the ascii number in to a character. 6. Write this character to the outfile 7. Loop 3-6 - until readresult is bad. 8. Close infile and outfile. c. Decrypt: All steps the same as in Encrypt except 5 where the password value and 32 are subtracted from the infile charvalue. Problem Data Table: * Vars : Arg, pass, and filename -> Short strings. outfile and infile -> ChanId. res -> OpenResults. readOPEN, passOPEN, writeOPEN, encrypt, and decrypt -> Booleans. ch and character -> Char. Line -> Long string. count -> Cardinal. * Imports : STextIO _-> WriteString, SkipLine, WriteLn, ReadChar. Strings -> Compare, CompareResults, Concat, Delete. StreamFile -> ChanId, Open, write, Close, read, old, OpenResults. SIOResult -> ReadResults. ProgramArgs -> ArgChan, IsArgPresent. TextIO -> WriteChar, ReadChar, SkipLine, WriteLn. IOResult -> ReadResult. Sample IO: Case 1: INPUT [command line] : -f:text.txt -p:1337isME -c INPUT [text.txt] : " Although many general things" OUTPUT [text.txt.crypt] : "Zͺ̷rrIJyͺŷr" Case 2: INPUT [command line] : -f:text.txt -p:1337isME -d INPUT [text.txt] : "Zͺ̷rrIJyͺŷr" OUTPUT [text.txt.crypt] : " Although many general things" Case 3: INPUT [command line] : "" OUTPUT [text.txt.crypt] : Header Information. User Manual: Description: This program is desgined to encrypt a file and then decrypt it later when the correct password is given. Operation: Use this program if in M$ DOS by typing the name of the program from the directory which it is located and then typing the approprate command line arguments. The arguments provide options for a varity of cases, these options can be displayed if the program is run with no argements. A banner will be displayed that loops like: CASE 1: No arguments Z:\CMPT 141\Second Half>encrypt ************************************************************************ *** Nicholas Erho *** CMPT 141 ***** ID: 200613 *** November 3, 2004 *** ************************************************************************ * Encrypter/Decrypter * ************************************************************************ * This program is desgined to encrypt a file and them decrypt a file * * when the proper password is given. The encrypted file is stored in * * a file of the same name as the input file, however an extention has * * been added to reveal to the user that the it is as encrypted file. * * Please note this program will not delete the origional file. * ************************************************************************ * Encrypt [-f:[file] -p:[password] [-c || -d]] * * * * -f Specifies the file to be processed * * -p Specifies the password--a password must be used * * and case matters. * * -c Indicates for the program to encrypt the file. * * -d Indicates for the program to decrypt the file. * ************************************************************************ As seem in from this banner -f:[file] indicates the file you with data to be coming from; so if the file you wish to encrypt or decrypt is called text.txt you would write "-f:text.txt". In a simmilar way -p:[password] indicates the password which you want your files to be protected with, for example "-p:8f9n3k". Its important to note that case DOES matter and you MUST have a password. The last argument can either be a -c or a -d. The -c tells the program to encrypt, while the -d tells the program to decrypt. You should NOT include both in a single argument, but you must have one. These two arguments detemine the remaining cases. CASE 2: -f:[file] -p:[password] -c Z:\CMPT 141\Second Half>encrypt -f:text.txt -p:8f9n3k -c This will automatically encrypt your file and store the result in a file with the same name but an extention of ".crypt". You are responcible to remove your source file. If the [filename].crypt file already exist your will be prompted with: Do you want to overwrite existing file (Y/N)? X Where X is your answer. If you chose 'y' then the file will be writen over; however, if you chose 'n' the program will exit not damaging your file, but also not encrypting the source. CASE 3: -f:[file] -p:[password] -d Z:\CMPT 141\Second Half>encrypt -f:text.txt.crypt -p:1499237002 -d This will automatically decrypt the secified file. But do note that the file must have an extention of .crpyt or it will be regected. This is to ensure that the file being decrypted was accually encrypted in the first place. If a file with the name of the encrypted file exists with a name with out the .crypt then you will be prompted with: Do you want to overwrite existing file (Y/N)? X Where X can be either 'y' or 'n'. If X is 'n' the program will exit and the file will remain as it intially was, but your source file will still be encrypted, however if is equal to 'y' the program will procced and over write the existing file. Its important to note that the longer the password the more complicated the encryption and that it is possible to re-encrypt an encrypted file, but for every step of encryption a step of unencryption must be taken to aquire the origional file. Finally, you can not encrypt files with spaces between the paragraphs. *) MODULE Encrypt; (* Modules imported form*) FROM STextIO IMPORT WriteString, SkipLine, WriteLn, ReadChar; FROM Strings IMPORT Compare, CompareResults, Concat, Delete; FROM StreamFile IMPORT ChanId, Open, write, Close, read, old, OpenResults; FROM SIOResult IMPORT ReadResults; FROM ProgramArgs IMPORT ArgChan, IsArgPresent; IMPORT TextIO; (* Modules for later qualified import*) IMPORT IOResult; TYPE String = ARRAY [0..1000] OF CHAR; (* Long to pick up an entire line of from a file*) ShortString = ARRAY [0..40] OF CHAR; (* Short because more effecient than use of long*) VAR Arg, pass, filename : ShortString; outfile, infile : ChanId; res : OpenResults; (* to store the result of an open attempt *) readOPEN, passOPEN, writeOPEN, encrypt, decrypt : BOOLEAN; (* Decrypts the input file using the password as the key*) PROCEDURE decrypter(file, password:ShortString); VAR filename : ShortString; ch : CHAR; Line: String; count : CARDINAL; character : CHAR; BEGIN (* Makes sure file is of right type, by looking at extention*) filename := file; Delete(file, 0, LENGTH(filename)-6); IF NOT (Compare(file, ".crypt" ) = equal) THEN WriteString("This file is not of .crypt type and thus can not be decrypted because it"); WriteLn; WriteString("has not been encrypted."); WriteLn; (*------------------------------------------------------*) ELSE (* Opens file with proper name (aka with out .crpyt on it and make sure its open*) Delete(filename, LENGTH(filename)-6, 6); Open (outfile, filename, write, res); IF res = opened THEN writeOPEN := TRUE; (* --------------------------------------------------- *) (* If file alread exists ask user if they want to overwrite*) ELSE WriteString ("Do you want to overwrite existing file (Y/N)? "); ReadChar(ch); SkipLine; IF CAP(ch) = 'Y' THEN Open (outfile, filename, old+write, res); IF res = opened THEN writeOPEN := TRUE; ELSE WriteString("Could not open output file."); WriteLn; END; END; END; (*-------------------------------------------------------*) (*Decryption and file I/O*) IF writeOPEN AND readOPEN AND passOPEN THEN (*Check to make sure every things okay*) REPEAT TextIO.ReadRestLine(infile, Line); (* Gets line from infile *) IF IOResult.ReadResult (infile) = allRight THEN (* ------------------------------------------- *) (* If read okay, decrypt by take ordinal values from letters and the password and subtracting them and 32 and writeing that new chr() value to the out file*) FOR count := 0 TO (LENGTH(Line)-1) DO character := CHR( (ORD(Line[count])-ORD(password[count MOD LENGTH(password)])-32)); TextIO.WriteChar(outfile, character); END; TextIO.WriteLn(outfile); (* When end of line is hit, write a line in file *) TextIO.SkipLine(infile); END; (* Do this untill all proper lines of the file are read *) UNTIL IOResult.ReadResult (infile) # allRight; (* Close both files*) Close(infile); Close(outfile); END; END END decrypter; (* Encrypts the file using the password as the locking key. *) PROCEDURE encrypter(file, password: ShortString); VAR filename : ShortString; ch, character : CHAR; Line : String; count: CARDINAL; BEGIN (* Open or create a file with the filename given + .crypt *) Concat(file, ".crypt", filename); Open (outfile, filename, write, res); IF res = opened THEN writeOPEN := TRUE; (* ---------------------------------- *) (* If the file alread exists ask to over write it*) ELSE WriteString ("Do you want to overwrite existing file (Y/N)? "); ReadChar(ch); SkipLine; IF CAP(ch) = 'Y' THEN Open (outfile, filename, old+write, res); IF res = opened THEN writeOPEN := TRUE; ELSE WriteString("Could not open output file."); WriteLn; END; END; END; (*---------------------------------------------*) (*Encryption and file I/O*) IF writeOPEN AND readOPEN AND passOPEN THEN(*checks to make sure every thing is okay*) REPEAT TextIO.ReadRestLine(infile, Line); (* Reads a line from the source *) (* ---------------------------------------------------- *) (* If read okay, encrypt by take ordinal values from letters and the password and adding them and 32 and writeing that new chr() value to the out file*) IF IOResult.ReadResult (infile) = allRight THEN FOR count := 0 TO (LENGTH(Line)-1) DO character := CHR( (ORD(Line[count])+ORD(password[count MOD LENGTH(password)]))+32); TextIO.WriteChar(outfile, character); END; (* Writes a line in crypted file if line exists in source file*) TextIO.WriteLn(outfile); TextIO.SkipLine(infile); (* To clear read buffer for next read *) END; UNTIL (IOResult.ReadResult (infile) # allRight); (* Close open files*) Close(infile); Close(outfile); END; END encrypter; PROCEDURE Header; (* My header: Nothing complicated here *) BEGIN WriteString ("************************************************************************"); WriteLn; WriteString ("*** Nicholas Erho *** CMPT 141 ***** ID: 200613 *** November 3, 2004 ***"); WriteLn; WriteString ("************************************************************************"); WriteLn; WriteString ("* Encrypter/Decrypter *"); WriteLn; WriteString ("************************************************************************"); WriteLn; WriteString ("* This program is desgined to encrypt a file and them decrypt a file *"); WriteLn; WriteString ("* when the proper password is given. The encrypted file is stored in *"); WriteLn; WriteString ("* a file of the same name as the input file, however an extention has *"); WriteLn; WriteString ("* been added to reveal to the user that the it is as encrypted file. *"); WriteLn; WriteString ("* Please note this program will not delete the origional file. *"); WriteLn; WriteString ("************************************************************************"); WriteLn; WriteString ("* Encrypt [-f:[file] -p:[password] [-c || -d]] *"); WriteLn; WriteString ("* *"); WriteLn; WriteString ("* -f Specifies the file to be processed *"); WriteLn; WriteString ("* -p Specifies the password--a password must be used *"); WriteLn; WriteString ("* and case matters. *"); WriteLn; WriteString ("* -c Indicates for the program to encrypt the file. *"); WriteLn; WriteString ("* -d Indicates for the program to decrypt the file. *"); WriteLn; WriteString ("************************************************************************"); END Header; BEGIN (* Sets default values *) encrypt := FALSE; decrypt := FALSE; (* Prints the header if no arguments are given.*) IF NOT IsArgPresent() THEN Header; ELSE REPEAT (* Collect all args *) TextIO.ReadToken(ArgChan(), Arg); IF (IOResult.ReadResult (ArgChan()) = allRight) THEN (* Gets file arg and checks to see if file opens okay*) IF (Arg[1] = 'f') THEN Delete(Arg, 0, 3); filename := Arg; Open (infile, filename, read, res); IF res = opened THEN readOPEN := TRUE; ELSE WriteString("Input file could not be opened."); WriteLn; END; END; END; (* Gets password argument and modifies it so that it is proper, by cutting of "-p:"*) IF (Arg[1] = 'p') THEN Delete(Arg, 0, 3); pass := Arg; IF LENGTH(pass)> 0 THEN passOPEN := TRUE; ELSE (* Checks to make sure a password is entered *) WriteString("No password entered."); WriteLn; passOPEN := FALSE; END; END; (* sets the condtions for decryption to run*) IF (Arg[1] = 'd') THEN decrypt := TRUE; END; (* Sets the conditions for encryption to run*) IF (Arg[1] = 'c') THEN encrypt := TRUE; END; UNTIL IOResult.ReadResult (ArgChan()) # allRight; END; IF encrypt AND NOT decrypt THEN encrypter(filename, pass); (* Runs the encrypt procedure*) END; IF decrypt AND NOT encrypt THEN decrypter(filename, pass); (* Runs the decrypt procedure *) END; (* Make sure that only either -c or -d are present but not both or none.*) IF (encrypt AND decrypt) OR (NOT encrypt AND NOT decrypt) THEN Header; END; END Encrypt. (* Sample Runs: Z:\CMPT 141\Second Half>encrypt ************************************************************************ *** Nicholas Erho *** CMPT 141 ***** ID: 200613 *** November 3, 2004 *** ************************************************************************ * Encrypter/Decrypter * ************************************************************************ * This program is desgined to encrypt a file and them decrypt a file * * when the proper password is given. The encrypted file is stored in * * a file of the same name as the input file, however an extention has * * been added to reveal to the user that the it is as encrypted file. * * Please note this program will not delete the origional file. * ************************************************************************ * Encrypt [-f:[file] -p:[password] [-c || -d]] * * * * -f Specifies the file to be processed * * -p Specifies the password--a password must be used * * and case matters. * * -c Indicates for the program to encrypt the file. * * -d Indicates for the program to decrypt the file. * ************************************************************************ Z:\CMPT 141\Second Half>encrypt -f:text.txt -p:1fe323h -c Z:\CMPT 141\Second Half>encrypt -f:text.txt.crypt -p:1fe323h -d Do you want to overwrite existing file (Y/N)? y SOURCE FILE: Although many general things can be said about problem solving, the focus of this book is on the programming of computers so as to employ them as tools in the process. Of course, as they are manufactured, computing machines have only very limited capabilities. Computers do not pose problems, analyse them, or find solutions. They only run the programs that embody the solutions discovered by some human programmer, and for this, the hardware alone is insufficient. The addition of a programming notation (or language) such as Modula-2 produces a system capable of using the programmer's instructions to solve problems. All the intelligent planning to take use of this system once it has been assembled is entirely a human responsibility. Computers can do only a few simple things, but they do them very quickly, so it is possible to employ them for rather sophisticated tasks. The programmer must set the task for the computer to perform with each step carefully detailed in some such notation--the complete sequence of which steps is termed a program. All the work this requires is worthwhile because the machine can execute the steps so quickly that one is still ahead of the game--despite the many hours it may take to plan and write down the necessary instructions. ENCRYPTED FILE: qsrȹrsssss~s륹Ħr rqsƻq¹s¿rqs¨̨rq¾ƨǺԹr Ÿqsƻʦŷs~sƼ򴵻s̨rs浻ƀs sq´srsŨrٻ̨rs ĴĦǨ¶̨sƨs̨rs}sǺ} sŷsrqǹǶqr쥴rr{æ|r rqǿ~rǷqsǺĴzsǶrq ɷs翷ƿs를qssqsƻĦƨr ꥼsŷq祻鿦 qsrǷqrqsqrr~sǺqsƻ̨}rqs ƻrrŨŨǷŁsqsǺr sƻqsrƸqŷss¿r~r þssrqſqrssƻĦsƨ Ǻsr򴵻륶qrs¨qs를sr ǺƸsʦqsq澷ss륷qrƳq Ł RECONSTRUCTED FILE: Although many general things can be said about problem solving, the focus of this book is on the programming of computers so as to employ them as tools in the process. Of course, as they are manufactured, computing machines have only very limited capabilities. Computers do not pose problems, analyse them, or find solutions. They only run the programs that embody the solutions discovered by some human programmer, and for this, the hardware alone is insufficient. The addition of a programming notation (or language) such as Modula-2 produces a system capable of using the programmer's instructions to solve problems. All the intelligent planning to take use of this system once it has been assembled is entirely a human responsibility. Computers can do only a few simple things, but they do them very quickly, so it is possible to employ them for rather sophisticated tasks. The programmer must set the task for the computer to perform with each step carefully detailed in some such notation--the complete sequence of which steps is termed a program. All the work this requires is worthwhile because the machine can execute the steps so quickly that one is still ahead of the game--despite the many hours it may take to plan and write down the necessary instructions. *)