代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C/C++
】
按位异或实现加密解密
作者:
东昌
/ 发布于
2016/6/27
/
818
本例实现最简单的加密解密方式,即使用按位异或。
#pragma once #pragma once //Program heads: #pragma once #include<stdio.h> #include<stdlib.h> #include<vector> #include<time.h> #include "Decryption.h" using namespace std; //Variable definitions: FILE *originalText, *toyoud; //To you is the filePtr points to where encrypted information stores. int write_original_info(char bit) { //Needs modification: To toyou after the debug finishes! if ((originalText = fopen("Plain Text.txt", "a")) != NULL) { fprintf(originalText, "%c", (char)bit); fclose(originalText); return 1; } else { printf("Error 03!\n"); return 0; } }//end function: Write the encypted information void dealBits(char key, char bit) { bit = bit ^ key; write_original_info(bit); }//end function: Do mask operation on chars void decrypt() { if ((originalText = fopen("Plain Text.txt", "w")) != NULL) fclose(originalText); if ((toyoud = fopen("toyou", "r")) != NULL) { char key, bit; fscanf(toyoud, "%c%c", &key, &bit); while ((fscanf(toyoud, "%c%c", &key, &bit)) != EOF) dealBits(key, bit); fclose(toyoud); } else { printf("Error 01: Can't open original plaintext file!\n"); } }//end function: Read original message, store information into char vector. #pragma once //Program heads: #pragma once #include<stdio.h> #include<stdlib.h> #include<vector> #include<time.h> #include "Encryption.h" using namespace std; //Variable definitions: FILE *message, *toyou; //To you is the filePtr points to where encrypted information stores. int write_encyption_info(int key, int bit) { //Needs modification: To toyou after the debug finishes! if ((toyou = fopen("toyou", "a")) != NULL) { //printf("%c %c\n", key, bit); fprintf(toyou, "%c%c", key, bit); fclose(toyou); return 1; } else { printf("Error 02: Write to encryption file fails!\n"); return 0; } }//end function: Write the encypted information void dealChar(char nextChar) { int random = (int)((rand() * 1.0 / RAND_MAX) * 94 + 32); //This is wrong code: //int random = (int)((rand() * 1.0 / RAND_MAX) * 256); //int digit = (((int)nextChar) ^ random); //digit = (((int)nextChar) ^ random); int digit = 0; while (digit < 32) { random = (int)((rand() * 1.0 / RAND_MAX) * 94 + 32); digit = (((int)nextChar) ^ random); }//end loop: Prevent special characters. //printf("%d %d\n", random, digit); write_encyption_info(random, digit); }//end function: Do mask operation on chars int encrypt() { srand(time(NULL)); if ((toyou = fopen("toyou", "w")) != NULL) { int random = (int)((rand() * 1.0 / RAND_MAX) * 126); int digit = (int)((rand() * 1.0 / RAND_MAX) * 126); fprintf(toyou, "%c%c", (char)random, (char)digit); fclose(toyou); }//end if. if ((message = fopen("message.txt", "r")) != NULL) { char nextChar; while ((nextChar = fgetc(message)) != EOF) { dealChar(nextChar); printf("%c", nextChar); } fclose(message); return 1; } else { printf("Error 01: Can't open original plaintext file!\n"); return 0; } }//end function: Read original message, store information into char vector. #include "Encryption.h" #include "Decryption.h" int main() { encrypt(); system("pause"); decrypt(); system("pause"); return 0; }
试试其它关键字
同语言下
.
C分鱼问题
.
链表
.
最大连续和
.
编码字符串
.
libiconv字符编码处理及判断字符串是否为utf8
.
一组数中两两二元组,差最大有几对,差最小呢?(数组
.
通过管道获取一个进程的执行状态
.
多关键字排序
.
字符串字典序排序
.
3元一次方程(牛顿迭代法求方程的根)
可能有用的
.
C分鱼问题
.
链表
.
最大连续和
.
编码字符串
.
libiconv字符编码处理及判断字符串是否为utf8
.
一组数中两两二元组,差最大有几对,差最小呢?(数组
.
通过管道获取一个进程的执行状态
.
多关键字排序
.
字符串字典序排序
.
3元一次方程(牛顿迭代法求方程的根)
东昌
贡献的其它代码
(
28
)
.
pandas读取指定列
.
根据经纬度计算距离排序
.
获取注入对象的方法
.
添加一个字段
.
sqoop command –help
.
去除C语言注释
.
从Request.Url获取根网址的最简单方法
.
删除数据库关联记录(外键)
.
递归生成树
.
按位异或实现加密解密
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3