導航:首頁 > 源碼編譯 > c加密演算法

c加密演算法

發布時間:2022-02-26 03:48:22

A. C語言加密演算法(簡單)

MD5是HASH演算法,他不能用來解密的,他主要是用來校驗信息的完整型,也就是我們常說的數值簽名,你可以去RFC文檔上收索,上邊有他具體的演算法,代碼也是封裝好了的,可以去研究研究

B. C語言加密演算法

#include<stdio.h>

intconstN=10;

//將方陣a[N][N]第row行循環左移m位
voidRowLeftn(chara[][N],intn,introw,intm){
inti,j,t;
if(row<0||row>n-1)return;
for(i=0;i<m;++i){
t=a[row][0];
for(j=0;j<n-1;++j)
a[row][j]=a[row][j+1];
a[row][n-1]=t;
}
}

//將方陣a[N][N]第col列循環上移m位
voidColUpn(chara[][N],intn,intcol,intm){
inti,j,t;
if(col<0||col>n-1)return;
for(i=0;i<m;++i){
t=a[0][col];
for(j=0;j<n-1;++j)
a[j][col]=a[j+1][col];
a[n-1][col]=t;
}
}

intmain(){
chartxt[N][N];
inta[N],b[N],i,j,n;
while(scanf("%d",&n)==1&&n>0){
fflush(stdin);
for(i=0;i<n;++i){
for(j=0;j<n;++j)
scanf("%c",&txt[i][j]);
}
for(i=0;i<n;++i){
for(j=0;j<n;++j)
printf("%c",txt[i][j]);
printf(" ");
}
for(i=0;i<n;++i)scanf("%d",&a[i]);
for(i=0;i<n;++i)scanf("%d",&b[i]);
for(i=0;i<n;++i)RowLeftn(txt,n,i,a[i]);
for(i=0;i<n;++i)ColUpn(txt,n,i,b[i]);
for(i=0;i<n;++i){
for(j=0;j<n;++j)
printf("%c",txt[i][j]);
}
printf(" ");
}
return0;
}

C. 用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;
}
如果密碼是字元串的話,最簡單的加密演算法就是對每個字元重新映射,只要加密解密雙方共同遵守同一個映射規則就行啦。

D. 用c語言設計了一個加密演算法:用a代替z,用b代替y,用c代替x,……,用z代替a。

#include <stdio.h>

int main()
{
char s[100],*p;
printf("請輸入字元串 : ");
gets(s);
p = s;
while(*p)
{
if((*p >= 'a') && (*p <= 'z')) /*處理小寫*/
{
*p ='z' - *p + 'a';
}
if((*p >= 'A') && (*p <= 'Z')) /*處理大寫,同理處理數字亦一樣..自己例推*/
{
*p ='Z' - *p + 'A';
}

p++;
}
printf("轉換後的字元串為 : %s\n\n",s);
return 0;
}

E. aes加密演算法C代碼

這答案是DES加密演算法,不是AES,你還「3q」。。。。。。。。

F. 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);
}

G. 用C語言設計一個移位加密演算法

很簡單的,直接讀取字元串,然後放進字元串數組里,然後根據密鑰的數字i ,得到i的位數,除以相應的1000,100,10等,最後到1,得到密鑰的各個位上的數,然後將其也放進一個數組里,然後,對第二個密鑰的數組讀出來直接操作第一個明文的數組,再將操作的數組放進另一個數組中即可。

H. des加密演算法(c/c++)

des.h文件:

#ifndefCRYPTOPP_DES_H

#defineCRYPTOPP_DES_H

#include"cryptlib.h"

#include"misc.h"

NAMESPACE_BEGIN(CryptoPP)

classDES:publicBlockTransformation

{

public:

DES(constbyte*userKey,CipherDir);

voidProcessBlock(constbyte*inBlock,byte*outBlock)const;

voidProcessBlock(byte*inoutBlock)const

{DES::ProcessBlock(inoutBlock,inoutBlock);}

enum{KEYLENGTH=8,BLOCKSIZE=8};

unsignedintBlockSize()const{returnBLOCKSIZE;}

protected:

staticconstword32Spbox[8][64];

SecBlock<word32>k;

};

classDESEncryption:publicDES

{

public:

DESEncryption(constbyte*userKey)

:DES(userKey,ENCRYPTION){}

};

classDESDecryption:publicDES

{

public:

DESDecryption(constbyte*userKey)

:DES(userKey,DECRYPTION){}

};

classDES_EDE_Encryption:publicBlockTransformation

