示例#1
0
/*解密单个分组*/
int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){
 ElemType cipherBits[64];
 ElemType copyRight[48];
 int cnt;
  
 Char8ToBit64(cipherBlock,cipherBits); 
 /*初始置换(IP置换)*/
 DES_IP_Transform(cipherBits);
 
 /*16轮迭代*/
 for(cnt = 15; cnt >= 0; cnt--){
  memcpy(copyRight,cipherBits+32,32);
  /*将右半部分进行扩展置换,从32位扩展到48位*/
  DES_E_Transform(copyRight);
  /*将右半部分与子密钥进行异或操作*/
  DES_XOR(copyRight,subKeys[cnt],48);  
  /*异或结果进入S盒,输出32位结果*/
  DES_SBOX(copyRight);
  /*P置换*/
  DES_P_Transform(copyRight);  
  /*将明文左半部分与右半部分进行异或*/
  DES_XOR(cipherBits,copyRight,32);
  if(cnt != 0){
/*最终完成左右部的交换*/
DES_Swap(cipherBits,cipherBits+32);
  }
 }
 /*逆初始置换(IP^1置换)*/
 DES_IP_1_Transform(cipherBits);
 Bit64ToChar8(cipherBits,plainBlock);
 return 0;
}
示例#2
0
文件: des.cpp 项目: yzmin/Des
//加密单个分组  
int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){  
	ElemType plainBits[64];  
	ElemType copyRight[48];  
	int cnt;  

	Char8ToBit64(plainBlock,plainBits);       
	//初始置换(IP置换)  
	DES_IP_Transform(plainBits);  

	//16轮迭代  
	for(cnt = 0; cnt < 16; cnt++){         
		memcpy(copyRight,plainBits+32,32);  
		//将右半部分进行扩展置换,从32位扩展到48位  
		DES_E_Transform(copyRight);  
		//将右半部分与子密钥进行异或操作  
		DES_XOR(copyRight,subKeys[cnt],48);   
		//异或结果进入S盒,输出32位结果  
		DES_SBOX(copyRight);  
		//P置换  
		DES_P_Transform(copyRight);  
		//将明文左半部分与右半部分进行异或  
		DES_XOR(plainBits,copyRight,32);  
		if(cnt != 15){  
			//最终完成左右部的交换  
			DES_Swap(plainBits,plainBits+32);  
		}  
	}  
	//逆初始置换(IP^1置换)  
	DES_IP_1_Transform(plainBits);  
	Bit64ToChar8(plainBits,cipherBlock);  
	return 0;  
}  
示例#3
0
// 解密单个分组
unsigned char DES_DecryptBlock(unsigned char cipherBlock[8], unsigned char subKeys[16][48],unsigned char plainBlock[8])
{
    unsigned char cipherBits[64];
    unsigned char copyRight[48];
    short cnt;
    Char8ToBit64(cipherBlock,cipherBits);       
    //初始置换(IP置换)
    DES_IP_Transform(cipherBits);
    // 16轮迭代 
    for(cnt = 15; cnt >= 0; cnt--)
	{       
        memcpy(copyRight,cipherBits+32,32);
        //将右半部分进行扩展置换,从32位扩展到48位 
        DES_E_Transform(copyRight);
        // 将右半部分与子密钥进行异或操作 
        DES_XOR(copyRight,subKeys[cnt],48);       
        //异或结果进入S盒,输出32位结果 
        DES_SBOX(copyRight);
        // P置换 
        DES_P_Transform(copyRight);       
        //将明文左半部分与右半部分进行异或 
        DES_XOR(cipherBits,copyRight,32);
        if(cnt != 0)
		{
            // 最终完成左右部的交换
            DES_Swap(cipherBits,cipherBits+32);
        }
    }
    // 逆初始置换(IP^1置换)
    DES_IP_1_Transform(cipherBits);
    Bit64ToChar8(cipherBits,plainBlock);
    return 0;
}
示例#4
0
unsigned char DES_EncryptBlock(unsigned char plainBlock[8], unsigned char subKeys[16][48], unsigned char cipherBlock[8])
{
    unsigned char plainBits[64];
    unsigned char copyRight[48];
    unsigned char cnt;
    Char8ToBit64(plainBlock,plainBits);       
    //初始置换(IP置换) 
    DES_IP_Transform(plainBits);
    // 16轮迭代 
    for(cnt = 0; cnt < 16; cnt++)
	{       
        memcpy(copyRight,plainBits+32,32);
        DES_E_Transform(copyRight);			// 将右半部分进行扩展置换,从32位扩展到48位
        DES_XOR(copyRight,subKeys[cnt],48);	// 将右半部分与子密钥进行异或操作 
        DES_SBOX(copyRight);				// 异或结果进入S盒,输出32位结果 
        DES_P_Transform(copyRight);			// P置换
        DES_XOR(plainBits,copyRight,32);	//将明文左半部分与右半部分进行异或 
        if(cnt != 15)
		{
            DES_Swap(plainBits,plainBits+32);//最终完成左右部的交换 
        }
    }
    DES_IP_1_Transform(plainBits);			//逆初始置换(IP^1置换)
    Bit64ToChar8(plainBits,cipherBlock);
    return 0;
}
示例#5
0
文件: qdes.cpp 项目: mildrock/dummy
/*
 *功能说明:DES算法解密函数
 *入口参数:key:密钥,长度为8字节
 *s_text:待解密的密文,长度为8字节;
 *d_text:解密得到的明文,长度为8字节。
 */
int _QDES(char *key, char *s_text, char *d_text)
{
	char plainBlock[8];
	char cipherBlock[8];
	char keyBlock[8];

	char bKey[64];
	char subKeys[16][48];
	memcpy(keyBlock, key, 8); //设置密钥
	Char8ToBit64(keyBlock, bKey); //将密钥转换为二进制流
	DES_MakeSubKeys(bKey, subKeys); //生成子密钥
	memcpy(cipherBlock, s_text, 8);

	char cipherBits[64];
	char copyRight[48];
	int cnt;
	Char8ToBit64(cipherBlock, cipherBits);
	DES_IP_Transform(cipherBits); //初始置换(IP置换)
	for (cnt = 15; cnt >= 0; cnt--) //16轮迭代
	{
		memset(copyRight, 0, 48);
		memcpy(copyRight, cipherBits + 32, 32);
		DES_E_Transform(copyRight); //将右半部分进行扩展置换,从32位扩展到48位
		DES_XOR(copyRight, subKeys[cnt], 48); //将右半部分与子密钥进行异或操作
		DES_SBOX(copyRight); //异或结果进入S盒,输出32位结果
		DES_P_Transform(copyRight); //P置换
		DES_XOR(cipherBits, copyRight, 32); //将明文左半部分与右半部分进行异或
		if (cnt != 0)
		{
			DES_Swap(cipherBits, cipherBits + 32); //最终完成左右部的交换
		}
	}
	DES_IP_1_Transform(cipherBits); //逆初始置换(IP^1置换)
	Bit64ToChar8(cipherBits, plainBlock);
	memcpy(d_text, plainBlock, 8);
	return 0;
}