﻿ Caesar, Exclusive OR Method - C Language Learned Sufferingly
C language learned by suffering

### Caesar, Exclusive OR Method

##### What is encryption?
Encryption is a technique that transforms the original data so that its meaning is unknown.
However, it is meaningless if the meaning cannot be understood, so the assumption is that the original can be restored.
Restoring the original data is called decryption, and the original data itself is called plaintext.

The intention of encryption is, of course, to prevent data theft by third parties.
Therefore, unlike other algorithms, the emphasis is on cryptographic strength above all else.
No matter how fast, an algorithm that can be easily deciphered is of limited use.
##### Caesar's cipher
It is said that the first person in the world to use cryptography was the famous Caesar (Caesar).
The method is extremely simple: simply shift the letters by a few characters.

For example, if you shift CAESAR (Caesar) by one letter, you get DBFTBS, and so on.
By shifting one letter in the opposite direction, the original CAESAR can be derived.

The following program applies this to a generic binary file.

source code
`````` /* Usage
CodeCaesar(input filename, output filename, password);
Password must be a number in the range 0-255
If the password is a negative number, it can be decrypted.
*/
void CodeCaesar(char finame[], char foname[], int key)
{
FILE *fi, &*fo;
int value;

fi = fopen(finame, "rb");
if (fi == NULL) return;
fo = fopen(finame, "wb");
if (fo == NULL) return;

while ((value = getc(fi)) ! = EOF) {
putc(value + key, fo);
}

fclose(fi);
fclose(fo);
}``````

This cipher is easily deciphered by comparing the difference with the original data.
##### EXCLUSIVE-OR cipher
The Caesar cipher is simple and easily deciphered.
So, to make it a little more difficult to understand, we use exclusive or.

Keywords.
##### Exclusive OR]

A binary number calculation in which the output is zero when the same number is input.

Exclusive OR results in a seemingly bullshit number, which makes it suitable for encryption.
To compute exclusive or in C, use the ^ symbol.

The following program applies this to a generic binary file.

source code
`````` /* Usage
CodeExor(input file name, output file name, password);
*Password must be a number in the range 0-255.
*The same password can be used for decryption.
*/
void CodeExor(char finame[], char foname[], int key)
{
FILE *fi, &*fo;
int value;

fi = fopen(finame, "rb");
if (fi == NULL) return;
fo = fopen(finame, "wb");
if (fo == NULL) return;

while ((value = getc(fi)) ! = EOF) {
putc(value ^ key, fo);
}

fclose(fi);
fclose(fo);
}``````

This cipher cannot be deciphered at first glance, but
If you test 256 different passwords, they will be deciphered. (A computer can do this in an instant.)
With the encryption so far, there are only 256 possible passwords, so it is easy to decipher.
Therefore, longer passwords can be used.

The idea is simple. Simply repeat multiple numbers.
The following program applies this to a generic binary file.

Source Code
`````` /*
Usage
CodeExor(input file name, output file name, password string);
The password can be a string of any length.
The same password can be used for decryption.
*/
void CodeExorLong(char finame[], char foname[], char key[])
{
FILE *fi, *fo;
int value, i = 0;

fi = fopen(finame, "rb");
if (fi == NULL) return;
fo = fopen(finame, "wb");
if (fo == NULL) return;

while ((value = getc(fi)) ! = EOF) {
putc(value ^ key[i], fo);
i++;
if (key[i] == '\0') i = 0;
}

fclose(fi);
fclose(fo);
}``````

This allows the number of passwords to be increased beyond 256, but
Given the performance of today's computers, passwords of only a few characters can be quickly deciphered.
##### Application of pseudorandom numbers
The result of exclusive disjunction may appear to be a hoax, but in fact there is a clear regularity.
To prevent this, pseudorandom numbers are used to make the regularity difficult to understand.

Pseudorandom numbers keep returning the same result if the initial values are the same, so
If you set the initial value from the password, you will always get the same calculation result.

Random number algorithm
Different compilers have different random number algorithms in the standard library.
Therefore, different compilers will produce different calculation results.
To prevent this, you must also create your own random number algorithm.

There are various ways to create an initial value from a password, but in this case we will simply use the sum of the character code values.
The following program applies this to a generic binary file.

Source Code
`````` /*
CodeExorRandom(input filename, output filename, password string);
The password can be a string of any length.
The same password can be used for decryption.
*/
int GetRandom(int min, int max)
{
return min + (int)(rand() * (max - min + 1.0) / (1.0 + RAND_MAX));
}

void CodeExorRandom(char finame[], char foname[], char key[])
{
FILE *fi, *fo;
int value, early = 0, i;

fi = fopen(finame, "rb");
if (fi == NULL) return;
fo = fopen(finame, "wb");
if (fo == NULL) return;

for (i = 0; key[i] ! = '\0'; i++) {
early += key[i];
}

srand(early);

i = 0;
while ((value = getc(fi)) ! = EOF) {
putc(value ^ GetRandom(0, 255), fo);
i++;
if (key[i] == '\0') i = 0;
}

fclose(fi);
fclose(fo);
}``````

To decipher this, the method of calculating random numbers must be analyzed, which is impossible for the layman.
However, it could be deciphered by a reasonably knowledgeable person if he or she so chooses.

Practicality of this encryption
All of the encryption methods shown here are simple and can be deciphered if one is so inclined.
However, they are fast and easy to program.
They are practical enough to be incorporated into the save data of self-made games.

As a side note, save data can be easily modified even for commercial games.
In particular, some bishojo games can be easily modified by simply filling in the system save data with 0xFF.
CG and recollections can be viewed in some cases.

Comment
COMMENT

Open the 💬 comment submission box