簡單的異或加密

利用一個簡單的事實: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
相關文章
相關標籤/搜索