M
M
Max Kot2020-02-10 17:46:38
Encryption
Max Kot, 2020-02-10 17:46:38

Encryption and decryption by gamma method in pascal, where is the error in the code?

It is necessary to write a program for encrypting and decrypting the text using the gamma method in pascal, I myself am not strong in it and started to google, I found a ready-made code, somehow commented out the code as I understood it myself, but there is an error somewhere, thank you very much in advance, code:

// Create variables for the program
Var A, B, C: array[0..256] of longint;
G: array[0..256] of real;
i: integer;
action: byte;
z: char;
Fin, Fout, Foutout: text;
begin // Beginning of the program
Write('What are we going to do, encrypt(1) or decrypt(2) the file: '); readln(action); // Choice between encryption (1) and decryption (2)
case action of // Open case by user input

1: begin // Start encryption
Assign(Fin, 'Fin.txt'); Reset(Fin); // Fin.txt is a txt file that is stored in the project directory where we need to write the text we want to encrypt
Assign(Fout, 'Fout.txt'); Rewrite(Fout); // Fout.txt is a txt file that is stored in the directory with the project in which it will be created and the encryption result will be written to it
Write('Enter the values ​​A (INT): '); readln(a[0]); // Enter number A for encryption
Write('Enter values ​​B (INT): '); readln(b[0]); // Enter number B for encryption
Write('Enter values ​​C (INT): '); readln(c[0]);
Read(Fin, z); // Read character by character from file
i:= i+1; // Counter
a[i]:=(171*a[i-1]) mod 30269; // Operation with the entered variable A
b[i]:=(172*b[i-1]) mod 30307; // Operation with the entered variable B
c[i]:=(170*c[i-1]) mod 30323; // Operation with the entered C variable
G[i]:=(a[i]/30269+b[i]/30307+c[i]/30323)*100; // This will be a floating point number
if G[i]>256 then G[i]:=G[i]-255; //Condition if g[i] is greater than 256 then g[i] is equal to g[i]-255
Write(Fout, chr(ord(z) xor trunc(G[i]))); // Write file
if Eoln(Fin) then begin Readln(Fin); WriteIn(Fout); end; // If the end of the line, then read the next line and write from a new line
end; // End of cycle
Close(Fin); Close(Fout); // Closing both txt files
end; // End of encryption


2: begin // Start of decryption
Assign(Fout, 'Fout.txt'); Reset(Fout); // Fout.txt is a txt file that is stored in the project directory where we need to write the text we want to decrypt
Assign(Foutout, 'Foutout.txt'); Rewrite(Foutout); // Foutout.txt is a txt file that is stored in the directory with the project in which it will be created and the result of decryption will be written to it
Write('Value A for encryption (INT): '); readln(a[0]); // Enter the number A that was written in encryption
Write('Value B in encryption (INT): '); readln(b[0]); // Enter the number B that was written in encryption
Write('C value for encryption (INT): '); readln(c[0]); // Enter the number C that was written in the encryption
while not Eof(Fout) do // Loop until the file ends
while not eoln(Fout) do begin // Loop until the line ends
Read(Fout, z); // Character-by-character reading from the file
i:=i+1; // Counter
a[i]:=(171*a[i-1]) mod 30269; // Operation with the entered variable A
b[i]:=(172*b[i-1]) mod 30307; // Operation with the entered variable B
c[i]:=(170*c[i-1]) mod 30323; // Operation with the entered C variable
G[i]:=(c[i]/30323+b[i]/30307+a[i]/30269)*100; // This will be a floating point number
if G[i]>256 then G[i]:=G[i]-255; //Condition if g[i] is greater than 256 then g[i] is equal to g[i]-255
Write(Foutout, chr(trunc(G[i]) xor ord(z))); // Write file
if Eoln(Fout) then begin Readln(Fout); WriteIn(Foutout); end; // If the end of the line, then read the next line and write from a new line
end; // End of loop
Close(Fout); Close(Foutout); // Closing both txt files
end; // End of decryption
end; // end case
end. // End of program

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question