⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc5_cts.h

📁 RC5-CTS模式加密解密算法C++程序实现。 解压之后运行main.dsw.在VC++下面打开
💻 H
字号:
   /* RC5_CTS encryption and decryption*/
/*Created by erjianChai 2005-04-10*/
 
#include "RC5.h"
WORD IV[2]={0x123689DA,0x23D4F3C8};
WORD Ct[2] = {0,0};
WORD Pt[2] = {0,0};
void RC5_CTSEN(char* plaintext, int length)
{
 int numOfBlock = (length)/8; // get the numOfBlocks
 int restOfBytes = (length)%8;//get the rest bytes of the plaintext

 //initia the IV to Ct 
 Ct[0] = IV[0];
 Ct[1] = IV[1];
 for(int num=0; num < length - restOfBytes; num+=8)
	{
	  memcpy(Pt, &plaintext[num], 8);
	  Pt[0] ^= Ct[0];
	  Pt[1] ^= Ct[1];
	  RC5_ENCRYPT(Pt, Ct);		
	  memcpy(&plaintext[num], Ct, 8);		
	}
	Pt[0] = Ct[0];
	Pt[1] = Ct[1];

	RC5_ENCRYPT(Pt, Ct);
	char *temp;
	temp = new char[restOfBytes];
	
	char *temp1;
	temp1 = new char[restOfBytes];
	memcpy(temp, Ct, restOfBytes);

	memcpy(temp1, &plaintext[num], restOfBytes);
	for (int j = 0; j<restOfBytes; j++)
	{
		temp1[j] ^= temp[j];
	}	
	memcpy(&plaintext[num], temp1, restOfBytes);
		
	
}


void RC5_CTSDE(char *ciphertext, int length)
{
  int numOfBlock = (length)/8; // get the numOfBlocks
  int restOfBytes = (length)%8;//get the rest bytes of the plaintext	
   
        Ct[0] = 0;
	Ct[1] = 0;
	Pt[0] = 0;
	Pt[1] = 0;
        char *temp, *temp1;
	temp = new char[restOfBytes];
	temp1 = new char[restOfBytes];
	
	//decrypt the last two blocks

	memcpy(temp, &ciphertext[8*numOfBlock], restOfBytes);
	memcpy(Ct, &ciphertext[8*(numOfBlock-1)], 8);
	Pt[0] = Ct[0];
	Pt[1] = Ct[1];

	RC5_ENCRYPT(Pt, Ct);
	memcpy(temp1, Ct, restOfBytes);
	
	for ( int num = 0; num < restOfBytes; num++)
	{
		temp[num] ^= temp1[num];
	}
	
	memcpy(&ciphertext[length - restOfBytes], temp, restOfBytes);
	
	
	//decrypt the front blocks 
	WORD ctTemp[2] = {0,0};
	
		
	for(int j =8*(numOfBlock-1) ; j >= 0; j -= 8)
	{
		memcpy(Ct, &ciphertext[j], 8); // c_n-1
		if (j <= 0)
		{
			ctTemp[0] = IV[0];
			ctTemp[1] = IV[1];
		}
		else
			memcpy(ctTemp, &ciphertext[j - 8], 8); // c_n-2
		
		RC5_DECRYPT(Ct, Pt);
		
	
		Pt[0] ^= ctTemp[0];
		Pt[1] ^= ctTemp[1];
		memcpy(&ciphertext[j], Pt, 8);	
	}

	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -