Those who do not have high encryption requirements can define their own rules for encryption. This kind of encryption is very simple and free. For example, when you save a file, you can add a number to each character in the file, and then when reading the file, subtract that number accordingly for each character, that is, What can be achieved is simple encryption, so that the files you store look garbled. It’s just that this rule is too simple. You can set the rules yourself. Encryption and decryption just need to be aligned.
The following program uses XOR operations to encrypt and decrypt files
/****************** Design Idea*** ***************/
// According to the encrypted/confidential password entered by the user,
// Get the original file and A string of equal length and password
// Encrypt/decrypt by XORing the corresponding elements
// In addition, because the XOR method is used, encryption and decryption are the same Program
// Decrypt the file according to the same encryption
#include
#include #include #include #include char filename [256]; // Original file char password[256]; // Encryption/decryption password const char filenametemp[] = "temp15435255435325432543.temp"; // Encryption/ Decrypt intermediate files void inputpass(char *pass); //Password input is displayed with "******" void main() { FILE *fp; // Encrypted/decrypted file FILE *fptemp; // Temporary file during encryption/decryption process int pwdlen; // Password length
char ch = 0; // Characters read
printf("Please enter the file name to be encrypted/decrypted (full path name): \n");
gets(filename);
if( (fp = fopen(filename, "rb")) == NULL) {
printf("File %s\n not found", filename);
exit(1);
} // if
printf ("Please enter the password to encrypt/decrypt: \n");
inputpass(password);
pwdlen = strlen(password);
if (pwdlen == 0) {
printf("Password cannot be empty, encryption/decryption failed\n");
exit(1);
} // if
fptemp = fopen(filenametemp, "wb"); // Open the intermediate file
while(1) {
ch = fgetc( fp);//Read a character from the original file
if(feof(fp)) { // The end of the file has been read
break; // Exit the loop
}
ch ^= password[i++]; // XOR the original characters and the password
fputc(ch, fptemp); // XOR the original characters and password The result is written to the intermediate file
if(i == pwdlen) { // Make the original file XOR-encrypted with the same fixed length as the password length
i = 0;
}
} // while
fclose(fp); // Close and open the original file
fclose(fptemp); // Close and open Intermediate file
remove(filename); // Delete the original file
rename(filenametemp, filename); // Rename the intermediate file to the original file
printf("Encryption/decryption successful\n"); // Encryption/decryption successful so far
}
//Password input is displayed with "******" p>
void inputpass(char *pass) {
int i = 0;
char c;
while(isprint(c = getch( ))) {
pass[i++] = c;
// printf("*");
}
pass [i] = '\0';
printf("\n");
}