C语言-RC4和移位 加密 解密系统

代码运行的编译器为CLion或者VS-Code,CMake最低版本要求3.17

代码实现的是一个简易的加密和解密系统,里面包含了移位密码和RC4两种算法。了解【RC4】

移位加解密算法运行描述:

加密:输入一串由小写字母组成的字符串作为明文,输入一个整数作为密码,结果产生一个由大写字母组成的密文

解密:输入待解密的密文,输入一个整数作为密码,结果产生一个由小写字母组成的解密后的明文

RC4加解密算法运行描述:

加密:输入一个整数N,代表你要输入的密钥长度,回车后在输入N个整数,中间用空格隔开,回车后输入待加密的明文,可输入出回车键(\n)之外的任何字符,结果产生的密文会保存在全局变量rc4_cipher中,解密时不需要输入密文。

解密:输入一个整数N,代表代表你要输入的密钥长度,回车后再输入N个整数,中间用空格隔开,结果产生明文

/*
 * author: surqing
*/
#include 
#include 
#include 

#define MSG_MAX_LENGTH 1001 // 输入明文的最大长度
#define TABLE_SIZE 256 // RC4加密的S表和K表的大小

unsigned char S[TABLE_SIZE];
unsigned char K[TABLE_SIZE];
unsigned char * rc4_cipher = NULL;

void shift();
void shift_encryption();
void shift_decryption();
char* shift_encrypt(const char* msg, const int key);
char* shift_decrypt(const char* cipher, const int key);

void RC4();
void RC4_encryption();
void RC4_decryption();

void init_S();
void init_K();
int* input_key(int size);
void swap(unsigned char * table, int i, int j);
void permute_S();
unsigned char* RC4_encrypt(unsigned char * msg);


void RC4() {
    int opt = -1;

    while (opt) {
        printf("\n=========================\n"
               "*** RC4 CIPHER SYSTEM ***\n"
               "=========================\n");
        printf("Please select operation:\n"
               "1.Encryption.\n"
               "2.Decryption.\n"
               "0.Exit.\n");

        scanf("%d", &opt);
        if (opt == 0) {
            break;
        }
        else if (opt == 1) {
            RC4_encryption();
        }
        else if (opt == 2) {
            RC4_decryption();
        }
        else {
            printf("Input error, enter again please!\n");
        }
    }

    return;
}

void RC4_encryption() {
    unsigned char msg[MSG_MAX_LENGTH];
    int key_size;
    int* keys = NULL;
//    unsigned char *cipher = NULL;

    printf("Please input length of the key:\n");
    scanf("%d", &key_size);
    printf("Please enter an integer key separated by Spaces:\n");
    keys = input_key(key_size);

    init_S();
    init_K(keys, key_size);
    permute_S();

    printf("Please enter the plaintext to be encrypted:\n");
//    scanf("%s", msg);
    getchar();
    gets(msg);

//    cipher = RC4_encrypt(msg);
    if (rc4_cipher != NULL) free(rc4_cipher);
    rc4_cipher = RC4_encrypt(msg);
    printf("Your cipher is:\n");
    printf("%s\n", rc4_cipher);
    printf("==========  encryption over!  ==========\n\n");

//    free(cipher);

    return;
}

void RC4_decryption() {
    if (rc4_cipher == NULL) {
        printf("rc4_cipher is null, perform encryption first!!!\n");
        return;
    }
    unsigned char cipher[MSG_MAX_LENGTH];
    int key_size;
    int* keys = NULL;
    unsigned char *msg = NULL;

    printf("Please input length of the key:\n");
    scanf("%d", &key_size);
    printf("Please enter an integer key separated by Spaces:\n");
    keys = input_key(key_size);

    init_S();
    init_K(keys, key_size);
    permute_S();

//    printf("Please enter the cipher to be decrypted:\n");
//    scanf("%s", cipher);

    msg = RC4_encrypt(rc4_cipher);// RC4是对称加密,通过对密文加密就能得到明文
    printf("Your message is:\n%s\n==========  Decryption over!  ==========\n", msg);

    free(keys);
    free(msg);

    return;
}



void shift() {
    int opt = -1;

    while (opt) {
        printf("===========================\n"
               "*** SHIFT CIPHER SYSTEM ***\n"
               "===========================\n");
        printf("Please select operation:\n"
               "1.Encryption.\n"
               "2.Decryption.\n"
               "0.Exit.\n");

        scanf("%d", &opt);
        if (opt == 0) {
            break;
        }
        else if (opt == 1) {
            shift_encryption();
        }
        else if (opt == 2) {
            shift_decryption();
        }
        else {
            printf("Input error, enter again please!\n");
        }
    }

    return;
}

