C language to achieve encryption and decryption function

Time:2021-10-15

Encryption mainly processes the original content through an algorithm to make the original content invisible. The decryption process usually needs to know the encryption algorithm, and then reverse process the encrypted content to realize the decryption function. Of course, decryption also has some methods of violent cracking. Next, take C language as an example to explain a simple method of encryption, decryption and brute force string cracking, which takes you into the door of encryption and decryption technology.

Let’s talk about Caesar encryption first. According to legend, Caesar password is the encryption system used by Caesar the great in ancient Rome to protect important military information. It is a replacement password, which plays an encryption role by pushing the alphabetical order back. If the alphabetical order is pushed back 3 digits, the letter a will be pushed as the letter D and the letter B will be pushed as the letter E. This example is similar to Caesar encryption.

Encryption algorithm:

Large (small) letters are also written for large (small) letters after encryption. The large (small) write letter is pushed back by N bits, and N is input by the user. If it exceeds the ASCII range of the large (small) write letter, it returns to the first large (small) write letter to continue the cycle.

Decryption algorithm (opposite to encryption algorithm):

Large (small) letters are also written for large (small) letters after decryption. The large (small) write letter is pushed forward by N bits, and N is input by the user. If it exceeds the ASCII range of the large (small) write letter, it returns to the last large (small) write letter to continue the cycle.

The code is as follows:

/*String encryption and decryption program Caesar encryption*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
//The function encode() pushes the alphabetical order back n bits to realize the file encryption function
void encode(char str[],int n){
 char c;
 int i;
 For (I = 0; I < strlen (STR); + + I) {// traversal string
 c=str[i];
 If (c > ='a '& & C < ='z') {// C is a lowercase letter
  If (c + n% 26 < ='z ') {// if it does not exceed the range of lowercase letters after encryption
  str[i]=(char)(c+n%26); // Encryption function
  }Else {// after encryption, it exceeds the range of lowercase letters. Cycle lowercase letters from the beginning
  str[i]=(char)(c+n%26-26);
  }
 }Else if (c > ='a '& & C < ='z') {// C is a capital letter
  If (c + n% 26 < ='z ') {// after encryption, it does not exceed the range of uppercase letters
  str[i]=(char)(c+n%26);
  }Else {// after encryption, the range of uppercase letters is exceeded. Cycle uppercase letters from the beginning
  str[i]=(char)(c+n%26-26);
  }
 }Else {// not a letter, not encrypted
  str[i]=c;
 }
 }
 printf("\nAfter encode: \n");
 puts(str); // Output encrypted string
}
 
 
//Decode() realizes the decryption function and moves the alphabetical order forward by N bits
void decode(char str[],int n){
 char c;
 int i;
 //Traversal string
 for(i=0;i<strlen(str);++i){
 c=str[i];
 //C is a lowercase letter
 if(c>='a' && c<='z'){
  //After decryption, it is also lowercase letters, which can be decrypted directly
  if(c-n%26>='a'){
  str[i]=(char)(c-n%26);
  }else{
  //After decryption, it will not be lowercase letters, and it will be processed into lowercase letters through circular lowercase letters
  str[i]=(char)(c-n%26+26);
  }
 }Else if (c > ='a '& & C < ='z') {// C is a capital letter
  If (C-N% 26 > ='a ') {// it is also uppercase after decryption
  str[i]=(char)(c-n%26);
  }Else {// after decryption, it will not be uppercase letters. Circulate uppercase letters and process them as uppercase letters
  str[i]=(char)(c-n%26+26);
  }
 }Else {// non letter processing
  str[i]=c;
 }
 }
 printf("\nAfter decode: \n");
 puts(str); // Output decrypted string
}//The function code is redundant and can be improved by readers
 
int main()
{
 char str[50];
 int k=0,n=0,i=1;
 printf("\nPlease input strings: ");
 scanf("%s",str); // Enter encryption and decryption string
 //Print menu
 printf("-----------------\n");
 printf("1: Encryption\n");
 printf("2: Decryption\n");
 printf("3: Violent Crack\n"); // brute force 
 printf("-----------------\n");
 printf("\nPlease choose: ");
 scanf("%d",&k);
 If (k = = 1) {// encryption
 printf("\nPlease input number: ");
 scanf("%d",&n);
 encode(str,n);
 }Else if (k = = 2) {// decrypt
 printf("\nPlease input number: ");
 scanf("%d",&n);
 decode(str,n);
 }else{
 For (I = 1; I < = 25; + + I) {// try brute force cracking on all possible n values
  printf("%d ",i);
  decode(str,i);
 }
 }
 return 0;
}

The test run is as follows:

The encryption and decryption function is successfully realized. Readers can complete the encryption and decryption of files with a little modification

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.