Exemple #1
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 #2
0
/*将二进制位串转为长度为8的字符串*/
int Bit64ToChar8(ElemType bit[64],ElemType ch[8]){
 int cnt;
 memset(ch,0,8);
 for(cnt = 0; cnt < 8; cnt++){
  BitToByte(bit+(cnt<<3),ch+cnt);
 }
 return 0;
}
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
unsigned char Bit64ToChar8(unsigned char bit[64],unsigned char ch[8])
{
    unsigned char cnt;
    memset(ch,0,8);
    for(cnt = 0; cnt < 8; cnt++)
	{
        BitToByte(bit+(cnt<<3),ch+cnt);
    }
    return 0;
}
Exemple #6
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); //转换成位
	}
}