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