📄 test_blowfish.cpp
字号:
}
};
const unsigned long pbox[18]=
{
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
0x9216d5d9, 0x8979fb1b
};
DWORD keybox[18+4*256];
template <class T>
void swap(T& x,T& y)
{
T temp;
temp=x;
x=y;
y=temp;
}
void BlowfishKeyInit();
unsigned long F(const char Bytes[4]);
void BlowfishEncipher(unsigned long& XL,unsigned long& XR);
void BlowfishDecipher(unsigned long& XL,unsigned long& XR);
void BlowfishKeyInit(char* strKey,int nLen)
{
/**//////////////////////////////////////
//把pbox,sbox中的内容复制到keybox中
unsigned long* pKey=keybox;
memcpy(pKey,pbox,sizeof(long)*18);
pKey+=18;
memcpy(pKey,sbox,sizeof(long)*4*256);
/**///////////////////////////////////////
//处理字符串key如果长度不够18*4=72bytes就扩展。
int i,oldlen=nLen;
char *cp=NULL;
if(nLen<72)
{
cp=new char[72*2];
memset(cp,0,sizeof(char)*72*2);
nLen=0;
while(nLen<72)
{
strcat(cp,strKey);
nLen+=oldlen;
}
}
else
cp=strKey;
/**////////////////////////////////
//用已处理的key和keybox中的前18个元素异或(也就是pbox的内容)
pKey=(unsigned long*)cp;
for (i=0;i<18;i++)
keybox[i]^=pKey[i];
/**////////////////////////////////////////
//用函数BlowfishEncipher迭代521=((18+256*4)/2)次,初始xl=xr=0,输出用来填充keybox;
unsigned long xl=0,xr=0;
for (i=0;i<521;i++)
{
BlowfishEncipher(xl,xr);
keybox[2*i]=xl;
keybox[2*i+1]=xr;
}
printf("\ninitialized!");
}
void BlowfishEncipher(unsigned long& XL,unsigned long& XR)
{
unsigned long xl=XL,xr=XR,temp=0;
int i;
for (i=0;i<16;i++)
{
xl^=keybox[i];
xr^=F((const char*)&xl);
swap(xl,xr);
}
swap(xl,xr);
xr^=keybox[16];
xl^=keybox[17];
XR=xr;
XL=xl;
}
unsigned long F(const char Bytes[4])
{
unsigned long temp;
temp=keybox[SBOX_BEGIN+Bytes[3]]+keybox[SBOX_BEGIN+256+Bytes[2]];
temp^=keybox[SBOX_BEGIN+256*2+Bytes[1]];
temp+=keybox[SBOX_BEGIN+256*3+Bytes[0]];
return temp;
}
void BlowfishDecipher(unsigned long& XL,unsigned long& XR)
{
unsigned long xl=XL,xr=XR,temp=0;
int i;
for (i=17;i>1;i--)
{
xl^=keybox[i];
xr^=F((const char*)&xl);
swap(xl,xr);
}
swap(xl,xr);
xr^=keybox[1];
xl^=keybox[0];
XR=xr;
XL=xl;
}
int main(int argc,char* argv[])
{
char szKey[100]={0};
char szMessage[100]={0};
printf("input the key:");
gets(szKey);
printf("\nthe key you input is: %s \ninput the message you want to encipher: ",szKey);
BlowfishKeyInit(szKey,strlen(szKey));
gets(szMessage);
printf("\nbefore encipher,the message is: %s ",szMessage);
int i;
int len=strlen(szMessage);
while(len%8!=0)//因为是64bits分组处理,所以字符串的长度必须为8的倍数。
{
szMessage[len++]=0;
}
for (i=0;i<len;i+=8)
{
BlowfishEncipher((unsigned long&)szMessage[i],(unsigned long&)szMessage[i+4]);
}
printf("\nthe enciphered message is:");
for (i=0;i<strlen(szMessage);i++)
printf("%.2x",(byte)szMessage[i]);
printf(" ");
printf("\ndecipher . . . ");
for (i=0;i<len;i+=8)
{
BlowfishDecipher((unsigned long&)szMessage[i],(unsigned long&)szMessage[i+4]);
}
printf("\nnow, the message is: %s ",szMessage);
getchar();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -