m0_66051118 2022-01-18 23:26 采纳率: 76.3%
浏览 78
已结题

关于C语言文件的加密解密问题

选用一篇英语文章,用其每个字母的行数与列数组成的序列作为该字母的密码。用这个密码对任意给定的一段英文文本进行加密,并且可对任意给定的一段密码进行解密。如第一行第一个单词是big,则这个单词的密文为(1,1),(1,2),(1,3),
以此类推将一篇英语文章的全部英语单词加密与解密。

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-01-19 00:11
    关注

    源文件main.c

    #include <stdio.h>
    
    char encrypt(char ch, int line, int column)
    {
        return ch ^ line ^ column;
    }
    
    int main()
    {
        int line = 1;
        int column = 1;
        char ch;
        while ((ch = getchar()) != EOF)
        {
            if (ch == '\n')
            {
                putchar(ch);
                line++;
                column = 1;
            }
            else
            {
                putchar(encrypt(ch, line, column));
                column++;
            }
        }
        return 0;
    }
    

    测试文件test.txt

    Hello, world!
    This is a test file.
    

    编译

    $ gcc -Wall main.c
    

    加密

    $ cat test.txt | ./a.out > test.out 
    

    加密文件内容

    $ hexdump -C test.out
    00000000  48 66 6e 69 6b 2b 26 7e  67 79 66 69 2d 0a 57 68  |Hfnik+&~gyfi-.Wh|
    00000010  68 75 27 6d 76 2a 6a 28  7d 6b 7c 78 2d 74 7a 7c  |hu'mv*j(}k|x-tz||
    00000020  74 38 0a                                          |t8.|
    00000023
    

    解密

    $ cat test.out | ./a.out
    Hello, world!
    This is a test file.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月18日
  • 已采纳回答 2月17日
  • 创建了问题 1月18日