void shift_encryption() {
    char msg[MSG_MAX_LENGTH];
    char *cipher;
    int key;

    printf("Please enter a string to encrypt:\n");
    scanf("%s", msg);

    printf("Please enter an integer as key:\n");
    scanf("%d", &key);

    cipher = shift_encrypt(msg, key);

    printf("Your cipher is:\n");
    printf("%s\n", cipher);
    printf("==========  encryption over!  ==========\n\n");

    free(cipher);

    return;
}

void shift_decryption() {
    char cipher[MSG_MAX_LENGTH];
    char* msg = NULL;
    int key;

    printf("Please input cipher to decrypt:\n");
    scanf("%s", cipher);
    printf("Please input integer as key:\n");
    scanf("%d", &key);
    msg = shift_decrypt(cipher, key);
    printf("Message is:\n");
    printf("%s\n==========  Decryption over!  ==========\n\n", msg);

    free(msg);

    return;
}

char* shift_encrypt(const char* msg, const int key) {
    int msgLen = strlen(msg);
    char* cipher = NULL;
    int i;

    cipher = (char*) calloc(msgLen + 1, sizeof(char));

    for (i = 0; i < msgLen; i++) {
        cipher[i] = (msg[i] - 'a' + key % 26) % 26 + 'A';
    }

    return cipher;
}

char* shift_decrypt(const char* cipher, int key) {
    int cipLen = strlen(cipher);
    char* msg = NULL;
    int i;

    msg = (char*) calloc(cipLen + 1, sizeof(char));

    for (i = 0; i < cipLen; i++) {
        msg[i] = (cipher[i] - 'A' + 26 - key % 26) % 26 + 'a';
    }

    return msg;
}


void init_S() {
    int i;

    for (i = 0; i < TABLE_SIZE; i++) {
        S[i] = (unsigned char)i;
    }

    return;
}

void init_K(int* seed_key, int seed_key_size) {
    int i;

    for (i = 0; i < TABLE_SIZE; i++) {
        K[i] = (unsigned char)seed_key[i % seed_key_size];
    }

    return;
}

int* input_key(int size) {
    int* key_arr = NULL;
    int i = 0;

    key_arr = (int*)calloc(size, sizeof(int));
    if (key_arr == NULL) {
        perror("calloc failed in function of input_key!");
        exit(1);
    }

    while (i < size) {
        scanf("%d", key_arr + i);
        key_arr[i] = (abs(key_arr[i])) % TABLE_SIZE;
        i++;
    }

    return key_arr;
}

void swap(unsigned char* table, int i, int j) {
    unsigned char t = table[i];
    table[i] = table[j];
    table[j] = t;

    return;
}

void permute_S() {
    int i = 0;
    int j = 0;

    for (i = 0; i < TABLE_SIZE; i++) {
        j = (j + S[i] + K[i]) % TABLE_SIZE;

        swap(S, i, j);
    }

    return;
}

unsigned char* RC4_encrypt(unsigned char* msg) {
    int i = 0;
    int m = 0;
    int n = 0;
    unsigned char* cipher = NULL;
    int msg_len = strlen(msg);

    cipher = (unsigned char*)calloc(msg_len + 1, sizeof(unsigned char));
    if (cipher == NULL) {
        perror("calloc failed in function of encrypt!");
        exit(1);
    }

    for (i = 0; i < msg_len; i++) {
        m = (m + 1) % TABLE_SIZE;
        n = (n + S[m]) % TABLE_SIZE;

        swap(S, m, n);

        unsigned char t = (S[m] + S[n]) % TABLE_SIZE;
        unsigned char k = S[t];

        cipher[i] = k ^ msg[i];
    }

    return cipher;
}




int main() {

    int opt = -1;

    while (opt != 0) {
        printf("================================\n"
               "**********   SYSTEM   **********\n"
               "================================\n");
        printf("Please select operation:\n"
               "1. Shift encryption/decryption.\n"
               "2. RC4 encryption/decryption.\n"
               "0. Exit system.\n");
        scanf("%d",&opt);

        if (opt == 0) {
            break;
        }
        else if (opt == 1) {
            shift();
        }
        else if (opt == 2) {
            RC4();
        }
        else {
            printf("Input error, please enter again:\n");
        }
    }

    free(rc4_cipher);

    return 0;
}

你可能感兴趣的:(C语言,信息安全,c语言)