資源簡介
RSA AES DES ECC加密算法源碼
MFC編程實現(xiàn)
MFC編程實現(xiàn)
代碼片段和文件信息
//?AES.cpp?:?implementation?file
//
/*?
???Written?by?Jianqin?Zhou?July?2009
???zhou9@yahoo.com
?
???Compiles?and?runs?fine?as?a?VC?6.0?program.
??
???Permission?for?free?direct?or?derivative?use?is?granted?subject?
???to?compliance?with?any?conditions?that?the?originators?of?the?
???algorithm?place?on?its?exploitation.??
???
???Written?for?clarity?rather?than?speed.
???Full?implementation.?
??*/
#include?“stdafx.h“
#include?“crypt.h“
#include?“AES.h“
#ifdef?_DEBUG
#define?new?DEBUG_NEW
#undef?THIS_FILE
static?char?THIS_FILE[]?=?__FILE__;
#endif
#include?
#define?BYTE?unsigned?char???????/*?8?bits??*/
#define?WORD?unsigned?long???????/*?32?bits?*/
/*?rotates?x?one?bit?to?the?left?*/
#define?ROTL(x)?(((x)>>7)|((x)<<1))
/*?Rotates?32-bit?word?left?by?1?2?or?3?byte??*/
#define?ROTL8(x)?(((x)<<8)|((x)>>24))
#define?ROTL16(x)?(((x)<<16)|((x)>>16))
#define?ROTL24(x)?(((x)<<24)|((x)>>8))
/*?Fixed?Data?*/
static?BYTE?InCo[4]={0xB0xD0x90xE};??/*?Inverse?Coefficients?*/
static?BYTE?fbsub[256];
static?BYTE?rbsub[256];
static?BYTE?ptab[256]ltab[256];
static?WORD?ftable[256];
static?WORD?rtable[256];
static?WORD?rco[30];
/*?Parameter-dependent?data?*/
int?NkNbNr;
BYTE?fi[24]ri[24];
WORD?fkey[120];
WORD?rkey[120];
static?WORD?pack(BYTE?*b)
{?/*?pack?bytes?into?a?32-bit?Word?*/
????return?((WORD)b[3]<<24)|((WORD)b[2]<<16)|((WORD)b[1]<<8)|(WORD)b[0];
}
static?void?unpack(WORD?aBYTE?*b)
{?/*?unpack?bytes?from?a?word?*/
????b[0]=(BYTE)a;
????b[1]=(BYTE)(a>>8);
????b[2]=(BYTE)(a>>16);
????b[3]=(BYTE)(a>>24);
}
//關(guān)于模多項式0x011b的乘10b運算
static?BYTE?xtime(BYTE?a)
{
????BYTE?b;
????if?(a&0x80)?b=0x1B;
????else????????b=0;
????a<<=1;
????a^=b;
????return?a;
}
static?BYTE?bmul(BYTE?xBYTE?y)
{?/*?x.y=?AntiLog(Log(x)?+?Log(y))?*/
????if?(x?&&?y)?return?ptab[(ltab[x]+ltab[y])%255];
????else?return?0;
}
static?WORD?SubByte(WORD?a)
{
????BYTE?b[4];
????unpack(ab);
????b[0]=fbsub[b[0]];
????b[1]=fbsub[b[1]];
????b[2]=fbsub[b[2]];
????b[3]=fbsub[b[3]];
????return?pack(b);????
}
static?BYTE?product(WORD?xWORD?y)
{?/*?dot?product?of?two?4-byte?arrays?*/
????BYTE?xb[4]yb[4];
????unpack(xxb);
????unpack(yyb);?
????return?bmul(xb[0]yb[0])^bmul(xb[1]yb[1])^bmul(xb[2]yb[2])^bmul(xb[3]yb[3]);
}
static?WORD?InvMixCol(WORD?x)
{?/*?matrix?Multiplication?*/
????WORD?ym;
????BYTE?b[4];
????m=pack(InCo);
????b[3]=product(mx);
????m=ROTL24(m);
????b[2]=product(mx);
????m=ROTL24(m);
????b[1]=product(mx);
????m=ROTL24(m);
????b[0]=product(mx);
????y=pack(b);
????return?y;
}
BYTE?ByteSub(BYTE?x)
{
????BYTE?y=ptab[255-ltab[x]];??/*?multiplicative?inverse?*/
????x=y;??x=ROTL(x);
????y^=x;?x=ROTL(x);
????y^=x;?x=ROTL(x);
????y^=x;?x=ROTL(x);
????y^=x;?y^=0x63;
????return?y;
}
void?gentables(void)
{?/*?generate?tables?*/
????int?i;
????BYTE?yb[4];
??/*?use?3?as?primitive?root?to?generate?power?and
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????535??2009-07-29?16:20??crypt.dsw
?????文件???????1317??2009-08-01?09:19??crypt.h
?????文件?????115712??2009-08-05?15:25??crypt.ncb
?????文件??????58880??2009-08-05?15:25??crypt.opt
?????文件???????2534??2009-08-05?15:24??crypt.plg
?????文件??????19393??2009-08-05?15:09??crypt.rc
?????文件???????6193??2009-08-05?15:23??cryptDlg.cpp
?????文件???????1560??2009-08-05?07:26??cryptDlg.h
?????文件??????17634??2009-08-05?15:23??DES.cpp
?????文件???????1295??2009-08-05?14:54??DES.h
?????文件???????3561??2009-07-29?16:20??ReadMe.txt
?????文件???????2529??2009-08-05?07:20??resource.h
?????文件????????207??2009-07-29?16:20??StdAfx.cpp
?????文件???????1054??2009-07-29?16:20??StdAfx.h
?????文件???????1078??2009-07-29?16:20??res\crypt.ico
?????文件????????397??2009-07-29?16:20??res\crypt.rc2
?????文件??????12193??2009-08-05?15:23??AES.cpp
?????文件???????1275??2009-08-05?14:47??AES.h
?????文件??????47000??2009-08-05?15:09??crypt.aps
?????文件??????10633??2009-08-05?15:24??crypt.clw
?????文件???????2426??2009-08-05?15:23??crypt.cpp
?????文件???????4302??2009-08-05?08:06??crypt.dsp
?????目錄??????????0??2009-07-30?09:18??res
?????文件??????12571??2009-08-05?15:23??DSS.cpp
?????文件???????1280??2009-08-01?15:02??DSS.h
?????文件??????36938??2009-08-05?15:23??RSA.cpp
?????文件???????1334??2009-08-01?15:06??RSA.h
?????文件??????11116??2009-08-05?15:23??DH.cpp
?????文件???????1183??2009-08-05?07:35??DH.h
?????文件??????20867??2009-08-05?15:23??ECC.cpp
............此處省略14個文件信息
評論
共有 條評論