{

public:

DES_EDE_Encryption(constbyte*userKey)

:e(userKey,ENCRYPTION),d(userKey+DES::KEYLENGTH,DECRYPTION){}

voidProcessBlock(constbyte*inBlock,byte*outBlock)const;

voidProcessBlock(byte*inoutBlock)const;

enum{KEYLENGTH=16,BLOCKSIZE=8};

unsignedintBlockSize()const{returnBLOCKSIZE;}

private:

DESe,d;

};

classDES_EDE_Decryption:publicBlockTransformation

{

public:

DES_EDE_Decryption(constbyte*userKey)

:d(userKey,DECRYPTION),e(userKey+DES::KEYLENGTH,ENCRYPTION){}

voidProcessBlock(constbyte*inBlock,byte*outBlock)const;

voidProcessBlock(byte*inoutBlock)const;

enum{KEYLENGTH=16,BLOCKSIZE=8};

unsignedintBlockSize()const{returnBLOCKSIZE;}

private:

DESd,e;

};

classTripleDES_Encryption:publicBlockTransformation

{

public:

TripleDES_Encryption(constbyte*userKey)

:e1(userKey,ENCRYPTION),d(userKey+DES::KEYLENGTH,DECRYPTION),

e2(userKey+2*DES::KEYLENGTH,ENCRYPTION){}

voidProcessBlock(constbyte*inBlock,byte*outBlock)const;

voidProcessBlock(byte*inoutBlock)const;

enum{KEYLENGTH=24,BLOCKSIZE=8};

unsignedintBlockSize()const{returnBLOCKSIZE;}

private:

DESe1,d,e2;

};

classTripleDES_Decryption:publicBlockTransformation

{

public:

TripleDES_Decryption(constbyte*userKey)

:d1(userKey+2*DES::KEYLENGTH,DECRYPTION),e(userKey+DES::KEYLENGTH,ENCRYPTION),

d2(userKey,DECRYPTION){}

voidProcessBlock(constbyte*inBlock,byte*outBlock)const;

voidProcessBlock(byte*inoutBlock)const;

enum{KEYLENGTH=24,BLOCKSIZE=8};

unsignedintBlockSize()const{returnBLOCKSIZE;}

private:

DESd1,e,d2;

};

NAMESPACE_END

#endif

des.cpp文件:

//des.cpp-modifiedbyWeiDaifrom:

/*

*

*circa1987,'s1977

*publicdomaincode.,but

*theactualencrypt/

*Outerbridge'sDEScodeasprintedinSchneier's"AppliedCryptography."

*

*Thiscodeisinthepublicdomain.Iwouldappreciatebugreportsand

*enhancements.

*

*PhilKarnKA9Q,[email protected],August1994.

*/

#include"pch.h"

#include"misc.h"

#include"des.h"

NAMESPACE_BEGIN(CryptoPP)

/*

*Threeofthesetables,theinitialpermutation,thefinal

*,areregularenoughthat

*forspeed,wehard-codethem.They'rehereforreferenceonly.

*Also,,gensp.c,

*tobuildthecombinedSPbox,Spbox[].They'realsoherejust

*forreference.

*/

#ifdefnotdef

/*initialpermutationIP*/

staticbyteip[]={

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

};

/*finalpermutationIP^-1*/

staticbytefp[]={

40,8,48,16,56,24,64,32,

39,7,47,15,55,23,63,31,

38,6,46,14,54,22,62,30,

37,5,45,13,53,21,61,29,

36,4,44,12,52,20,60,28,

35,3,43,11,51,19,59,27,

34,2,42,10,50,18,58,26,

33,1,41,9,49,17,57,25

};

/*expansionoperationmatrix*/

staticbyteei[]={

32,1,2,3,4,5,

4,5,6,7,8,9,

8,9,10,11,12,13,

12,13,14,15,16,17,

16,17,18,19,20,21,

20,21,22,23,24,25,

24,25,26,27,28,29,

28,29,30,31,32,1

};

/*The(in)famousS-boxes*/

staticbytesbox[8][64]={

/*S1*/

14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,

0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,

4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,

15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,

/*S2*/

15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,

3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,

0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,

13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,

/*S3*/

10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,

13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,

13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,

1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,

/*S4*/

7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,

13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,

10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,

3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,

/*S5*/

2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,

14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,

4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,

11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,

/*S6*/

12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,

10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,

9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,

4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,

/*S7*/

4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,

13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,

1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,

6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,

/*S8*/

13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,

1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,

7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,

2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11

};

/*32--boxes*/

staticbytep32i[]={

16,7,20,21,

29,12,28,17,

1,15,23,26,

5,18,31,10,

2,8,24,14,

32,27,3,9,

19,13,30,6,

22,11,4,25

};

#endif

/*permutedchoicetable(key)*/

staticconstbytepc1[]={

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

};

/*numberleftrotationsofpc1*/

