From 6a837e0e7e309a7706bd8a18c09e36edfa4960c9 Mon Sep 17 00:00:00 2001 From: hairrrrr <781728963@qq.com> Date: Wed, 22 Apr 2020 08:51:10 +0800 Subject: [PATCH] 4-22 --- code/practise/20 底层程序设计/msg.txt | 4 ++ code/practise/20 底层程序设计/newmsg.txt | 4 ++ code/practise/20 底层程序设计/readme.md | 51 ++++++++++++++++++++++++ code/practise/20 底层程序设计/xor.c | 21 ++++++++++ 4 files changed, 80 insertions(+) create mode 100644 code/practise/20 底层程序设计/msg.txt create mode 100644 code/practise/20 底层程序设计/newmsg.txt create mode 100644 code/practise/20 底层程序设计/readme.md create mode 100644 code/practise/20 底层程序设计/xor.c diff --git a/code/practise/20 底层程序设计/msg.txt b/code/practise/20 底层程序设计/msg.txt new file mode 100644 index 0000000..6e9779d --- /dev/null +++ b/code/practise/20 底层程序设计/msg.txt @@ -0,0 +1,4 @@ +If two people write exactly the same program, each should be put in +micro-code and then they certainly won't be the same. + -- epigrams-on-programming + Time:4/21/2020 \ No newline at end of file diff --git a/code/practise/20 底层程序设计/newmsg.txt b/code/practise/20 底层程序设计/newmsg.txt new file mode 100644 index 0000000..b199006 --- /dev/null +++ b/code/practise/20 底层程序设计/newmsg.txt @@ -0,0 +1,4 @@ +o@ RQI VCIVJC QTORC C^GERJ_ RNC UGKC VTIATGK, CGEN UNISJB DC VSR OH +KOETI-EIBC GHB RNCH RNC_ ECTRGOHJ_ QIH'R DC RNC UGKC. + -- CVOATGKU-IH-VTIATGKKOHA + rOKC:4/21/2020 \ No newline at end of file diff --git a/code/practise/20 底层程序设计/readme.md b/code/practise/20 底层程序设计/readme.md new file mode 100644 index 0000000..42ccc65 --- /dev/null +++ b/code/practise/20 底层程序设计/readme.md @@ -0,0 +1,51 @@ +#### 程序:XOR 加密 + +对数据加密的一种最简单的方法就是,将每个字符与一个密钥进行异或(XOR)运算。假设密钥时一个 & 字符。如果将它与字符 z 异或,我们会得到 \ 字符(假定字符集位 ACSII 字符集)。具体计算如下: + +```c + 00100110 (& 的 ASCII 码) +XOR 01111010 (z 的 ASCII 码) + 01011100 (\ 的 ASCII 码) +``` + +要将消息解密,只需要采用相同的算法。例如,如果将 & 与 \ 异或就可以得到 &: + +```c + 00100110 (& 的 ASCII 码) +XOR 01011100 (\ 的 ASCII 码) + 01111010 (z 的 ASCII 码) +``` + +下面的程序 xor.c 通过每个字符于 & 字符进行异或来加密消息。原始消息可以由用户输入也可以输入重定向从文件读入。加密后的消息可以在屏幕上显示也可以通过输出重定向存入到文件中。例如 msg 文件包含以下内容: + +``` +If two people write exactly the same program, each should be put in +micro-code and then they certainly won't be the same. + -- epigrams-on-programming + Time:4/21/2020 +``` + +为了对文件 msg 加密并将加密后的消息存入文件 newmsg 中,输入以下命令: + +```c +xor newmsg +``` + +文件 newmsg 将包含下面的内容: + +``` +o@ RQI VCIVJC QTORC C^GERJ_ RNC UGKC VTIATGK, CGEN UNISJB DC VSR OH +KOETI-EIBC GHB RNCH RNC_ ECTRGOHJ_ QIH'R DC RNC UGKC. + -- CVOATGKU-IH-VTIATGKKOHA + rOKC:4/21/2020 +``` + +要恢复原始消息,需要命令: + +```c +xor +#include + +#define KEY '&' + +int main(void) { + + int orig_ch, new_ch; + + while ((orig_ch = getchar()) != EOF) { + new_ch = orig_ch ^ KEY; + if (isprint(orig_ch) && isprint(new_ch)) + putchar(new_ch); + else + putchar(orig_ch); + } + + return 0; +}