導航:首頁 > 文檔加密 > c語言加密演算法密鑰

c語言加密演算法密鑰

發布時間:2022-10-10 02:30:51

Ⅰ c語言加密解密演算法

這里使用的是按位加密,按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語言 數據加密

  1. 什麼是異或演算法

  2. 異或的特點是原始值經過兩次異或某一個數後會變成原來的值,所以有時利用這個特性來進行加密,加密端把數據與一個密鑰進行異或操作,生成密文。接收方收到密文後利用加密方提供的密鑰進行再次異或操作就能得到明文。

常式:

/*以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]='';

/*END關鍵代碼*/

return1;

}

intmain()

{

charword[MAX_LEN];

charkey[MAX_KEY_LEN];

charsecretWord[MAX_LEN];

printf("請輸入明文:");

scanf("%1024s",word);

printf("請輸入密鑰:");

scanf("%10s",key);

if(!CheckKey(key))

{

printf("密鑰輸入錯誤! ");

exit(-1);

}

if(Encrypt(word,key,secretWord))

printf("密文是:%s ",secretWord);

return0;

}

閱讀全文

與c語言加密演算法密鑰相關的資料

熱點內容
php基礎編程教程pdf 瀏覽:219
穿越之命令與征服將軍 瀏覽:351
android廣播重復 瀏覽:832
像阿里雲一樣的伺服器 瀏覽:318
水冷空調有壓縮機嗎 瀏覽:478
訪問日本伺服器可以做什麼 瀏覽:432
bytejava詳解 瀏覽:448
androidjava7 瀏覽:384
伺服器在山洞裡為什麼還有油 瀏覽:885
天天基金app在哪裡下載 瀏覽:974
伺服器軟路由怎麼做 瀏覽:291
冰箱壓縮機出口 瀏覽:227
OPT最佳頁面置換演算法 瀏覽:644
網盤忘記解壓碼怎麼辦 瀏覽:853
文件加密看不到裡面的內容 瀏覽:654
程序員腦子里都想什麼 瀏覽:434
oppp手機信任app在哪裡設置 瀏覽:189
java地址重定向 瀏覽:272
一年級下冊摘蘋果的演算法是怎樣的 瀏覽:448
程序員出軌電視劇 瀏覽:90