staticconstbytetotrot[]={

1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28

};

/*permutedchoicekey(table)*/

staticconstbytepc2[]={

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

};

/*EndofDES-definedtables*/

/*bit0isleft-mostinbyte*/

staticconstintbytebit[]={

0200,0100,040,020,010,04,02,01

};

/*Setkey(initializekeyschelearray)*/

DES::DES(constbyte*key,CipherDirdir)

:k(32)

{

SecByteBlockbuffer(56+56+8);

byte*constpc1m=buffer;/*placetomodifypc1into*/

byte*constpcr=pc1m+56;/*placetorotatepc1into*/

byte*constks=pcr+56;

registerinti,j,l;

intm;

for(j=0;j<56;j++){/*convertpc1tobitsofkey*/

l=pc1[j]-1;/*integerbitlocation*/

m=l&07;/*findbit*/

pc1m[j]=(key[l>>3]&/*findwhichkeybytelisin*/

bytebit[m])/*andwhichbitofthatbyte*/

?1:0;/*andstore1-bitresult*/

}

for(i=0;i<16;i++){/*keychunkforeachiteration*/

memset(ks,0,8);/*Clearkeyschele*/

for(j=0;j<56;j++)/*rotatepc1therightamount*/

pcr[j]=pc1m[(l=j+totrot[i])<(j<28?28:56)?l:l-28];

/**/

for(j=0;j<48;j++){/*selectbitsindivially*/

/*checkbitthatgoestoks[j]*/

if(pcr[pc2[j]-1]){

/*maskitinifit'sthere*/

l=j%6;

ks[j/6]|=bytebit[l]>>2;

}

}

/*Nowconverttoodd/eveninterleavedformforuseinF*/

k[2*i]=((word32)ks[0]<<24)

|((word32)ks[2]<<16)

|((word32)ks[4]<<8)

|((word32)ks[6]);

k[2*i+1]=((word32)ks[1]<<24)

|((word32)ks[3]<<16)

|((word32)ks[5]<<8)

|((word32)ks[7]);

}

if(dir==DECRYPTION)//reversekeyscheleorder

for(i=0;i<16;i+=2)

{

std::swap(k[i],k[32-2-i]);

std::swap(k[i+1],k[32-1-i]);

}

}

/**/

/*Ccodeonlyinportableversion*/

//RichardOuterbridge'sinitialpermutationalgorithm

/*

inlinevoidIPERM(word32&left,word32&right)

{

word32work;

work=((left>>4)^right)&0x0f0f0f0f;

right^=work;

left^=work<<4;

work=((left>>16)^right)&0xffff;

right^=work;

left^=work<<16;

work=((right>>2)^left)&0x33333333;

left^=work;

right^=(work<<2);

work=((right>>8)^left)&0xff00ff;

left^=work;

right^=(work<<8);

right=rotl(right,1);

work=(left^right)&0xaaaaaaaa;

left^=work;

right^=work;

left=rotl(left,1);

}

inlinevoidFPERM(word32&left,word32&right)

{

word32work;

right=rotr(right,1);

work=(left^right)&0xaaaaaaaa;

left^=work;

right^=work;

left=rotr(left,1);

work=((left>>8)^right)&0xff00ff;

right^=work;

left^=work<<8;

work=((left>>2)^right)&0x33333333;

right^=work;

left^=work<<2;

work=((right>>16)^left)&0xffff;

left^=work;

right^=work<<16;

work=((right>>4)^left)&0x0f0f0f0f;

left^=work;

right^=work<<4;

}

*/

//WeiDai''sinitialpermutation

//algorithm,

//(likeinMSVC)

inlinevoidIPERM(word32&left,word32&right)

{

word32work;

right=rotl(right,4U);

work=(left^right)&0xf0f0f0f0;

left^=work;

right=rotr(right^work,20U);

work=(left^right)&0xffff0000;

left^=work;

right=rotr(right^work,18U);

work=(left^right)&0x33333333;

left^=work;

right=rotr(right^work,6U);

work=(left^right)&0x00ff00ff;

left^=work;

right=rotl(right^work,9U);

work=(left^right)&0xaaaaaaaa;

left=rotl(left^work,1U);

right^=work;

}

inlinevoidFPERM(word32&left,word32&right)

{

word32work;

right=rotr(right,1U);

work=(left^right)&0xaaaaaaaa;

right^=work;

left=rotr(left^work,9U);

work=(left^right)&0x00ff00ff;

right^=work;

left=rotl(left^work,6U);

work=(left^right)&0x33333333;

right^=work;

left=rotl(left^work,18U);

work=(left^right)&0xffff0000;

right^=work;

left=rotl(left^work,20U);

work=(left^right)&0xf0f0f0f0;

right^=work;

left=rotr(left^work,4U);

}

