简单的异或加密

利用一个简单的事实:

x ^ y ^ z 得到 a

那么 a ^ z ^ y 就可以得到 x,或者 a ^ y ^ z 也可以得到 x

加密:提供一个key.txt和input.txt,key.txt装你的密钥,比如:abc123,input.txt装原文(可以包含中文)

解密:key.txt内容仍然是密钥,或者key.txt的内容修改为321cba,input.txt装密文

最高支持密钥长度64位

利用了“输入输出重定向”

这个的缺点在于很可能你加密的密钥是abc123,你输入的密钥是aBc123、ABC123甚至vcx123也能解密,也就是说这货并不具备实用性。对于更多的内容请参考加密解密相关的书籍

main.c

#include <stdio.h>

#include "20/xor.h"

int main(int argc, char *argv[]) {
    _xor();
    return 0;
}

xor.h

#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
posted @ 2014-10-29 12:56  rldts  阅读(1851)  评论(0编辑  收藏  举报