📄 混沌密码加密bmp图像.cpp
字号:
// 混沌密码加密BMP图像.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "math.h"
#include<iostream>
using namespace std;
double A;
double Xb;
double Xn;
//生成混沌序列x[8]
void product_x(double *x,double x0)
{
int i;
x[0]=A*sin(x0-Xb)*sin(x0-Xb);
for(i=0;i<7;i++)
x[i+1]=A*sin(x[i]-Xb)*sin(x[i]-Xb);
}
//生成8bit的K值
void product_k(unsigned char *K,double *x)
{
int i;
for(i=0;i<8;i++)
{
if(x[i]>=(2*A/3))
(*K)=((*K)<<1)+1;
else (*K)=(*K)<<1;
}
}
//加密-解密
void encryption(FILE *bmp1,FILE *bmp2)
{
unsigned char temp,tmp;
double X[8],x0;
unsigned char K;
int i;
for(i=0;i<118;i++)
{
tmp=fgetc(bmp1);
fputc(tmp,bmp2);
}
X[7]=Xn;
while(!feof(bmp1))
{
temp=fgetc(bmp1);
x0=X[7];
product_x(X,x0);
product_k(&K,X);
temp=temp^K;
fputc(temp,bmp2);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE *bmp,*file;
char bmp_name[30],filename[30];
char Q;
Xn=3.1415926;
str:cout<<"**********混沌密码**********"<<endl;
cout<<endl<<"加密(E)、解密(D)or退出(O):";
cin>>Q;
if(Q=='E'||Q=='e')
{
str1: cout<<"*****使用混沌密码加密BMP图像*****"<<endl<<endl;
cout<<"输入BMP文件名:";
cin>>filename;
cout<<endl<<"输入加密后输出的BMP文件名:";
cin>>bmp_name;
bmp=fopen(bmp_name,"wb");
file=fopen(filename,"rb");
if(!file||!bmp)
{
cout<<endl<<"打开.bmp文件失败!"<<endl;
goto str1;
}
cout<<endl<<"请输入A值:";
cin>>A;
cout<<endl<<"请输入Xb值:";
cin>>Xb;
cout<<endl<<endl<<"开始加密!"<<endl<<endl<<"bmp图像加密中……"<<endl;
encryption(file,bmp);
cout<<endl<<endl<<"图像加密完毕!"<<endl;
fclose(bmp);
fclose(file);
goto str;
}
else if(Q=='D'||Q=='d')
{
str2: cout<<"*****使用混沌密码解密BMP图像*****"<<endl<<endl;
cout<<"输入BMP文件名:";
cin>>filename;
cout<<endl<<"输入解密密后输出的BMP文件名:";
cin>>bmp_name;
bmp=fopen(bmp_name,"wb");
file=fopen(filename,"rb");
if(!file||!bmp)
{
cout<<endl<<"打开.bmp文件失败!"<<endl;
goto str2;
}
cout<<endl<<"请输入A值:";
cin>>A;
cout<<endl<<"请输入Xb值:";
cin>>Xb;
cout<<endl<<endl<<"开始解密!"<<endl<<endl<<"bmp图像解密中……"<<endl;
encryption(file,bmp);
cout<<endl<<endl<<"图像解密完毕!"<<endl;
fclose(bmp);
fclose(file);
goto str;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -