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

📄 rc5cbc.c

📁 rc5-72在cbc模式下的加密和解密程序
💻 C
字号:
/* RC5REF.C -- Reference implementation of RC5-32/12/16 in C.        *//* Copyright (C) 1995 RSA Data Security, Inc.                        */#include <stdio.h>#include <time.h>typedef unsigned long int WORD; /* Should be 32-bit = 4 bytes        */#define w        32             /* word size in bits                 */#define r        12             /* number of rounds                  */  #define b        16             /* 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             *//* 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 ;
  WORD A=pt[0]+S[0] ;
  WORD 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;
  WORD B=ct[1];
  WORD 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));      } } void main(){ int i, j;
 WORD  pt1[2], pt2[2], ct[2] = {0,0};  unsigned char key[b];  WORD IV[2];
  time_t t0, t1;  if (sizeof(WORD)!=4)      printf("RC5 error: WORD has %d bytes.\n",sizeof(WORD));  printf("RC5-32/12/16 examples:\n");
  //printf(" input the initial vector:");
   IV[0]=0X11111111;
   IV[1]=0X11111111;  
 printf("This is a cbc encryting mode");
 printf("\n\nThe initial vector is 0x%x 0x%x",IV[0],IV[1]);
 
 printf("\n\nplease input the plaintext:");
   for(j=0;j<2;j++)
	 scanf("%X",&pt1[j]);
   for(j=0;j<2;j++)
	   pt1[j]=pt1[j]^IV[j];
   
  printf("\nplease input the key :"); 
  for(i=0;i<b;i++)
  scanf("%X",&key[i]);
   
   RC5_SETUP(key);  
   RC5_ENCRYPT(pt1,ct); 
   printf("\nThe result ciphertext:0x%x 0x%x",ct[0],ct[1]);
   
   RC5_DECRYPT(ct,pt2);
   for(i=0;i<2;i++)
	   pt2[i]=pt2[i]^IV[i];
   printf("\nthe result pt2 is 0x%x 0x%x",pt2[0],pt2[1]);


 time (&t0);  for (i=1;i<1000000;i++)       RC5_ENCRYPT(ct,ct);  time (&t1);  printf ("\n\n   Time_t for 100000 blocks:  %ld \n", t1-t0);}

//key= 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 23
//plaintext=0x33333333 0x44444444.....ciphertext=0xedb7b55a 0x572af09a



⌨️ 快捷键说明

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