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

📄 rc5cts.c

📁 rc5-cts 模式的实现源码,与RFC中的实有所不同
💻 C
字号:
#include <stdio.h>
#include <time.h>
#include <memory.h>
typedef unsigned long int WORD; /* Should be 32-bit = 4 bytes        */
typedef unsigned char BYTE;    /* Should be 8-bit = 1 bytes        */
#define w        32             /* word size in bits                 */
#define r        12             /* number of rounds                  */  
#define b        8             /* number of bytes in key            */
#define c         4             /* number  words in key = ceil(8*b/w)*/
#define t        26             /* size of table S = 2*(r+1) words   */
WORD S[t];                      /* expanded key table                */
WORD P = 0xb7e15163, Q = 0x9e3779b9;  /* magic constants             */
FILE *fr, *fw;
char dirr[100], dirw[100];
BYTE key[b];
WORD IV[2], pt[2] = {0,0}, ct[2] = {0,0}, filesize=0, blockcount=0, L=0;

/* Rotation operators. x must be unsigned, to get logical right shift*/
#define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1)))))
#define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1)))))

void RC5_ENCRYPT(WORD *pt, WORD *ct) /* 2 WORD input pt/output ct    */
{
	WORD i, A=pt[0]+S[0], B=pt[1]+S[1];
	for (i=1; i<=r; i++) 
	{
		A = ROTL(A^B,B)+S[2*i]; 
		B = ROTL(B^A,A)+S[2*i+1];
	}
	ct[0] = A; ct[1] = B;
} 

void RC5_DECRYPT(WORD *ct, WORD *pt) /* 2 WORD input ct/output pt    */
{
	WORD i, B=ct[1], A=ct[0];
	for (i=r; i>0; i--) 
	{
		B = ROTR(B-S[2*i+1],A)^A; 
		A = ROTR(A-S[2*i],B)^B;
	}
	pt[1] = B-S[1]; pt[0] = A-S[0];
}

void RC5_SETUP(unsigned char *K) /* secret input key K[0...b-1]      */
{
	WORD i, j, k, u=w/8, A, B, L[c]; 
	/* Initialize L, then S, then mix key into S */
	for (i=b-1,L[c-1]=0; i!=-1; i--) L[i/u] = (L[i/u]<<8)+K[i];
	for (S[0]=P,i=1; i<t; i++) S[i] = S[i-1]+Q;
	for (A=B=i=j=k=0; k<3*t; k++,i=(i+1)%t,j=(j+1)%c)   /* 3*t > 3*c */
	{
		A = S[i] = ROTL(S[i]+(A+B),3);
		B = L[j] = ROTL(L[j]+(A+B),(A+B));
	}
}

//Encreption start
void RC5CTS_ENCRYPT()
{
	WORD cttemp[2];
	//initialize ciphertext block
	ct[0] = IV[0];
	ct[1] = IV[1];

	while(!feof(fr))
	{
		if( ferror( fr ) )      
		{
			perror( "Read error" );
			break;
		}

		//Set the plaintext to "00000000" for padding
		memset(pt,0,8);
			
		if((blockcount--)>2)
		{
			fread(pt,c,2,fr);

			//XOR with previous ciphertext
			pt[0] = pt[0] ^ ct[0];
			pt[1] = pt[1] ^ ct[1];
			
			//Encrypt the plaintext
			RC5_ENCRYPT(pt,ct);
			fwrite(ct,c,2,fw);
		}
		else break;
	}

	//deal last two blocks
	//deal the P(N-1) block
	fread(pt,c,2,fr);
	pt[0] = pt[0] ^ ct[0];
	pt[1] = pt[1] ^ ct[1];
	RC5_ENCRYPT(pt,ct);

	//save the C(N) block as cttemp
	cttemp[0] = ct[0];
	cttemp[1] = ct[1];
		
	//deal the P(N) block		
	memset(pt,0,8);//Set the plaintext to "00000000" for padding
	fread(pt,c,2,fr);
	pt[0] = pt[0] ^ ct[0];
	pt[1] = pt[1] ^ ct[1];
	RC5_ENCRYPT(pt,ct);

	//write the C(N-1) block
	fwrite(ct,c,2,fw);
	//write the C(N) block
	fwrite(cttemp,1,L,fw);
		
	//close filehandle
	fclose(fr);
	fclose(fw);
}//Encreption end

