加密文本 有人可以告诉我如何做到这一点

encrypt text Could someone please show me how to do this?

本文关键字:何做 这一点 告诉我 加密文本      更新时间:2023-10-16

我编写了一个程序,用于读取和打印用户选择的文本文件。我现在希望程序加密文件中的文本,并且用户能够选择字母移位。有人可以告诉我如何做到这一点吗?这是我到目前为止所拥有的:

#include <stdio.h>
int main(void) {
    FILE *file_in;
    char filename[20];
    char ch;
    int shift;
    // file_in is the name given to the stream. filename will be the file that the user chooses. ch is the characters in the file.

    printf("What file do you want to open?: ");
    gets(filename);
    file_in=fopen(filename, "r");
    //ask the user to enter the name of a file
    if(file_in==NULL)
        printf("Error! File did not open.n");
    //print message if the file can not be found
    while((ch=fgetc(file_in)) != EOF) 
        putchar(ch);
        //prints the results on the screen and shows the end of the file
    fclose(file_in);
    //closes stream
}`enter code here`

对于简单的字母移位,将移位存储为char,并像从用户输入中读取文件名一样读取它。将putchar(ch);替换为用于加密的putchar(ch+shift);,将ch-shift替换为解密或 vv。 如果总和大于char的最大值,ch+shift将静默溢出,从而保证每个字母ch只有一个"加密"的对应者。

相关文章: