Image encryption

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
//
// Created by szj on 2021/4/22.
//
int main(a) {
    printf("C language image encryption.... \n");
    // Image encryption and decryption

    // The address to be encrypted
    char *flieUrl = "/ Users/shizhenjiang/Desktop/picture/C Flie test/android. The jpeg." ";
    // Encrypted address
    char *newFlieUrl = "/ Users/shizhenjiang/Desktop/picture/C Flie test/newAndroid. The jpeg." ";

    // key (encrypted by key)
    char *password = "12345";

    //TODO binary reads the old file and writes to the new file to generate a new encrypted file
    // Parameter one: file path
    "R" reads the file "w" writes the file "rb" reads the file as binary "rw" writes the file as binary
    FILE *file = fopen(flieUrl, "rb");
    FILE *newFlie = fopen(newFlieUrl, "wb");

    if(! file || ! newFlie) {printf("Failed to open, please check path %s", flieUrl);
        exit(0);    // End the program
    }

    // Receive the value read
    int c;
    // Set the key
    int index = 0;

    //TODO EOF = end of flie
    while((c = fgetc(file)) ! = EOF) {//CC is the final key
        int cc = password[index++ % strlen(password)];
        // While writing to the file,
        fputc(c ^ cc, newFlie);
    }
    printf("File encryption successful!! \n");
    printf("Original file encryption path :%s\n", flieUrl);
    printf("New file encryption path :%s", newFlieUrl);
    // File closed
    fclose(file);
    fclose(newFlie);

    return 0;
}
Copy the code
  • FlieUrl requires an encrypted image address
  • NewFlieUrl encrypts the generated image address

Encryption format:

 char *password = "12345";
Copy the code

Encrypt according to ‘key’;

Image decryption

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
//
// Created by szj on 2021/4/22.
//

int main(a) {
    printf("C language image decryption.... \n");
    // Image encryption and decryption

    // The address to be encrypted
    char *flieUrl = "/ Users/shizhenjiang/Desktop/picture/C Flie test/newAndroid. The jpeg." ";
    // Encrypted address
    char *newFlieUrl = "/ Users/shizhenjiang/Desktop/picture/C Flie test/newAndroid2. The jpeg." ";



    //TODO binary reads the old file and writes to the new file to generate a new encrypted file
    // Parameter one: file path
    "R" reads the file "w" writes the file "rb" reads the file as binary "rw" writes the file as binary
    FILE *file = fopen(flieUrl, "rb");
    FILE *newFlie = fopen(newFlieUrl, "wb");

    if(! file || ! newFlie) {printf("Failed to open, please check path %s", flieUrl);
        exit(0);    // End the program
    }
    // key (encrypted by key)
    char *password = "12345";

    // Receive the value read
    int c;
    // Set the key
    int index = 0;

    //TODO EOF = end of flie
    while((c = fgetc(file) )! = EOF) {//CC is the final key
        int cc = password[index++ % strlen(password)];
        // While writing to the file,
        fputc(c ^ cc, newFlie);
    }
    printf("Decrypted file successfully!! \n");
    printf("File decryption path :%s\n", flieUrl);
    printf("New path to decrypt file :%s", newFlieUrl);
    // File closed
    fclose(file);
    fclose(newFlie);

    return 0;
}
Copy the code
  • FlieUrl encrypted image address (must be encrypted image to decrypt)
  • NewFlieUrl decrypted image address

Note:

  • The encryption and decryption keys must be the same!
  • Only encrypted images can be decrypted

Original is not easy, your praise is my biggest support ~