使用异或加密数据

(1)使用异或可简单加密数据,解密时使用加密后的数据与密钥重做一次异或运算即可。

(2)由于某些操作系统不能正确处理非打印字符,因此,对于此类字符可采取原有不做加密。


#include <stdio.h>
#include <ctype.h>

#define KEY 'z'

int main(void){
	char orig_char, new_char;

	orig_char=getchar();
	while(orig_char!=EOF){
		new_char=orig_char^KEY;
		if(isprint(orig_char)&&isprint(new_char)){
			putchar(new_char);
		}else{
			putchar(orig_char);
		}
		orig_char=getchar();
	}
	return 0;
}

运行程序时:

加密:xor <orig_file >new_file

解密:xor <new_file >orig_file

posted @ 2013-02-09 22:24  lujinhong  阅读(290)  评论(0编辑  收藏  举报