📄 idea.cpp
字号:
#include "StdAfx.h"
#include "Idea.h"
# define maxim 65537
# define fuyi 65536
# define one 65535
# define round 8
CIdea::CIdea(void)
{
}
CIdea::~CIdea(void)
{
}
void CIdea::Init( short unsigned uskey[] )
{
key(uskey,Z); /* generate encryption subkeys Z[i][r] */
de_key(Z,DK); /* compute decryption subkeys DK[i][r] */
}
bool CIdea::Encrypt( unsigned XX[], unsigned YY[])
{
unsigned XXX[5],YYY[5];
XXX[1] = XX[0];
XXX[2] = XX[1];
XXX[3] = XX[2];
XXX[4] = XX[3];
cip(XXX,YYY,Z); /* encipher XX to YY with key Z */
YY[0] = YYY[1];
YY[1] = YYY[2];
YY[2] = YYY[3];
YY[3] = YYY[4];
return 1;
}
bool CIdea::Decrypt( unsigned XX[], unsigned YY[] )
{
unsigned XXX[5],YYY[5];
XXX[1] = XX[0];
XXX[2] = XX[1];
XXX[3] = XX[2];
XXX[4] = XX[3];
cip(XXX,YYY,DK); /* encipher XX to YY with key Z */
YY[0] = YYY[1];
YY[1] = YYY[2];
YY[2] = YYY[3];
YY[3] = YYY[4];
return 1;
}
void CIdea::cip ( unsigned In[5], unsigned Out[5], unsigned Z[7][10] )
{
unsigned int r, x1, x2, x3, x4, kk, t1, t2, a;
x1=In[1]; x2=In[2]; x3=In[3]; x4=In[4];
for (r=1; r<=8; r++) /* the round function */
{
/* the group operation on 64-bits block */
x1 = mul(x1,Z[1][r]); x4 = mul(x4,Z[4][r]);
x2 = (x2 + Z[2][r]) & one; x3 = ( x3 + Z[3][r] ) & one;
/* the function of the MA structure */
kk = mul( Z[5][r], (x1^x3) );
t1 = mul( Z[6][r], ( kk + (x2^x4)) & one );
t2 = ( kk + t1 ) & one;
/* the involutary permutation PI */
x1 = x1^t1; x4 = x4^t2;
a = x2^t2; x2 = x3^t1; x3 = a;
}
/* the output transformation */
Out[1] = mul (x1, Z[1][round+1] );
Out[4] = mul (x4, Z[4][round+1] );
Out[2] = (x3 + Z[2][round+1] ) & one;
Out[3] = (x2 + Z[3][round+1] ) & one;
}
unsigned CIdea::mul ( unsigned a, unsigned b)
{
long int p;
long unsigned q;
if ( a==0 ) p = maxim-b;
else if ( b==0 ) p = maxim-a;
else
{
q=(unsigned long)a*(unsigned long)b;
p=(q & one) - (q>>16); if (p<=0) p=p+maxim;
}
return ( unsigned ) ( p & one );
}
unsigned CIdea::inv ( unsigned xin)
{
long n1, n2, q, r, b1, b2, t;
if ( xin == 0 ) b2 = 0;
else
{
n1=maxim; n2=xin; b2=1; b1=0;
do
{
r = (n1 % n2);
q = (n1-r)/n2;
if ( r== 0)
{
if ( b2<0 )
b2 = maxim+b2;
}
else
{
n1=n2;
n2=r;
t=b2;
b2=b1-q*b2;
b1=t;
}
}while ( r !=0 );
}
return ( unsigned) b2;
}
void CIdea::key ( short unsigned uskey[9], unsigned Z[7][10] )
{
short unsigned S[54];
int i,j,r;
for ( i =1; i<9; i++ ) S[i-1] = uskey[i];
/* shifts */
for ( i = 8; i<54; i++ )
{
if ( (i+2)%8 == 0) /* for S[14], S[22], ... */
S[i] = ( S[i-7]<<9 )^( S[i-14]>>7 ) & one;
else if ( (i+1)%8 == 0) /* for S[15], S[23], .. */
S[i] = ( S[i-15]<<9 )^( S[i-14]>>7 ) & one;
else /* for other S[i] */
S[i] = ( S[i-7]<<9 )^( S[i-6]>>7 ) & one;
}
/* get subkeys */
for ( r=1; r<=round+1; r++ )
{
for ( j=1; j<7; j++ )
{
Z[j][r] = S[ 6*(r-1) + j-1];
}
}
}
void CIdea::de_key ( unsigned Z[7][10], unsigned DK[7][10] )
{
int j;
for ( j = 1; j<=round+1; j++ )
{
DK[1][round-j+2] = inv (Z[1][j]);
DK[4][round-j+2] = inv (Z[4][j]);
if (j==1 || j==round+1 )
{
DK[2][round-j+2] = ( fuyi-Z[2][j] ) & one;
DK[3][round-j+2] = ( fuyi-Z[3][j] ) & one;
}
else
{
DK[2][round-j+2] = ( fuyi-Z[3][j] ) & one;
DK[3][round-j+2] = ( fuyi-Z[2][j] ) & one;
}
}
for ( j=1; j<=round+1; j++ )
{
DK[5][round+1-j] = Z[5][j];
DK[6][round+1-j] = Z[6][j];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -