这里使用的是按位加密,按ASCII码进行加密的算法自己写个,很容易的。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void
dofile(char
*in_fname,char
*pwd,char
*out_fname);/*对文件进行加密的具体函数*/
void
usage(char
*name);
void
main(int
argc,char
*argv[])/*定义main()函数的命令行参数*/
{
char
in_fname[30];/*用户输入的要加密的文件名*/
char
out_fname[30];
char
pwd[10];/*用来保存密码*/
if(argc!=4)
{/*容错处理*/
usage(argv[0]);
printf("\nIn-fname:\n");
gets(in_fname);/*得到要加密的文件名*/
while(*in_fname==NULL)
{
printf("\nIn-fname:\n");
gets(in_fname);
}
printf("Password
6-8:\n");
gets(pwd);/*得到密码*/
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
6-8:\n");
gets(pwd);
}
printf("Out-file:\n");
gets(out_fname);/*得到加密后你要的文件名*/
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
else
{/*如果命令行参数正确,便直接运行程序*/
strcpy(in_fname,argv[1]);
strcpy(pwd,argv[2]);
strcpy(out_fname,argv[3]);
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
faied!\n");
printf("Password
6-8:\n");
gets(pwd);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
}
/*加密子函数开始*/
void
dofile(char
*in_fname,char
*pwd,char
*out_file)
{
FILE
*fp1,*fp2;
register
char
ch;
int
j=0;
int
j0=strlen(pwd);
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot
open
in-file.\n");
exit(1);/*如果不能打开要加密的文件,便退出程序*/
}
fp2=fopen(out_file,"wb");
if(fp2==NULL)
{
printf("cannot
open
or
create
out-file.\n");
exit(1);/*如果不能建立加密后的文件,便退出*/
}
/*加密算法开始*/
while(j0>=0)
{
ch=fgetc(fp1);
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2);/*异或后写入fp2文件*/
ch=fgetc(fp1);
}
j0--;
}
fclose(fp1);/*关闭源文件*/
fclose(fp2);/*关闭目标文件*/
}
void
usage(char
*name)
{
printf("\t=======================File
encryption======================\n");
printf("\tusage:
%s
In-fname
password
out_fname\n",name);
printf("\tExample:
%s
file1.txt
12345678
file2.txt\n",name);
}
Ⅱ c语言编写的程序,在输入密码时,如何加密
加密和解密算法是程序编制中的重要一环。试想,如果我们平时使用的腾讯QQ、支付宝支付密码、今日头条账号密码那么轻易就被别人盗取的话,很多不可以预料的事情就会发生!
在现实生活中,我们遇到过太多QQ密码被盗取的情况,有的朋友QQ被盗之后,骗子利用朋友间信任骗取钱财的事情屡见不鲜。支付宝也曾出现过支付宝账户被恶意盗取的事件,对用户利益造成了严重损害!这些在技术上都指向了同一相关问题:软件加密算法的强壮程度。今天,小编利用C语言来简单实现一种加密方法。下面是源代码。
需要说明:程序利用了ascii码值的按照一定规律变换实现加密,对于解密过程,则是加密的逆过程。下面是程序的运行结果。
4190阅读
搜索
编程免费课程300节
初学编程100个代码
java自学一般要学多久
5秒破解excel密码
python必背100源代码
40岁零基础学编程
Ⅲ DES加密算法C语言实现
#include<iostream.h>
class SubKey{ //定义子密钥为一个类
public:
int key[8][6];
}subkey[16]; //定义子密钥对象数组
class DES{
int encipher_decipher; //判断加密还是解密
int key_in[8][8]; //用户原始输入的64位二进制数
int key_out[8][7]; //除去每行的最后一位校验位
int c0_d0[8][7]; //存储经PC-1转换后的56位数据
int c0[4][7],d0[4][7]; //分别存储c0,d0
int text[8][8]; //64位明文
int text_ip[8][8]; //经IP转换过后的明文
int A[4][8],B[4][8]; //A,B分别存储经IP转换过后明文的两部分,便于交换
int temp[8][6]; //存储经扩展置换后的48位二进制值
int temp1[8][6]; //存储和子密钥异或后的结果
int s_result[8][4]; //存储经S变换后的32位值
int text_p[8][4]; //经P置换后的32位结果
int secret_ip[8][8]; //经逆IP转换后的密文
public:
void Key_Putting();
void PC_1();
int function(int,int); //异或
void SubKey_Proction();
void IP_Convert();
void f();
void _IP_Convert();
void Out_secret();
};
void DES::Key_Putting() //得到密钥中对算法有用的56位
{
cout<<"请输入64位的密钥(8行8列且每行都得有奇数个1):\n";
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
cin>>key_in[i][j];
if(j!=7) key_out[i][j]=key_in[i][j];
}
}
void DES::PC_1() //PC-1置换函数
{
int pc_1[8][7]={ //PC-1
{57, 49, 41, 33, 25, 17, 9},
{1, 58, 50, 42, 34, 26, 18},
{10, 2, 59, 51, 43, 35, 27},
{19, 11, 3, 60, 52, 44, 36},
{63, 55, 47, 39, 31, 23, 15},
{7, 62, 54, 46, 38, 30, 22},
{14, 6, 61, 53, 45, 37, 29},
{21, 13, 5, 28, 20, 12, 4}
};
int i,j;
for(i=0;i<8;i++)
for(j=0;j<7;j++)
c0_d0[i][j]=key_out[ (pc_1[i][j]-1)/8 ][ (pc_1[i][j]-1)%8 ];
}
int DES::function(int a,int b) //模拟二进制数的异或运算,a和b为整型的0和1,返回值为整型的0或1
{
if(a!=b)return 1;
else return 0;
}
void DES::SubKey_Proction() //生成子密钥
{
int move[16][2]={ //循环左移的位数
1 , 1 , 2 , 1 ,
3 , 2 , 4 , 2 ,
5 , 2 , 6 , 2 ,
7 , 2 , 8 , 2 ,
9 , 1, 10 , 2,
11 , 2, 12 , 2,
13 , 2, 14 , 2,
15 , 2, 16 , 1
};
int pc_2[8][6]={ //PC-2
14, 17 ,11 ,24 , 1 , 5,
3 ,28 ,15 , 6 ,21 ,10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20 ,13 , 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
for(int i=0;i<16;i++) //生成子密钥
{
int j,k;
int a[2],b[2];
int bb[28],cc[28];
for(j=0;j<4;j++)
for(k=0;k<7;k++)
c0[j][k]=c0_d0[j][k];
for(j=4;j<8;j++)
for(k=0;k<7;k++)
d0[j-4][k]=c0_d0[j][k];
for(j=0;j<4;j++)
for(k=0;k<7;k++){
bb[7*j+k]=c0[j][k];
cc[7*j+k]=d0[j][k];
}
for(j=0;j<move[i][1];j++){
a[j]=bb[j];
b[j]=cc[j];
}
for(j=0;j<28-move[i][1];j++){
bb[j]=bb[j+1];
cc[j]=cc[j+1];
}
for(j=0;j<move[i][1];j++){
bb[27-j]=a[j];
cc[27-j]=b[j];
}
for(j=0;j<28;j++){
c0[j/7][j%7]=bb[j];
d0[j/7][j%7]=cc[j];
}
for(j=0;j<4;j++) //L123--L128是把c0,d0合并成c0_d0
for(k=0;k<7;k++)
c0_d0[j][k]=c0[j][k];
for(j=4;j<8;j++)
for(k=0;k<7;k++)
c0_d0[j][k]=d0[j-4][k];
for(j=0;j<8;j++) //对Ci,Di进行PC-2置换
for(k=0;k<6;k++)
subkey[i].key[j][k]=c0_d0[ (pc_2[j][k]-1)/7 ][ (pc_2[j][k]-1)%7 ];
}
}
void DES::IP_Convert()
{
int IP[8][8]={ //初始置换IP矩阵
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
cout<<"你好,你要加密还是解密?加密请按1号键(输入1),解密请按2号键,并确定."<<'\n';
cin>>encipher_decipher;
char * s;
if(encipher_decipher==1) s="明文";
else s="密文";
cout<<"请输入64位"<<s<<"(二进制):\n";
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
cin>>text[i][j];
for(i=0;i<8;i++) //进行IP变换
for(j=0;j<8;j++)
text_ip[i][j]=text[ (IP[i][j]-1)/8 ][ (IP[i][j]-1)%8 ];
}
Ⅳ c语言 数据加密
什么是异或算法
异或的特点是原始值经过两次异或某一个数后会变成原来的值,所以有时利用这个特性来进行加密,加密端把数据与一个密钥进行异或操作,生成密文。接收方收到密文后利用加密方提供的密钥进行再次异或操作就能得到明文。
例程:
/*以DWORD为单位对文件进行加密,将每个DWORD与0xfcba0000(密钥)做异或,写入另一个文件*/
#include<stdio.h>
#include<stdlib.h>
#defineDWORDunsignedlong
#defineBYTEunsignedchar
#definefalse0
#definetrue1
intmain(intargc,char*argv[])
{
FILE*hSource;
FILE*hDestination;
DWORDdwKey=0xfcba0000;
char*pbBuffer;
DWORDdwBufferLen=sizeof(DWORD);
DWORDdwCount;
DWORDdwData;
if(argv[1]==0||argv[2]==0)
{
printf("missingargument!
");
returnfalse;
}
char*szSource=argv[1];
char*szDestination=argv[2];
hSource=fopen(szSource,"rb");//打开源文件.
hDestination=fopen(szDestination,"wb");//打开目标文件
if(hSource==NULL){printf("openSourceFileerror!");returnfalse;}
if(hDestination==NULL){printf("openDestinationFileerror!");returnfalse;}
//分配缓冲区
pbBuffer=(char*)malloc(dwBufferLen);
do{
//从源文件中读出dwBlockLen个字节
dwCount=fread(pbBuffer,1,dwBufferLen,hSource);
//加密数据
dwData=*(DWORD*)pbBuffer;//char*TOdword
dwData^=dwKey;//xoroperation
pbBuffer=(char*)&dwData;
//将加密过的数据写入目标文件
fwrite(pbBuffer,1,dwCount,hDestination);
}while(!feof(hSource));
//关闭文件、释放内存
fclose(hSource);
fclose(hDestination);
printf("%sisencryptedto%s
",szSource,szDestination);
return0;
}
Ⅳ DES加密算法C语言实现
/*********************************************************************/
/*-文件名:des.h */
/*- */
/*-功能: 实现DES加密算法的加密解密功能 */
/*********************************************************************/
typedef int INT32;
typedef char INT8;
typedef unsigned char ULONG8;
typedef unsigned short ULONG16;
typedef unsigned long ULONG32;
/*如果采用c++编译器的话采用如下宏定义
#define DllExport extern "C" __declspec(dllexport)
*/
#define DllExport __declspec(dllexport)
/*加密接口函数*/
DllExport INT32 DdesN(ULONG8 *data, ULONG8 **key, ULONG32 n_key,ULONG32 readlen);
DllExport INT32 desN(ULONG8 *data, ULONG8 **key, ULONG32 n_key,ULONG32 readlen);
DllExport INT32 des3(ULONG8 *data, ULONG8 *key,ULONG32 n ,ULONG32 readlen);
DllExport INT32 Ddes3(ULONG8 *data,ULONG8 *key,ULONG32 n ,ULONG32 readlen);
DllExport INT32 des(ULONG8 *data, ULONG8 *key,INT32 readlen);
DllExport INT32 Ddes(ULONG8 *data,ULONG8 *key,INT32 readlen);
*********************************************************************/
/*-文件名:des.c */
/*- */
/*-功能: 实现DES加密算法的加密解密功能 */
//*********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include "des.h"
#define SUCCESS 0
#define FAIL -1
#define READFILESIZE 512
#define WZ_COMMEND_NUM 4
#define WZUSEHELPNUM 19
#define DESONE 1
#define DESTHREE 2
#define DESMULTI 3
INT8 *WZ_Commend_Help[] =
{
"基于DES的加密解密工具v1.0 ",/*0*/
"追求卓越,勇于创新 ",
"----着者 : 吴真--- ",
" "
};
INT8 *WZ_USE_HELP[]={
"输入5+n个参数:",
"\t1.可执行文件名 *.exe",
"\t2.操作类型 1:一层加密;2:一层解密;",
"\t\t13:N层单密钥加密;23:N层单密钥解密;",
"\t\t39:N层多密钥加密;49:N层多密钥解密",
"\t3.读出数据的文件名*.txt",
"\t4.写入数据的文件名*.txt",
"\t5.密钥(8字节例如:wuzhen12)",
"\t[6].N层单密钥的层数或者...二层加密|解密密钥",
"\t[7].三层加密|解密密钥",
"\t[8]. ...",
"\t[N].N层加密|解密密钥",
"\t 例1: des 1 1.txt 2.txt 12345678",
"\t : des 2 2.txt 3.txt 12345678",
"\t 例2: des 13 1.txt 2.txt tiantian 5",
"\t : des 23 2.txt 3.txt tiantian 5",
"\t 例3: des 39 1.txt 2.txt 12345678 tiantian gaoxinma",
"\t : des 49 2.txt 3.txt 12345678 tiantian gaoxinma",
"******************************"
};
INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length);/*以16进制写入文件*/
INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len);/*16进制解码*/
INT32 file_enc(FILE *readfile,FILE *writefile,
ULONG8 *key,ULONG32 keynum,
ULONG8 **superkey,ULONG32 n_superkey,
ULONG8 flag);
INT32 file_dec(FILE *readfile,FILE *writefile,
ULONG8 *key,ULONG32 keynum,
ULONG8 **superkey,ULONG32 n_superkey,
ULONG8 flag);
void wz_print_help();
INT32 main(INT32 argc,INT8 *argv[])
{
INT8 *FILENAME1,*FILENAME2;
FILE *fp, *fp2;
ULONG8 *key ;
ULONG8 **superkey ;/*n层加密解密密钥*/
ULONG8 n_superkey ;
ULONG32 num;
if ( argc >= 5 && (atoi(argv[1]) == 39 || atoi(argv[1]) == 49 ) )
{
n_superkey = argc - 4 ;
superkey = ( INT8 **)calloc(1, n_superkey*sizeof( void *) ) ;
for ( num = 0 ; num < n_superkey ; num++)
{
superkey[num] = argv[4+num] ;
}
}
else if ( argc == 6 && (atoi(argv[1]) == 13 || atoi(argv[1]) == 23 ) && (atoi(argv[5])) > 0)
{
}
else if ( argc == 5 && ( atoi(argv[1]) == 1 || atoi(argv[1]) == 2 ))
{
}
else
{
wz_print_help();
return FAIL;
}
FILENAME1 = argv[2];
FILENAME2 = argv[3];
if ((fp= fopen(FILENAME1,"rb")) == NULL || (fp2 = fopen(FILENAME2,"wb"))==NULL)
{
printf("Can't open file\n");
return FAIL;
}
key = argv[4] ;
switch( atoi(argv[1] ))
{
case 1: /*加密*/
file_enc(fp,fp2,key,0, NULL,0, DESONE);
printf("\n \tDES 一层加密完毕,密文存于%s文件\n",FILENAME2);
break;
case 2:
file_dec(fp,fp2,key,0, NULL, 0,DESONE);
printf("\n \tDES 一层解密完毕,密文存于%s文件\n",FILENAME2);
break;
case 13:
file_enc(fp,fp2,key,atoi(argv[5]),NULL,0,DESTHREE);
printf("\n \tDES %u层单密钥加密完毕,密文存于%s文件\n",atoi(argv[5]),FILENAME2);
break;
case 23:
file_dec(fp,fp2,key,atoi(argv[5]),NULL,0,DESTHREE);
printf("\n \tDES %u层单密钥解密完毕,密文存于%s文件\n",atoi(argv[5]),FILENAME2);
break;
case 39:
file_enc(fp,fp2,NULL,0,superkey,n_superkey,DESMULTI);
printf("\n \tDES 多密钥加密完毕,密文存于%s文件\n",FILENAME2);
free(superkey);
superkey = NULL;
break;
case 49:
file_dec(fp,fp2,NULL,0,superkey,n_superkey,DESMULTI);
printf("\n \tDES 多密钥加密完毕,密文存于%s文件\n",FILENAME2);
free(superkey);
superkey = NULL;
break;
default:
printf("请选择是加密|解密 plese choose encrypt|deencrypt\n");
break;
}
fclose(fp);
fclose(fp2);
return SUCCESS;
}
void wz_print_help()
{
INT32 i ;
printf("\t");
for ( i = 0 ; i < 22 ; i++)
{
printf("%c ",5);
}
printf("\n");
for( i = 0 ; i < WZ_COMMEND_NUM ; i++)
{
printf("\t%c\t%s %c\n",5,WZ_Commend_Help[i],5);
}
printf("\t");
for ( i = 0 ; i < 22 ; i++)
{
printf("%c ",5);
}
printf("\n");
for( i = 0 ; i < WZUSEHELPNUM ; i++)
{
printf("\t%s\n",WZ_USE_HELP[i]);
}
return ;
}
INT32 file_enc(FILE *readfile,FILE *writefile,
ULONG8 *key,ULONG32 keynum,
ULONG8 **superkey,ULONG32 n_superkey,
ULONG8 flag)
{
INT32 filelen = 0,readlen = 0,writelen = 0;
ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/
ULONG8 readbuf[READFILESIZE] = { 0 };
filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );
while( filelen == READFILESIZE )
{
totalfilelen += READFILESIZE;
switch(flag)
{
case DESONE:
des( readbuf,key,READFILESIZE);
break;
case DESTHREE:
des3( readbuf, key ,keynum,READFILESIZE);
break;
case DESMULTI:
desN( readbuf, superkey ,n_superkey,READFILESIZE);
break;
}
hextofile( readbuf, writefile, READFILESIZE );/*以16进制形式写入文件*/
memset(readbuf,0,READFILESIZE);
filelen = fread( readbuf, sizeof( INT8 ), READFILESIZE, readfile );
}
/*这是从文件中读出的最后一批数据,长度可能会等于0,所以要先判断*/
if ( filelen > 0 )
{
/*如果从文件中读出的长度不等于0,那么肯定有8个字节以上的空间
文件长度存在最后8个字节中*/
totalfilelen += filelen;
memcpy( &readbuf[READFILESIZE-8], (ULONG8*)&totalfilelen,4);
switch(flag)
{
case DESONE:
des( readbuf,key,READFILESIZE);
break;
case DESTHREE:
des3( readbuf, key ,keynum,READFILESIZE);
break;
case DESMULTI:
desN( readbuf, superkey ,n_superkey,READFILESIZE);
break;
}
hextofile( readbuf, writefile,READFILESIZE );/*以16进制形式写入文件*/
memset(readbuf,0 ,READFILESIZE);
}
else /*filelen == 0*/
{
memcpy( &readbuf[0], (ULONG8*)&totalfilelen,4);
switch(flag)
{
case DESONE:
des( readbuf,key,8);
break;
case DESTHREE:
des3( readbuf, key ,keynum,8);
break;
case DESMULTI:
desN( readbuf, superkey ,n_superkey,8);
break;
}
hextofile( readbuf, writefile, 8);/*以16进制形式写入文件*/
}
return SUCCESS;
}
INT32 file_dec(FILE *readfile,FILE *writefile,
ULONG8 *key,ULONG32 keynum,
ULONG8 **superkey,ULONG32 n_superkey,
ULONG8 flag)
{
INT32 filelen = 0,readlen = 0,writelen = 0;
ULONG32 totalfilelen = 0 ;/*统计实际的文件的长度*/
INT32 num = 0;
ULONG8 readbuf[READFILESIZE] = { 0 };
ULONG8 sendbuf[READFILESIZE*2] = { 0 };
fseek(readfile,-16,SEEK_END);/*最后16个字节的表示文件长度的空间*/
filelen = fread( sendbuf, sizeof( INT8 ), 16, readfile );
encodehex( readbuf,sendbuf,8);
switch(flag)
{
case DESONE:
Ddes( readbuf,key,8);
break;
case DESTHREE:
Ddes3( readbuf, key ,keynum,8);
break;
case DESMULTI:
DdesN( readbuf, superkey ,n_superkey,8);
break;
}
/*解密*/
memcpy((ULONG8*)&totalfilelen, &readbuf[0],4);/*得到文件总长*/
memset(readbuf,0 ,8);
memset(sendbuf,0 ,16);
num = totalfilelen/READFILESIZE;/*有几个READFILESIZE组*/
totalfilelen %= READFILESIZE;
fseek(readfile,0,SEEK_SET);/*跳到文件头*/
while(num--)
{
filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE*2, readfile );
encodehex( readbuf,sendbuf,READFILESIZE);
switch(flag)
{
case DESONE:
Ddes( readbuf,key,READFILESIZE);
break;
case DESTHREE:
Ddes3( readbuf, key ,keynum,READFILESIZE);
break;
case DESMULTI:
DdesN( readbuf, superkey ,n_superkey,READFILESIZE);
break;
}
writelen = fwrite(readbuf, sizeof( INT8 ), READFILESIZE, writefile);
memset(readbuf,0 ,READFILESIZE);
memset(sendbuf,0 ,READFILESIZE*2);
}
if ( totalfilelen > 0 )/*最后一块有多余的元素*/
{
filelen = fread( sendbuf, sizeof( INT8 ), READFILESIZE*2, readfile );
encodehex( readbuf,sendbuf,READFILESIZE);
switch(flag)
{
case DESONE:
Ddes( readbuf,key,READFILESIZE);
break;
case DESTHREE:
Ddes3( readbuf, key ,keynum,READFILESIZE);
break;
case DESMULTI:
DdesN( readbuf, superkey ,n_superkey,READFILESIZE);
break;
}
writelen = fwrite(readbuf, sizeof( INT8 ), totalfilelen, writefile);
memset(readbuf,0 ,READFILESIZE);
memset(sendbuf,0 ,READFILESIZE*2);
}
return SUCCESS;
}
INT32 hextofile( ULONG8 *buf ,FILE *writefile, ULONG32 length)
{
ULONG32 writelen = 0 ;
/*以16进制形式写入文件*/
while( writelen < length)
{
if(buf[writelen] == 0)
{
fprintf( writefile, "%x", 0 );
fprintf( writefile, "%x", 0 );
}
else if (buf[writelen] < 0x10)
{
fprintf( writefile, "%x", 0 );
fprintf( writefile, "%x", buf[writelen] );
}
else
{
fprintf( writefile, "%x", buf[writelen] );
}
writelen++;
}
return SUCCESS;
}
INT32 encodehex(ULONG8 *tobuf,ULONG8 *frombuf,ULONG32 len)
{
ULONG8 *readfirst = frombuf ;
ULONG8 *readend = &frombuf[1] ;
INT8 *s;
ULONG8 y[2] ;
ULONG32 i;
for ( i = 0 ; i < len ; i++)
{
y[0] = *readfirst ;
y[1] = *readend ;
readfirst += 2 ;
readend += 2 ;
tobuf[i] = (ULONG8)strtol((INT8*)y, &s, 16);
}
return SUCCESS;
}
Ⅵ C语言古罗马加密算法
// An highlighted block
#include <stdio.h>
int N=26;
int exgcd(int a, int n) {
int p = a, q = n;
int x = 0, y = 1;
int z = q / p;
while (p != 1 && q != 1) {
int t = p;
p = q % p;
q = t;
t = y;
y = x - y * z;
x = t;
z = q / p;
}
y %= n;
if (y < 0) y += n;
return y;
}
void main()
{ int a,b;
char ch[1000];printf("输入明文");gets (ch);
printf("输入秘钥ab\n");
scanf("%d",&a);
scanf("%d",&b);
d=exgcd(a,26);
int i=0;
char re[1000];
while (ch [i]!= '\0')
{
if ((int)ch [i]>= 65 && (int)ch[i] <= 90)//判断字符是否介于<A,Z>
{
re[i] = (char)(((a*((int)ch [i]- 65) + b) % N) + 65);
printf("%c",re[i]);
}
if ((int)ch [i]>= 97 && (int)ch [i]<= 122)//判断字符是否介于<a,z>
{
re[i] = (char)(((a*((int)ch [i]- 97) + b) % N) + 97);
printf("%c",re[i]);
}
else
i++;//若不是字母则不加处理
i++;
}
printf("\n");
i=0;
char ch1;
while (re[i]!='\0')
{
if ((int)re[i] >= 65 && (int)re [i]<= 90)
{
ch1 = (char)(((d*(((int)re [i]- 65) - b)+N) % N) + 65);
printf("%c",ch1);
}
if ((int)re [i]>= 97 && (int)re [i]<= 122)
{
ch1 = (char)(((d*(((int)re [i]- 97) - b)+N) % N) + 97);
printf("%c",ch1);
}
else
i++;
i++;
}
printf("\n");
}
维吉尼亚密码
#include <stdio.h>
#include <string.h>
void main()
{
char ch[1000],re[1000],key[1000],r;
int key1[1000],n=0,k,m;
int i=0;
printf("输入密文");
gets(ch);
printf("输入密钥");
gets(key);
while(key[i]!='\0')
{key1[i]=(int)key[i]-97;
i++;
}
int c=strlen(ch);
// printf("%d",c);printf("%d",i);
int j;
if(c%i>0)j=c/i+1;
else j=c/i;
//printf("%d",j);
for(k=0;k<j;k++)
{
for( m=0;m<i;m++)
{
if(ch[n]!='\0'){
re[n]=(((int)ch[n]-97+key1[m])%26+97)<=122 ? (char)(((int)ch[n]-97+key1[m])%26+97):(char)(((int)ch[n]-97+key1[m])%26+97-26);
printf("%c",re[n]);
n++;
}
}
}n=0;
printf("\n");
for(k=0;k<j;k++)
{
for(m=0;m<i;m++)
{
if(re[n]!='\0'){
r=(((int)re[n]-97-key1[m])%26+97)>=97?(char)(((int)re[n]-97-key1[m])%26+97):(char)(((int)re[n]-97-key1[m])%26+97+26);
printf("%c",r);
n++;
}
}
}
}
47
48
49
50
Ⅶ 用c语言设计一个简单地加密算,解密算法,并说明其中的原理
恰巧这两天刚看的一种思路,很简单的加密解密算法,我说一下吧。
算法原理很简单,假设你的原密码是A,用A与数B按位异或后得到C,C就是加密后的密码,用C再与数B按位异或后能得回A。即(A异或B)异或B=A。用C实现很简单的。
这就相当于,你用原密码A和特定数字B产生加密密码C,别人拿到这个加密的密码C,如果不知道特定的数字B,他是无法解密得到原密码A的。
对于密码是数字的情况可以用下面的代码:
#include <stdio.h>
#define BIRTHDAY 19880314
int main()
{
long a, b;
scanf("%ld", &a);
printf("原密码:%ld\n", a);
b = BIRTHDAY;
a ^= b;
printf("加密密码:%ld\n", a);
a ^= b; printf("解密密码:%ld\n", a);
return 0;
}
如果密码是字符串的话,最简单的加密算法就是对每个字符重新映射,只要加密解密双方共同遵守同一个映射规则就行啦。
Ⅷ c语言中的文件加密
首先打开VC++6.0
选择文件,新建
选择C++ source file 新建一个空白文档
声明头文件
#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;
}
Ⅸ RSA加密解密算法示例(C语言)
#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 = 2; i <= prime2; i++) // 判断是否为素数
{
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() % eular; if (e < EXPONENT_MAX) break;
}
while (1)
{
if (gcd(e, eular) == 1) return e; e = (e + 1) % eular; if (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 = 0; i < str_read_len; i++)
{
int C = 1; int a = str_read[i], b = a % mod;
for (int j = 0; j < pubKey; j++) //实现加密
{
C = (C*b) % mod;
}
str_encrypt[i] = C;
printf("%d ", str_encrypt[i]);
}
printf("\n");
}
//解密函数
void jiemi()
{
int i=0; for (i = 0; i < str_read_len; i++)
{
int C = 1; int a = str_encrypt[i], b=a%mod;
for (int j = 0; j < priKey; j++)
{
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 * prime2; printf("模数: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;
}
Ⅹ C语言 加密算法
#include<stdio.h>
#include<string.h>
#defineMAX_LEN1024
#defineMAX_KEY_LEN10
/*key必须是1-9之间的数字*/
/*拥有K个字符的Key,包含且仅包含1-K*/
intCheckKey(char*key)
{
inti,check[MAX_KEY_LEN]={0};
intmax=strlen(key);
intkeyVal;
for(i=0;i<max;i++)
{
keyVal=key[i]-'0';
if(keyVal>max||keyVal<1)
return0;
if(check[keyVal]==1)
return0;
else
check[keyVal]=1;
}
return1;
}
intEncrypt(char*word,char*key,char*secretWord)
{
inti,start;
intnLenWord=strlen(word);
intnLenKey=strlen(key);
intindex[MAX_KEY_LEN];
if(nLenWord%nLenKey!=0)
{
printf("明文的位数不是密钥位数的整数倍! ");
return0;
}
for(i=0;i<nLenKey;i++)
{
index[i]=key[i]-'0'-1;
}
/*START关键代码*/
start=0;
while(start<nLenWord)
{
for(i=0;i<nLenKey;i++)
{
secretWord[start+i]=word[start+index[i]];
}
start+=nLenKey;
}
secretWord[nLenWord]='