Simple encryption / decryption algorithm _ / c ++

        About encryption and decryption problem, some encryption algorithm does not exist decryption algorithm (the reason is irreversible encryption algorithm, that can not be reduced by the ciphertext algorithm), there is some encryption and decryption algorithm (because of its encryption process is reversible , i.e., the ciphertext may be reduced by the reverse algorithm). However, a separate encryption algorithm may not be reliable, then various combinations of encryption algorithms may be used. As for associated encryption algorithm you own to understand.

        Here's a simple encryption / decryption algorithm example, want this encryption and decryption algorithms have some knowledge: 

        Introduces encryption algorithms: encryption primarily calculated as '^' Cause '^' operation is reversible, as binary 1 = 1 ^ 0, ^ 1 = 0 1; 0 1 = 1 ^, 0 ^ 0 = 0;

  Therefore, 1 = 1 ^ 0 ^ 0, that is, the encrypted part [1 ^ 0] = R & lt, decryption section is [R & lt ^ 0], so that the ciphertext decryption algorithm to the encrypted data during the time 0 '^' operator, so it can achieve a number of binary encryption and decryption. Instruction is executed in the calculation of binary numbers, where it is necessary to understand why decimal and ASCII codes may also be used '^' encryption and decryption arithmetic .`

        c ++ source code: 

                example_1:

#include <iostream>

#define KEY 1313113

using namespace std;


//加密算法
int encrypt(int src_pass, int key){
    return src_pass^key;
}

//解密算法
int decrypt(int des_pass, int key){
    return des_pass^key;
}

int main(){
    int src_pass = 2000;
    cout<<src_pass<<endl;
    int des_pass = encrypt(src_pass, KEY);
    cout<<des_pass<<endl;
    int result = decrypt(des_pass, KEY);
    cout<<result<<endl;
    return 0;
}

         example2:

  

#include <iostream>
#include <windows.h>
#define L 6

using namespace std;

//原始数据
int mess[L] = {-23, -28, -19, -19, -18, -10};

//对原始数据进行加密
//加密算法
void encrypt(char *arr, int len){
    for(int i = 0; i < len; i++){
       arr[i] |= -0x80;	//希望读者了解ascii^-0x80,这里等价于 ascii^0x80
       arr[i] ^= 0x1;
    }
}

//解密算法
void decrypt(char *arr, int len){
    for(int i = 0; i < len; i++){
        arr[i] ^= 0x1;	
        arr[i] ^= 0x80;	//对ascii最高位进行了加密'^'运算,当然也要进行解密'^'运算
    }
}

bool judge(char* arr, int index){
    if(index < 0){
        return true;
    }else{
        if((int)arr[index] == mess[index]){
            judge(arr, index-1);
        }else{
            return false;
        }
    }
    return true;
}

//测试用
void print_(char *arr, int len){
    for(int i = 0; i < len; i++){
        cout<<(int)arr[i]<<", ";
    }
    cout<<endl;
}

//测试用
string print(char *arr, int len){
    string tmp = "";
    for(int i = 0; i < len; i++){
        tmp += arr[i];
    }
    return tmp;
}

//测试时的源代码
int main()
{
    char *arr = new char[L];
    cout<<"please input the password: ";
    for(int i = 0; i < L; i++){
        cin>>arr[i];
    }
    string tmp = "";
    tmp = print(arr, L);
    cout<<tmp<<endl;
    encrypt(arr, L);
    print_(arr, L);
    tmp = print(arr, L);
    cout<<tmp<<endl;
    decrypt(arr, L);

    tmp = print(arr, L);
    cout<<tmp<<endl;
    if(judge(arr, L-1)){
        cout<<"you is the user, hello!!"<<endl;
    }else{
        cout<<"you are not the user!!!"<<endl;
    }
    system("pause");
    return 0;
}
        Two small examples of hope, you can gain something.

    

Published 31 original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_36557960/article/details/79299093