Blowfish算法C#实现.docx
《Blowfish算法C#实现.docx》由会员分享,可在线阅读,更多相关《Blowfish算法C#实现.docx(16页珍藏版)》请在冰豆网上搜索。
![Blowfish算法C#实现.docx](https://file1.bdocx.com/fileroot1/2023-6/1/0fdda7d7-ecad-4fed-b198-7d1fa3c0ee2a/0fdda7d7-ecad-4fed-b198-7d1fa3c0ee2a1.gif)
Blowfish算法C#实现
Blowfish算法C#实现
BlowfishC#
usingSystem;
namespaceBlowfish_NET
{
///
///BlowfishCBCimplementation.
///
///
///Usethisclasstoencryptordecryptbytearraysorasingleblocks
///withBlowfishinCBC(CipherBlockBlockChaining)mode.Thisisthe
///recommendedwaytouseBlowfish.NET,unlesscertainrequirements
///(e.g.movingblockwithoutdecryption)exist.
///
publicclassBlowfishCBC:
BlowfishECB
{
//(westoretheIVastwo32bitintegers,tovoidpackingand
//unpackinginbetweenthehandlingofdatachunks)
uintm_ivHi;
uintm_ivLo;
//////////////////////////////////////////////////////////////////////
///
///Thecurrentinitializationvector(IV),whichmeasuresone
///block.Propertyforconvientreadandwrites.
///
publicbyte[]IV
{
set
{
SetIV(value,0);
}
get
{
byte[]result=newbyte[BLOCK_SIZE];
GetIV(result,0);
returnresult;
}
}
//////////////////////////////////////////////////////////////////////
///
///Setstheinitializationvector(IV).
///
///buffercontainingthenewIVmaterial
///wheretheIVmaterialstarts
publicvoidSetIV(
byte[]buf,
intnOfs)
{
m_ivHi=(((uint)buf[nOfs])<<24)|
(((uint)buf[nOfs+1])<<16)|
(((uint)buf[nOfs+2])<<8)|
buf[nOfs+3];
m_ivLo=(((uint)buf[nOfs+4])<<24)|
(((uint)buf[nOfs+5])<<16)|
(((uint)buf[nOfs+6])<<8)|
buf[nOfs+7];
}
///
///GetsthecurrentIVmaterial(ofthesizeofoneblock).
///
///buffertocopytheIVto
///wheretostartcopying
publicvoidGetIV(
byte[]buf,
intnOfs)
{
uintivHi=m_ivHi;
uintivLo=m_ivLo;
buf[nOfs++]=(byte)(ivHi>>24);
buf[nOfs++]=(byte)(ivHi>>16);
buf[nOfs++]=(byte)(ivHi>>8);
buf[nOfs++]=(byte)ivHi;
buf[nOfs++]=(byte)(ivLo>>24);
buf[nOfs++]=(byte)(ivLo>>16);
buf[nOfs++]=(byte)(ivLo>>8);
buf[nOfs]=(byte)ivLo;
}
//////////////////////////////////////////////////////////////////////
///
///Defaultconstructor.TheIVneedstobeassignedafterthe
///instancehasbeencreated.
///
///
publicBlowfishCBC(
byte[]key,
intnOfs,
intnLen):
base(key,nOfs,nLen)
{
}
//////////////////////////////////////////////////////////////////////
///
///Zerokeyconstructor,callInitialize()togetaproperlysetup
///instance,thenapplytheIV.
///
publicBlowfishCBC():
base(null,0,0)
{
}
//////////////////////////////////////////////////////////////////////
///
publicnewvoidInvalidate()
{
base.Invalidate();
m_ivHi=m_ivLo=0;
}
//////////////////////////////////////////////////////////////////////
///
publicnewvoidEncryptBlock(
uintunHi,
uintunLo,
outuintunOutHi,
outuintunOutLo)
{
byte[]block;
block=m_block;
block[0]=(byte)(unHi>>24);
block[1]=(byte)(unHi>>16);
block[2]=(byte)(unHi>>8);
block[3]=(byte)unHi;
block[4]=(byte)(unLo>>24);
block[5]=(byte)(unLo>>16);
block[6]=(byte)(unLo>>8);
block[7]=(byte)unLo;
Encrypt(block,0,block,0,BLOCK_SIZE);
unOutHi=(((uint)block[0])<<24)|
(((uint)block[1])<<16)|
(((uint)block[2])<<8)|
block[3];
unOutLo=(((uint)block[4])<<24)|
(((uint)block[5])<<16)|
(((uint)block[6])<<8)|
block[7];
}
//////////////////////////////////////////////////////////////////////
///
publicnewvoidDecryptBlock(
uintunHi,
uintunLo,
outuintunOutHi,
outuintunOutLo)
{
byte[]block;
block=m_block;
block[0]=(byte)(unHi>>24);
block[1]=(byte)(unHi>>16);
block[2]=(byte)(unHi>>8);
block[3]=(byte)unHi;
block[4]=(byte)(unLo>>24);
block[5]=(byte)(unLo>>16);
block[6]=(byte)(unLo>>8);
block[7]=(byte)unLo;
Decrypt(block,0,block,0,BLOCK_SIZE);
unOutHi=(((uint)block[0])<<24)|
(((uint)block[1])<<16)|
(((uint)block[2])<<8)|
block[3];
unOutLo=(((uint)block[4])<<24)|
(((uint)block[5])<<16)|
(((uint)block[6])<<8)|
block[7];
}
//////////////////////////////////////////////////////////////////////
///
publicnewintEncrypt(
byte[]dataIn,
intnPosIn,
byte[]dataOut,
intnPosOut,
intnCount)
{
intnEnd;
uintunSwap;
uint[]sbox1=m_sbox1;
uint[]sbox2=m_sbox2;
uint[]sbox3=m_sbox3;
uint[]sbox4=m_sbox4;
uint[]pbox=m_pbox;
uintpbox00=pbox[0];
uintpbox01=pbox[1];
uintpbox02=pbox[2];
uintpbox03=pbox[3];
uintpbox04=pbox[4];
uintpbox05=pbox[5];
uintpbox06=pbox[6];
uintpbox07=pbox[7];
uintpbox08=pbox[8];
uintpbox09=pbox[9];
uintpbox10=pbox[10];
uintpbox11=pbox[11];
uintpbox12=pbox[12];
uintpbox13=pbox[13];
uintpbox14=pbox[14];
uintpbox15=pbox[15];
uintpbox16=pbox[16];
uintpbox17=pbox[17];
//(alittle,butniftytrick:
wedon'tneedanextraIVpair,but
//weusethelocalblockcacheinreversedorder)
uintunHi=m_ivHi;
uintunLo=m_ivLo;
nCount&=~(BLOCK_SIZE-1);
nEnd=nPosIn+nCount;
while(nPosIn{
unHi^=(((uint)dataIn[nPosIn])<<24)|
(((uint)dataIn[nPosIn+1])<<16)|
(((uint)dataIn[nPosIn+2])<<8)|
dataIn[nPosIn+3];
unLo^=(((uint)dataIn[nPosIn+4])<<24)|
(((uint)dataIn[nPosIn+5])<<16)|
(((uint)dataIn[nPosIn+6])<<8)|
dataIn[nPosIn+7];
nPosIn+=8;
unHi^=pbox00;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^
sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox01;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^
sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox02;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^
sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox03;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^
sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox04;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox05;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox06;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox07;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox08;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox09;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox10;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox11;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox12;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox13;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox14;
unLo^=(((sbox1[(int)(unHi>>24)]+sbox2[(int)((unHi>>16)&0x0ff)])^sbox3[(int)((unHi>>8)&0x0ff)])+sbox4[(int)(unHi&0x0ff)])^pbox15;
unHi^=(((sbox1[(int)(unLo>>24)]+sbox2[(int)((unLo>>16)&0x0ff)])^sbox3[(int)((unLo>>8)&0x0ff)])+sbox4[(int)(unLo&0x0ff)])^pbox16;
//...whichmakesthisswapnecessary
unSwap=unLo^pbox17;
unLo=unHi;
unHi=unSwap;
dataOut[nPosOut]=(byte)(unHi>>24);
dataOut[nPosOut+1]=(byte)(unHi>>16);
dataOut[nPosOut+2]=(byte)(unHi>>8);
dataOut[nPosOut+3]=(byte)unHi;
dataOut[nPosOut+4]=(byte)(unLo>>24);
dataOut[nPosOut+5]=(byte)(unLo>>16);
dataOut[nPosOut+6]=(byte)(unLo>>8);
dataOut[nPosOut+7]=(byte)unLo;
nPosOut+=8;
}
m_ivHi=unHi;
m_ivLo=unLo;
returnnCount;
}
//////////////////////////////////////////////////////////////////////
///
publicnewintDecrypt(
byte[]dataIn,
intnPosIn,
byte[]dataOut,
intnPosOut,
intnCount)
{
intnEnd;
uintunHi,unLo,unHiBak,unLoBak;
uint[]sbox1=m_sbox1;
uint[]sbox2=m_sbox2;
uint[]sbox3=m_sbox3;
uint[]sbox4=m_sbox4;
uint[]pbox=m_pbox;
uintpbox00=pbox[0];
uintpbox01=pbox[1];
uintpbox02=pbox[2];
uintpbox03=pbox[3];
uintpbox04=pbox[4];
uintpbox05=pbox[5];
uintpbox06=pbox[6];
uintpbox07=pbox[7];
uintpbox08=pbox[8];
uintpbox09=pbox[9];
uintpbox10=pbox[10];
uintpbox11=pbox[11];
uintpbox12=pbox[12];
uintpbox13=pbox[13];
uintpbox14=pbox[14];
uintpbox15=pbox[15];
uintpbox16=pbox[16];
uintpbox17=pbox[17];
uintivHi=m_ivHi;
uintivLo=m_ivLo;
nCount&=~(BLOCK_SIZE-1);
nEnd=nPosIn+nCount;
while(nPosIn{
unHi=unHiBak=(((uint)dataIn[nPosIn])<<24)|
(((uint)dataIn[nPosIn+1])<<16)