Exemple #1
0
/*将长度为8的字符串转为二进制位串*/
int Char8ToBit64(ElemType ch[8],ElemType bit[64]){
 int cnt;
 for(cnt = 0; cnt < 8; cnt++){  
  ByteToBit(*(ch+cnt),bit+(cnt<<3));
 }
 return 0;
}
Exemple #2
0
static void DES(char* Out, char* In, PSubKey pSubKey, int Type)
{
	int i;
	static char M[64], tmp[32], *Li=&M[0], *Ri=&M[32];

	ByteToBit(M, In, 64);
	Transform(M, M, IP_Table, 64);

	if (Type==ENCRYPT)
	{
		for (i=0; i<16; ++i)
		{
			memcpy(tmp, Ri, 32);
			F_func(Ri, (*pSubKey)[i]);
			Xor(Ri, Li, 32);
			memcpy(Li, tmp, 32);
		}
	}
	else
	{
		for (i=15; i>=0; --i)
		{
			memcpy(tmp, Li, 32);
			F_func(Li, (*pSubKey)[i]);
			Xor(Li, Ri, 32);
			memcpy(Ri, tmp, 32);
		}
	}

	Transform(M, M, IPR_Table, 64);
	BitToByte(Out, M, 64);
}
Exemple #3
0
void Recover(char MesOut[4],char MesIn[4])
//combine several functions(for decryption)
{
     static bool MesBit[32]={0};
     ByteToBit(MesBit,MesIn,32);
     TablePermute(MesBit,MesBit,IP_Tabletwo,32);
     BitToByte(MesOut,MesBit,32);
}
Exemple #4
0
void PlayPermute(char MesOut[4],char MesIn[4])
//combine several functions(for encrytion)
{
    static bool MesBit[32]={0};
    ByteToBit(MesBit,MesIn,32);
    TablePermute(MesBit,MesBit,IP_Table,32);
    BitToByte(MesOut,MesBit,32);
}
Exemple #5
0
//将长度为8的字符串转为二进制位串
int Char8ToBit64(char ch[8], char bit[64])
{
	int cnt;
	for (cnt = 0; cnt < 8; cnt++)
	{
		ByteToBit(*(ch + cnt), bit + (cnt << 3));
	}
	return 0;
}
Exemple #6
0
unsigned char Char8ToBit64(unsigned char ch[8],unsigned char bit[64])
{
    unsigned char cnt;
    for(cnt = 0; cnt < 8; cnt++)
	{       
        ByteToBit(*(ch+cnt),bit+(cnt<<3));
    }
    return 0;
}
Exemple #7
0
static void S_func(char* Out, char* In)
{
	int i, j, k;
	
	for (i=0; i<8; i++)
	{
		j = (In[0]<<1) + In[5];
		k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
		ByteToBit(Out,(char *) &S_Box[i][j][k], 4);
		In+=6;
		Out+=4;
	}
}
Exemple #8
0
static void SetSubKey(PSubKey pSubKey, char* Key)
{
	int i;
	static char K[64], *KL = &K[0], *KR = &K[28];

	ByteToBit(K, Key, 64);
	Transform(K, K, PC1_Table, 56);
	for (i=0; i<16; ++i)
	{
		RotateL(KL, 28, LOOP_Table[i]);
		RotateL(KR, 28, LOOP_Table[i]);
		Transform((*pSubKey)[i], K, PC2_Table, 48);
	}
}
Exemple #9
0
void des(char de[8], char en[8], int MS) //加密,解密
{
	int mw[64], tmp[32], emw[64], a[8];
	int kzmw[48], keyturn[48], ss[32];
	int hang, lie, k = 0;
	int lun, j, i, count = 0;
	int *Li, *Ri;
	for (i = 0; i < 8; i++) {
		a[k++] = (int) (en[i]);
	}
	ByteToBit(emw, a, 64);
	for (j = 0; j < 64; j++) {
		count = ip[j] - 1;
		mw[j] = emw[count]; //初始置换
	}
	Li = &mw[0], Ri = &mw[32];
	for (i = 0; i < 48; i++) { //右明文扩展置换
		count = ei[i] - 1;
		kzmw[i] = Ri[count];
	}
	if (MS == 0) //DES加密过程
			{
		for (lun = 0; lun < 16; lun++) {
			for (i = 0; i < 32; i++)
				ss[i] = Ri[i];
			for (i = 0; i < 48; i++) { //右明文扩展置换
				count = ei[i] - 1;
				kzmw[i] = Ri[count];
			}
			for (i = 0; i < 48; i++)
				keyturn[i] = key[lun][i]; //轮密钥
			Xor(kzmw, keyturn, 48);

			for (i = 0; i < 8; i++) //s盒
					{
				hang = kzmw[i * 6] * 2 + kzmw[i * 6 + 5];
				lie = kzmw[i * 6 + 1] * 8 + kzmw[i * 6 + 2] * 4
						+ kzmw[i * 6 + 3] * 2 + kzmw[i * 6 + 4];
				tmp[i * 4 + 3] = sbox[i][(hang + 1) * 16 + lie] % 2;
				tmp[i * 4 + 2] = (sbox[i][(hang + 1) * 16 + lie] / 2) % 2;
				tmp[i * 4 + 1] = (sbox[i][(hang + 1) * 16 + lie] / 4) % 2;
				tmp[i * 4] = (sbox[i][(hang + 1) * 16 + lie] / 8) % 2;
			}
			for (i = 0; i < 32; i++) { //P置换
				count = Pzh[i] - 1;
				Ri[i] = tmp[count];
			}
			Xor(Ri, Li, 32);
			for (i = 0; i < 32; i++) //交换左右明文
					{
				Li[i] = ss[i];
			}
		}
		for (i = 0; i < 32; i++) {
			tmp[i] = Li[i];
			Li[i] = Ri[i];
			Ri[i] = tmp[i];
		}
		for (i = 0; i < 64; i++) {
			count = fp[i] - 1;
			emw[i] = mw[count];
		}
		BitToByte(de, emw, 64); //位到字节的转换
	} else //DES解密过程
	{

		for (lun = 15; lun >= 0; lun--) {
			for (i = 0; i < 32; i++)
				ss[i] = Ri[i];
			for (i = 0; i < 48; i++) { //右明文扩展置换
				count = ei[i] - 1;
				kzmw[i] = Ri[count];
			}
			for (i = 0; i < 48; i++)
				keyturn[i] = key[lun][i]; //轮密钥
			Xor(kzmw, keyturn, 48);
			for (i = 0; i < 8; i++) //s盒
					{
				hang = kzmw[i * 6] * 2 + kzmw[i * 6 + 5];
				lie = kzmw[i * 6 + 1] * 8 + kzmw[i * 6 + 2] * 4
						+ kzmw[i * 6 + 3] * 2 + kzmw[i * 6 + 4];
				tmp[i * 4 + 3] = sbox[i][(hang + 1) * 16 + lie] % 2;
				tmp[i * 4 + 2] = (sbox[i][(hang + 1) * 16 + lie] / 2) % 2;
				tmp[i * 4 + 1] = (sbox[i][(hang + 1) * 16 + lie] / 4) % 2;
				tmp[i * 4] = (sbox[i][(hang + 1) * 16 + lie] / 8) % 2;
			}
			for (i = 0; i < 32; i++) { //P置换
				count = Pzh[i] - 1;
				Ri[i] = tmp[count];
			}
			Xor(Ri, Li, 32); //按位异或
			for (i = 0; i < 32; i++) //交换左右明文
					{
				Li[i] = ss[i];
			}

		}
		for (i = 0; i < 32; i++) {
			tmp[i] = Li[i];
			Li[i] = Ri[i];
			Ri[i] = tmp[i];
		}
		for (i = 0; i < 64; i++) {
			count = fp[i] - 1;
			emw[i] = mw[count];
		}
		BitToByte(de, emw, 64); //转换成位
	}
}