C language base64 encryption

1. Program purpose

Use c/c++ to complete the following algorithm:

Enter a string and check the total ascii code value of the string. If the value is greater than 1000, the string is base64 encrypted and output, otherwise the string is XORed with the base64 encoding table and output

2. Program code

code show as below:

#include<stdio.h>
#include<string.h>
int main(){
    
    
	char x[1000]="",y[1000]="",z[1000]="";
	char base[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	int i,sum=0,len=0,j;
	printf("请输入字符串:");
	gets(x);
	len=strlen(x);
	for(i=0;i<len;i++){
    
    

		sum=sum+x[i];
	}

	for(i=0,j=0;j<len;i+=4,j+=3){
    
    
		y[i]=base[x[j]>>2];
		y[i+1]=base[(x[j]&0x3)<<4|(x[j+1]>>4)];
		y[i+2]=base[(x[j+1]&0xf)<<2|(x[j+2]>>6)];
		y[i+3]=base[(x[j+2]&0x3f)];
	}
    switch(len%3){
    
    
     case 1:{
    
    
        y[i-2]='=';
		y[i-1]='=';
		break;
    }
     case 2:
        {
    
    
        y[i-1]='=';
        break;
        }
    }

	if(sum>1000){
    
    
		printf("字符串的ASCII值为%d\n",sum);
		printf("输入的字符串ASCII值大于1000\n");
		printf("base64加密:");
		puts(y);
	}
	else{
    
    
		printf("输入的字符串ASCII值小于1000,将与base64编码表进行异或运算\n");
		printf("异或运算后的结果为:");
		for(i=0;i<len;i++){
    
    
			z[i]=x[i]^base[i];
		}
		puts(z);
	}

    }

3. Program analysis

1. Let’s talk about base64 encryption first.

Usually a byte is 8 bits, and base64 encryption is to make these 8 bits into 6 bits, for example, the binary corresponding to "ABC":

01000001 01000010 01000011

Converting to six digits is: (010000) (010100) (001001) (000011)

It is precisely that it has only six digits at most, 2 to the 6th power = 64, base64 is named after this, and there are a total of 64 characters in the base64 encoding table, as shown in the following table

Click to view image source

Through the above table, base64 encrypts "ABC" to get: QUJD

If there are two characters, such as the binary of "12": 00110001 00110010

Converting to a six-digit group is 001100 010011 001000 (not enough to automatically fill in 0)

Originally base64 encryption according to this rule is: MTI

But like this kind of string length of 2%3=2, you need to add '=', that is, the final encryption result is: MTI=

If the string length is 1, that is, 1%3=1, you need to add two '=' after encryption;

2. Code analysis

#include<stdio.h>
#include<string.h>
int main(){
	char x[1000]="",y[1000]="",z[1000]="";//初始化数组
	char base[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//引入base64编码表
	int i,sum=0,len=0,j;
	printf("请输入字符串:");
	gets(x);
	len=strlen(x);//获取字符串长度
	for(i=0;i<len;i++){

		sum=sum+x[i];
	}

This section is mainly to initialize the array, input the string and calculate the ASCII value of the string

for(i=0,j=0;j<len;i+=4,j+=3){
		y[i]=base[x[j]>>2];
		y[i+1]=base[(x[j]&0x3)<<4|(x[j+1]>>4)];
		y[i+2]=base[(x[j+1]&0xf)<<2|(x[j+2]>>6)];
		y[i+3]=base[(x[j+2]&0x3f)];
	}
    switch(len%3){
     case 1:{
        y[i-2]='=';
		y[i-1]='=';
		break;
    }
     case 2:
        {
        y[i-1]='=';
        break;
        }
    }

This section is to base64 encrypt the input string

I don't know what to say about the for loop theory, so I can only give a chestnut.

For example, input ABC, the corresponding binary is: 01000001 01000010 01000011, let the first group of binary shift two bits to the left, which is 010000, and get the first group of six-digit base 010000;

Then let 01000001 and 0x3 (that is, 11) be ANDed, and the last two digits of 01000001 are taken out, and then shifted to the left by four digits to get 010000, which is the first two digits of the second group of base, and then shifted to the right The four-digit 01000010 (that is, 0100) is ORed to obtain the second group of six-digit base 010100;

The third group of base is to let 01000010&0xf (that is, 1111) get 0010, then move two bits to the left to get 001000, move 01000011 to two bits to get 01, 001000|01 to get the third group base 001001;

The fourth group of base is 01000011&0x3f (that is, 111111) to get 000011

I will not talk about the code of adding '=', and I can only give a chestnut

For example, the input string is '1', after the for loop, the value of i is 4, that is, the length of the y array is 5, but the value of y[i-2] is empty, so add '=' from y[i-2] starts to assign values. Since there is no stored value in y[i], the length of the array is 4, and 1%3=1, which also conforms to the above code (it feels very messy, but I did my best QAQ )

if(sum>1000){
		printf("字符串的ASCII值为%d\n",sum);
		printf("输入的字符串ASCII值大于1000\n");
		printf("base64加密:");
		puts(y);
	}
	else{
		printf("输入的字符串ASCII值小于1000,将与base64编码表进行异或运算\n");
		printf("异或运算后的结果为:");
		for(i=0;i<len;i++){
			z[i]=x[i]^base[i];
		}
		puts(z);
	}

    }

ASCII value greater than 1000, base64 encryption and output

The ASCII value is less than 1000, and the original string is XORed with the base64 encoding table and output

4. Running results and verification

Program running result:
insert image description here

Online base64 encryption verification:

insert image description here

The ASCII value is less than 1000, and the original string is XORed with the base64 encoding table and output

Guess you like

Origin blog.csdn.net/m0_51295934/article/details/124482048