//Decreption start
void RC5CTS_DECRYPT()
{
	BYTE* temp;
	WORD i, cttemp[2], pttemp[2];
	//initialize ciphertext block		
	ct[0] = IV[0];
	ct[1] = IV[1];

	while(!feof(fr))
	{  		
		if( ferror( fr ) )
		{
			perror( "Read error" );
			break;
		}

		//save the previous ciphertext
		cttemp[0] = ct[0];
		cttemp[1] = ct[1];

		if((blockcount--)>2)
		{
			fread(ct,c,2,fr);
			RC5_DECRYPT(ct,pt);

			//XOR with previous ciphertext
			pt[0] = pt[0] ^ cttemp[0];
			pt[1] = pt[1] ^ cttemp[1];
			fwrite(pt,c,2,fw);
		}
		else break;
	}

	//deal last two blocks
	//deal the C(N-1) block
	fread(ct,c,2,fr);
	RC5_DECRYPT(ct,pttemp);
	//dealthe C(N) block
	memset(ct,0,8);
	fread(ct,c,2,fr);
	pt[0] = ct[0] ^ pttemp[0];
	pt[1] = ct[1] ^ pttemp[1];
	//save P(N)+0 block as pt
	temp = (BYTE*)pt;
	for(i=L;i<2*c;i++)
	{		
		temp[i]=0;		
	}

	//save C(N)+X block as ct
	ct[0] = pt[0] ^ pttemp[0];
	ct[1] = pt[1] ^ pttemp[1];
	RC5_DECRYPT(ct,pttemp);

	//save P(N-1) block as pttemp
	pttemp[0] = pttemp[0] ^ cttemp[0];
	pttemp[1] = pttemp[1] ^ cttemp[1];

	//write the P(N-1) block
	fwrite(pttemp,c,2,fw);
	//write the P(N) block
	fwrite(pt,1,L,fw);

	//close filehandle
	fclose(fr);
	fclose(fw);
}//Decreption end

//void RC5CTS_START()
void main()
{
	WORD i;
	BYTE* temp;
	char choice;

	printf("Please Input your 8 BYTES Key \n");
	for(i=0;i<b;i++)
	{
		scanf("%c",&key[i]);
		if(key[i]=='\n')i--;
	}
	printf("key = "); 
	for (i=0; i<b; i++) printf("%c",key[i]);
	printf("\n");
          
	RC5_SETUP(key); 

	temp = (BYTE*)IV;
	printf("Please Input your 8 BYTES IV \n");
	for(i=0;i<2*c;i++)
	{
		scanf("%c",&temp[i]);
		if(temp[i]=='\n')i--;
	}
	printf("IV = "); 
	printf("%s",IV);
	printf("\n");
	
	printf("Please Input the DIR of file that to be deal : \n");
	scanf("%s", dirr);
      
	if( (fr=fopen(dirr,"rb"))==NULL)
	{
		printf("Can't open file, Please Check the dir .\n");
		return;
	}
	else
	{
		fseek(fr,0L,SEEK_END);
		filesize = ftell(fr);
		L = filesize%(2*c);
		blockcount = filesize/(2*c) + (L>0);
		if(L==0)
			L = 2*c;
		rewind(fr);
	}

	printf("Please Input the DIR of file that you convert %s to(Encryption): \n",dirr);
	scanf("%s", dirw);

	if((fw=fopen(dirw,"wb"))==NULL )
	{
		printf("Can't creat %s Sorry !\n",dirw);
		return;
	}

//	printf("Encryption press 'E'  and Decryption press 'D' \n");     // Chose to Encryption or Decryption
//	while(scanf("%c",&choice))
//	{
		//if(choice!='\n') break;
//	}

//	if(choice=='E'||choice=='e')
		RC5CTS_ENCRYPT();
//if(choice=='D'||choice=='d')
printf("Please Input the DIR of file that to be deal : \n");
	scanf("%s", dirr);
      
	if( (fr=fopen(dirr,"rb"))==NULL)
	{
		printf("Can't open file, Please Check the dir .\n");
		return;
	}
	else
	{
		fseek(fr,0L,SEEK_END);
		filesize = ftell(fr);
		L = filesize%(2*c);
		blockcount = filesize/(2*c) + (L>0);
		if(L==0)
			L = 2*c;
		rewind(fr);
	}
	printf("Please Input the DIR of file that you convert %s to(Decryption): \n",dirr);
	scanf("%s", dirw);

	if((fw=fopen(dirw,"wb"))==NULL )
	{
		printf("Can't creat %s Sorry !\n",dirw);
		return;
	}

	RC5CTS_DECRYPT();
}

//void main()
//{
//	RC5CTS_START();
//}

⌨️ 快捷键说明

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