MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
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.)
Long password
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.



About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box