下面程序用异或操作对文件进行加密和解密
/******************设计思路******************/
//根据用户输入的加密/机密密码,
//每次都拿原文件和密码等长度的一个字符串和密码
//对应元素异或进行加密/解密
//另外因为是用异或方法,所以加密和解密就是同一个程序
//即按照同样的加密即是对文件的解密
#include
#include
#include
#include
#include
charfilename[256]//原文件
charpassword[256]//加密/解密密码
constcharfilenametemp[]="temp15435255435325432543.temp"//加密/解密中间文件
voidinputpass(char*pass)//密码输入以"******"显示
voidmain(){
FILE*fp//加密/解密的文件
FILE*fptemp//加密/解密过程临时文件
intpwdlen//密码长度
inti=0//计数器
charch=0//读入的字符
printf("请输入要加密/解密的文件名(全路径名):\n")
gets(filename)
if((fp=fopen(filename,"rb"))==NULL){
printf("找不到文件%s\n",filename)
exit(1)
}//if
printf("请输入要加密/解密的密码:\n")
inputpass(password)
pwdlen=strlen(password)
if(pwdlen==0){
printf("密码不能为空,加密/解密失败\n")
exit(1)
}//if
fptemp=fopen(filenametemp,"wb")//打开中间文件
while(1){
ch=fgetc(fp)//从原文件读入一个字符
if(feof(fp)){//已经读到文件尾
break//退出循环
}
ch^=password[i++]//对原字符和密码进行异或操作
fputc(ch,fptemp)//将异或结果写入中间文件
if(i==pwdlen){//使得原文件每和密码长度相同的固定长度异或加密
i=0
}
}//while
fclose(fp)//关闭打开原文件
fclose(fptemp)//关闭打开中间文件
remove(filename)//删除原文件
rename(filenametemp,filename)//将中间文件重命名为原文件
printf("加密/解密成功\n")//至此加密/解密成功
}
//密码输入以"******"显示
voidinputpass(char*pass){
inti=0
charc
while(isprint(c=getch())){
pass[i++]=c
//printf("*")
}
pass[i]='\0'
printf("\n")
}
c语言文件加密和解密方法如下:
1、首先打开VC++6.0;
2、选择文件,新建;
3、选择C++ source file 新建一个空白文档;
4、声明头文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
首先写个加密函数,算法就是简介里说的
void EncryptFile(FILE *sfp,FILE *dfp,char pwd)
{
char ch
if(sfp==0||dfp==0)
{
printf("ERROR!\n")
return
}
while((ch=fgetc(sfp))!=EOF)
{
if((ch>='a')&&(ch<='z'))
{
ch=(ch-'a'+1)%26+'a'
ch=ch^pwd
}
if((ch>='A')&&(ch<='Z'))
{
ch=(ch-'A'+1)%26+'A'
ch=ch^pwd
}
fputc(ch,dfp)
}
}
写解密子函数:与加密的过程相反
void DecryptFile(FILE *sfp,FILE *dfp,char pwd)
{
char ch
while((ch=fgetc(sfp))!=EOF)
{
if((ch>='a')&&(ch<='z'))
{
ch=ch^pwd
ch=(ch-'a'+25)%26+'a'
}
if((ch>='A')&&(ch<='Z'))
{
ch=ch^pwd
ch=(ch-'A'+25)%26+'A'
}
fputc(ch,dfp)
}
}
输出函数,输出文件内容
void OutputFile(FILE *fp)
{
char ch
while((ch=fgetc(fp))!=EOF)
putchar(ch)
}
主函数,主要调用这几个函数
int main()
{
/*用户输入的要加密的文件名*/
char sfilename[20]
/*用户输入加密后保存的文件名*/
char dfilename[20]
/*用来保存密码字符*/
char pwd
FILE *sfp,*dfp
printf("\nPlease input filename to be encrypted:\n")
/*得到要加密的文件名*/
gets(sfilename)
/*得到加密后你要的文件名*/
printf("input filename to save the encrypted file:\n")
gets(dfilename)
/*得到加密字符*/
printf("Please input your Password:\n")
//scanf("%c",&pwd)
pwd=getch()
/*屏幕以*来表示输入的加密字符*/
printf("*\n")
/*以只读方式打开要加密的文件*/
if((sfp=fopen(sfilename,"r"))==0)
{
printf("Can't open the file :%s\n",sfilename)
exit(0)
}
/*输出要加密的文件*/
printf("\nThe the text of file to be encrypted is:\n")
OutputFile(sfp)
/*建立加密后的文件*/
if((dfp=fopen(dfilename,"w+"))==0)
{
printf("Can't open or create the file :%s\n",dfilename)
//exit(0)
}
/*文件加密*/
fseek(sfp,0L,SEEK_SET)
EncryptFile(sfp,dfp,pwd)
printf("\n\nEncrypted the file successfully!\n")
/*输出加密后的文件*/
printf("\nAfter encrypting the text of file is:\n")
fseek(dfp,0L,SEEK_SET)
OutputFile(dfp)
fclose(sfp)
fclose(dfp)
getch()
return 0
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define PRIME_MAX 200 // 生成素数范围
#define EXPONENT_MAX 200 // 生成指数e范围
#define Element_Max 127 // 加密单元的最大值,这里为一个char, 即1Byte
char str_read[100]="hello world !" // 待加密的原文
int str_encrypt[100] // 加密后的内容
char str_decrypt[100] // 解密出来的内容
int str_read_len // str_read 的长度
int prime1, prime2 // 随机生成的两个质数
int mod, eular // 模数和欧拉数
int pubKey, priKey // 公钥指数和私钥指数
// 生成随机素数,实际应用中,这两个质数越大,就越难破解。
int randPrime()
{
int prime, prime2, i
next:
prime = rand() % PRIME_MAX // 随机产生数
if (prime <= 1) goto next // 不是质数,生成下一个随机数
if (prime == 2 || prime == 3) return prime
prime2 = prime / 2 // prime>=4, prime2 的平方必定大于 prime , 因此只检查小于等于prime2的数
for (i = 2i <= prime2i++) // 判断是否为素数
{
if (i * i >prime) return prime
if (prime % i == 0) goto next // 不是质数,生成下一个随机数
}
}
// 欧几里德算法,判断a,b互质
int gcd(int a, int b)
{
int temp
while (b != 0) {
temp = b
b = a % b
a = temp
}
return a
}
//生成公钥指数,条件是 1<e <欧拉数,且与欧拉数互质。
int randExponent()
{
int e
while (1)
{
e = rand() % eularif (e <EXPONENT_MAX) break
}
while (1)
{
if (gcd(e, eular) == 1) return ee = (e + 1) % eularif (e == 0 || e >EXPONENT_MAX) e = 2
}
}
//生成私钥指数
int inverse()
{
int d, x
while (1)
{
d = rand() % eular
x = pubKey * d % eular
if (x == 1)
{
return d
}
}
}
//加密函数
void jiami()
{
str_read_len = strlen(str_read) //从参数表示的地址往后找,找到第一个'\0',即串尾。计算'\0'至首地址的“距离”,即隔了几个字符,从而得出长度。
printf("密文是:")
for (int i = 0i <str_read_leni++)
{
int C = 1int a = str_read[i], b = a % mod
for (int j = 0j <pubKeyj++) //实现加密
{
C = (C*b) % mod
}
str_encrypt[i] = C
printf("%d ", str_encrypt[i])
}
printf("\n")
}
//解密函数
void jiemi()
{
int i=0 for (i = 0i <str_read_leni++)
{
int C = 1int a = str_encrypt[i], b=a%mod
for (int j = 0j <priKeyj++)
{
C = (C * b) % mod
}
str_decrypt[i] = C
}
str_decrypt[i] = '\0'printf("解密文是:%s \n", str_decrypt)
}
int main()
{
srand(time(NULL))
while (1)
{
prime1 = randPrime()prime2 = randPrime()printf("随机产生两个素数:prime1 = %d , prime2 = %d ", prime1, prime2)
mod = prime1 * prime2printf("模数:mod = prime1 * prime2 = %d \n", mod)if (mod >Element_Max) break// 模数要大于每个加密单元的值
}
eular = (prime1 - 1) * (prime2 - 1) printf("欧拉数:eular=(prime1-1)*(prime2-1) = %d \n", eular)
pubKey = randExponent()printf("公钥指数:pubKey = %d\n", pubKey)
priKey = inverse()printf("私钥指数:priKey = %d\n私钥为 (%d, %d)\n", priKey, priKey, mod)
jiami()jiemi()
return 0
}