利用一个简单的事实:ui
x ^ y ^ z 获得 athis
那么 a ^ z ^ y 就能够获得 x,或者 a ^ y ^ z 也能够获得 x加密
加密:提供一个key.txt和input.txt,key.txt装你的密钥,好比:abc123,input.txt装原文(能够包含中文)spa
解密:key.txt内容仍然是密钥,或者key.txt的内容修改成321cba,input.txt装密文code
最高支持密钥长度64位blog
利用了“输入输出重定向”ip
这个的缺点在于极可能你加密的密钥是abc123,你输入的密钥是aBc12三、ABC123甚至vcx123也能解密,也就是说这货并不具有实用性。对于更多的内容请参考加密解密相关的书籍get
main.cinput
#include <stdio.h> #include "20/xor.h" int main(int argc, char *argv[]) { _xor(); return 0; }
xor.hit
#ifndef XOR_H #define XOR_H /** Using a series of characters to xor encrypt your text. key.txt is required by this program to supply key word. for example, if you use 'abc123' as the key word to encrypt your text, you'll need to use '321cba' to decrypt the text. You'll need to use input-redirect and output-redirect to make this work for your input file and output file. Maximum supports 64-character key. */ void _xor(); #endif // XOR_H
xor.c
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "xor.h" #define MAX_KEY_LEN 64 void _xor() { char key[MAX_KEY_LEN]; FILE *fp; fp = fopen("key.txt", "r"); if (fp == NULL) { printf("CANNOT OPEN key.txt\n"); return; } // read key word char ch; while(isspace(ch = getc(fp))) // skip leading white-characters ; int j = 0; for (int i = 0; i < MAX_KEY_LEN && ch!=EOF; i++) { if (ch!='\n' && ch!='\r') { key[j++] = ch; } ch = getc(fp); } // encrypt/decrypt int orig_char, new_char; while ((orig_char = getchar())!=EOF) { for (int i = 0; i < j; i++) { new_char = orig_char ^ key[i]; } putchar(new_char); } }
run.bat
xor.exe <input.txt >out.txt