两个关于C语言加密解密程序

1.信息加密。编写加密程序,对用户从键盘输入的信息(不少于20个单词)以学号尾号为秘钥进行加密(例:学号尾号为3,输入信息I am z……,则密文为L dp c……,尾号为0则秘钥为10),将密文存入文本文件。
2.解密。编写程序,将已加密的文件读出并解密后存入另一文件。

便历整个字符串把每个字符的Ascll值加上学生号对10取余得到的余数。
解密则用Ascll减去余数。

//#pragma GCC optimize(3, "Ofast", "inline")
//#pragma GCC optimize("unroll-loops")
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
int main(void) {
//    freopen("unlocked.in", "r", stdin);
//    freopen("locked.out", "w", stdout);
    int Number;
    scanf("%d", &Number);
    int key = Number % 10;
    if (key == 0)
        key = 10;
    char str[300];
    //这个地方要用c语言的标准读入一整串包含空格的字符串
    int len = strlen(str);
    int i;
    for(i = 0; i < len; i++)
        str[i] = (char)((int)(str[i] + key));
    printf("%s", str);
//      fclose();
    return 0;
}

供参考:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void encryption(char* text,int key)//加密
{
    char* ptr = NULL;
    for (ptr = text; *ptr; ptr++) {
        if (isalpha(*ptr))
            isupper(*ptr) ? *ptr = (*ptr - 'A' + key) % 26 + 'A' : 
                             *ptr = (*ptr - 'a' + key) % 26 + 'a'; 
    }
    *ptr = '\0';
}
void decrypt(char* text, int key)//解密
{
    char* ptr = NULL;
    for (ptr = text; *ptr; ptr++) {
        if (isalpha(*ptr))
            isupper(*ptr) ? *ptr = (*ptr - 'A' - key + 26) % 26 + 'A' : 
                             *ptr = (*ptr - 'a' - key + 26) % 26 + 'a'; 
    }
    *ptr = '\0';
}
void write_file(char* filename, char* text)
{
    FILE* fp;
    fp = fopen(filename, "w");
    if (fp == NULL) {
        printf("无法打开文件");
        exit(0);
    }
    fprintf(fp, "%s", text);
    fclose(fp);
}
int main() 
{
    int  m, n;
    char text[1024] = { 0 }, ptext[1024] = { 0 };
    printf("输入要加密的明文:");
    gets_s(text, 1024);
    printf("输入学号:");
    scanf("%d", &m);
    m % 10 == 0 ? n = 10 : n = m % 10;

    encryption(text, n);
    printf("加密后的明文为:%s\n", text); 
    write_file("data.txt", text);

    strcpy(ptext, text);
    decrypt(ptext, n);
    printf("解密后的明文为:%s\n", ptext);
    write_file("data1.txt", ptext);

    return 0;
}

img