-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandlingRailfence.pas
98 lines (85 loc) · 2.51 KB
/
FileHandlingRailfence.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
unit FileHandlingRailfence;
interface
procedure writeFile(inputFilePath: string);
{ Writes the content of a text file }
procedure encryptFileRailfence(inputFilePath: string; outputFilePath: string;
rails: integer);
{ Encrypts a text file with railfence }
procedure decryptFileRailfence(inputFilePath: string; outputFilePath: string;
rails: integer);
{ Encrypts a text file with railfence }
procedure saveToFile(outputFilePath: string; data: string);
{ saves text to file }
implementation
uses
SysUtils,
railFence;
procedure writeFile(inputFilePath: string);
var
fileLine: string;
inputFile: textfile;
begin
assignfile(inputFile, inputFilePath);
reset(inputFile); // open file
writeln('');
writeln('Contents of file: ');
while not eof(inputFile) do // read and write each line of the file
begin
readln(inputFile, fileLine);
writeln('> ', fileLine);
end;
writeln('');
closefile(inputFile); // close file
end;
procedure encryptFileRailfence(inputFilePath: string; outputFilePath: string;
rails: integer);
var
outputFile, inputFile: textfile;
fileContents, fileLine: string;
begin
assignfile(outputFile, outputFilePath);
assignfile(inputFile, inputFilePath);
// assign both file paths to respective vars
reset(inputFile); // open input file
while not eof(inputFile) do
begin
readln(inputFile, fileLine); // read in a line
fileContents := fileContents + fileLine; // add it to filecontents
end;
closefile(inputFile); // close input file
fileContents := encryptRail(rails, fileContents); // encrypt file contents
rewrite(outputFile); // open output
write(outputFile, fileContents); // write filecontents to output
closefile(outputFile); // close the output file
end;
procedure decryptFileRailfence(inputFilePath: string; outputFilePath: string;
rails: integer);
{ Same as above, but decrypting }
var
outputFile, inputFile: textfile;
fileContents, fileLine: string;
begin
assignfile(outputFile, outputFilePath);
assignfile(inputFile, inputFilePath);
reset(inputFile);
while not eof(inputFile) do
begin
readln(inputFile, fileLine);
fileContents := fileContents + fileLine;
end;
closefile(inputFile);
fileContents := decryptRail(rails, fileContents);
rewrite(outputFile);
write(outputFile, fileContents);
closefile(outputFile);
end;
procedure saveToFile(outputFilePath: string; data: string);
var
outputFile: textfile;
begin
assignfile(outputFile, outputFilePath);
rewrite(outputFile);
write(outputFile, data);
closefile(outputFile);
end;
end.