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

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

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

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

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

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

RC4加解密算法运行描述:

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

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

/*
 * author: surqing
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#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;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算法 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值