//

voidDES::ProcessBlock(constbyte*inBlock,byte*outBlock)const

{

word32l,r,work;

#ifdefIS_LITTLE_ENDIAN

l=byteReverse(*(word32*)inBlock);

r=byteReverse(*(word32*)(inBlock+4));

#else

l=*(word32*)inBlock;

r=*(word32*)(inBlock+4);

#endif

IPERM(l,r);

constword32*kptr=k;

for(unsignedi=0;i<8;i++)

{

work=rotr(r,4U)^kptr[4*i+0];

l^=Spbox[6][(work)&0x3f]

^Spbox[4][(work>>8)&0x3f]

^Spbox[2][(work>>16)&0x3f]

^Spbox[0][(work>>24)&0x3f];

work=r^kptr[4*i+1];

l^=Spbox[7][(work)&0x3f]

^Spbox[5][(work>>8)&0x3f]

^Spbox[3][(work>>16)&0x3f]

^Spbox[1][(work>>24)&0x3f];

work=rotr(l,4U)^kptr[4*i+2];

r^=Spbox[6][(work)&0x3f]

^Spbox[4][(work>>8)&0x3f]

^Spbox[2][(work>>16)&0x3f]

^Spbox[0][(work>>24)&0x3f];

work=l^kptr[4*i+3];

r^=Spbox[7][(work)&0x3f]

^Spbox[5][(work>>8)&0x3f]

^Spbox[3][(work>>16)&0x3f]

^Spbox[1][(work>>24)&0x3f];

}

FPERM(l,r);

#ifdefIS_LITTLE_ENDIAN

*(word32*)outBlock=byteReverse(r);

*(word32*)(outBlock+4)=byteReverse(l);

#else

*(word32*)outBlock=r;

*(word32*)(outBlock+4)=l;

#endif

}

voidDES_EDE_Encryption::ProcessBlock(byte*inoutBlock)const

{

e.ProcessBlock(inoutBlock);

d.ProcessBlock(inoutBlock);

e.ProcessBlock(inoutBlock);

}

voidDES_EDE_Encryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const

{

e.ProcessBlock(inBlock,outBlock);

d.ProcessBlock(outBlock);

e.ProcessBlock(outBlock);

}

voidDES_EDE_Decryption::ProcessBlock(byte*inoutBlock)const

{

d.ProcessBlock(inoutBlock);

e.ProcessBlock(inoutBlock);

d.ProcessBlock(inoutBlock);

}

voidDES_EDE_Decryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const

{

d.ProcessBlock(inBlock,outBlock);

e.ProcessBlock(outBlock);

d.ProcessBlock(outBlock);

}

voidTripleDES_Encryption::ProcessBlock(byte*inoutBlock)const

{

e1.ProcessBlock(inoutBlock);

d.ProcessBlock(inoutBlock);

e2.ProcessBlock(inoutBlock);

}

voidTripleDES_Encryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const

{

e1.ProcessBlock(inBlock,outBlock);

d.ProcessBlock(outBlock);

e2.ProcessBlock(outBlock);

}

voidTripleDES_Decryption::ProcessBlock(byte*inoutBlock)const

{

d1.ProcessBlock(inoutBlock);

e.ProcessBlock(inoutBlock);

d2.ProcessBlock(inoutBlock);

}

voidTripleDES_Decryption::ProcessBlock(constbyte*inBlock,byte*outBlock)const

{

d1.ProcessBlock(inBlock,outBlock);

e.ProcessBlock(outBlock);

d2.ProcessBlock(outBlock);

}

NAMESPACE_END

程序運行如下:

I. 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加密演算法相關的資料

熱點內容
c語言編譯器怎麼打中文 瀏覽:490
加密exe文件打不開怎麼辦 瀏覽:10
仕女pdf 瀏覽:929
安裝儲存伺服器是什麼意思 瀏覽:112
如何改文件夾內照片的後綴 瀏覽:764
程序員與公關關系 瀏覽:202
linuxgpu測試 瀏覽:384
tcl智能鎖用什麼app 瀏覽:143
程序員那麼可愛不好看 瀏覽:890
拳擊沙袋可以解壓嗎 瀏覽:304
周末php培訓班 瀏覽:984
戶型公攤面積快速演算法 瀏覽:323
亞洲7衛星加密節目破解 瀏覽:787
什麼相機app濾鏡好用 瀏覽:815
oracle存儲過程提示編譯完 瀏覽:547
頂級程序員出山 瀏覽:365
java獲取指定路徑 瀏覽:175
xampp教程linux 瀏覽:386
壓縮空氣洗車 瀏覽:707
cad中命令zoome 瀏覽:1001