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

📄 testdes.c

📁 一个测试DES加密算法和测试程序
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "rsaref.h"


static void InitRandomStruct(R_RANDOM_STRUCT *);
static int GetKey (char *,R_RSA_PRIVATE_KEY *,R_RSA_PUBLIC_KEY *);
static void PrintError(char *, int);
static void WriteBigInteger(FILE *, unsigned char *, unsigned int);
static void encryption(char *,char *,R_RANDOM_STRUCT *);
static void unencryption();



//////////////////////var/////////////////////////

//static R_RSA_PUBLIC_KEY  Publickey;
//static R_RSA_PRIVATE_KEY Privatekey;
//static R_RANDOM_STRUCT randomStruct;

static R_RSA_PUBLIC_KEY  *Publickey;
static R_RSA_PRIVATE_KEY *Privatekey;

R_RANDOM_STRUCT randomStruct;



int main(void )
{
  char filename[127];
  char infilename1[127];
  char outfilename2[127];

  Publickey = (R_RSA_PUBLIC_KEY *)malloc(sizeof(R_RSA_PUBLIC_KEY));
  Privatekey = (R_RSA_PRIVATE_KEY *)malloc(sizeof(R_RSA_PRIVATE_KEY));

  memset(filename,0,127);  

  InitRandomStruct(&randomStruct);
  
  printf("plase enter a file name for save key!\n");
  scanf("%s",filename);
  
  if(!GetKey(filename,Privatekey,Publickey))
	  printf("get key failed!\n\n");
  
  printf("please enter the file name for need encrypted file!\n");
  
  scanf("%s",infilename1);

  printf("please enter the file for save \n");

  scanf("%s",outfilename2);
  
  encryption(infilename1,outfilename2,&randomStruct);



  unencryption();

  free(Publickey);
  free(Privatekey);

  return 0;
}

static int GetKey(fn,Private_key,Public_key)
char * fn;
R_RSA_PRIVATE_KEY* Private_key;
R_RSA_PUBLIC_KEY* Public_key;
{
  R_RSA_PUBLIC_KEY PUBLIC_KEY3;
  R_RSA_PRIVATE_KEY PRIVATE_KEY3;
  R_RSA_PROTO_KEY protoKey;
  int KEYPAIR3_READY = 0;

  int status=0;
  int i=0;
  int len;

  FILE *pfprivate,*pfpublic;
  char fnprivate[255],fnpublic[255];
  unsigned char *date=NULL;
  strcpy(fnprivate,fn);
  strcpy(fnpublic,fn);
  
  strcat(fnpublic,"public.bin");
  strcat(fnprivate,"private.bin");
  
  //创建文件公钥文件
  if((pfpublic = fopen(fnpublic,"wb"))==NULL)
  {
	  printf("create public key file aborted!\n");
	  return 0;
  }
  //创建私钥文件;

  if((pfprivate = fopen(fnprivate,"wb")) == NULL)
  {
	  printf("create private key file aborted !\n");

	  fclose(pfpublic);

	  return 0;
  }

 

 
  protoKey.bits = 512;
  protoKey.useFermat4 = 1;

  if (status = R_GeneratePEMKeys(&PUBLIC_KEY3, &PRIVATE_KEY3, &protoKey,&randomStruct)) {
    PrintError("generating keys", status);
    return 0;
  }

  memcpy(Public_key,&PUBLIC_KEY3,sizeof(PUBLIC_KEY3));

  memcpy(Private_key,&PRIVATE_KEY3,sizeof(PRIVATE_KEY3));


//  KEYPAIR3_READY = 1;

  fprintf(pfpublic, "Public Key, %u bits:\n", PUBLIC_KEY3.bits);
  fprintf(pfpublic, "  modulus: ");

  date = PUBLIC_KEY3.modulus;

  for(i=0; i< (len = sizeof(PUBLIC_KEY3.modulus)) ;i++,date++)
  {
      fprintf(pfpublic," 0x%02x",*date);
  }


 // WriteBigInteger(pfpublic,PUBLIC_KEY3.modulus,sizeof(PUBLIC_KEY3.modulus));
  fprintf(pfpublic,"\n");  
  fprintf(pfpublic, "  exponent: ");
  date=PUBLIC_KEY3.exponent;

  for(i=0;i<sizeof(PUBLIC_KEY3.exponent);i++,date++)
  {
	  fprintf(pfpublic," 0x%02x",*date);
  }

//  WriteBigInteger(pfpublic,PUBLIC_KEY3.exponent,sizeof(PUBLIC_KEY3.exponent));
  fprintf(pfpublic,"\n");  

  fprintf (pfprivate, "private Key, %u bits:\n", PRIVATE_KEY3.bits);
  fprintf (pfprivate, "  modulus: ");
  
  date = PRIVATE_KEY3.modulus;

  for(i=0;i<sizeof(PRIVATE_KEY3.modulus);i++,date++)
  {
	  fprintf(pfprivate," 0x%02x",*date);
  }
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.modulus,sizeof(PRIVATE_KEY3.modulus));

   fprintf(pfprivate, "public exponent: ");

  date=PRIVATE_KEY3.publicExponent;

  for(i=0;i<sizeof(PRIVATE_KEY3.publicExponent);i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }
  fprintf(pfprivate,"\n");
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.publicExponent,sizeof(PRIVATE_KEY3.publicExponent));
  
  date=PRIVATE_KEY3.exponent;

  fprintf(pfprivate, "exponent: ");
  for(i=0;i<sizeof(PRIVATE_KEY3.exponent);i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
 }

  fprintf(pfprivate,"\n");
   //WriteBigInteger(pfprivate,PRIVATE_KEY3.exponent,sizeof(PRIVATE_KEY3.exponent));

  date=PRIVATE_KEY3.prime[0];

  fprintf(pfprivate, "prime 1: ");
  for(i=0;i<sizeof(PRIVATE_KEY3.prime[0]);i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }

  fprintf(pfprivate,"\n");  
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.prime[0],sizeof(PRIVATE_KEY3.prime[0]));

  date=PRIVATE_KEY3.prime[1];

  fprintf(pfprivate, "prime 2: ");
  for(i=0;i<sizeof(PRIVATE_KEY3.prime[1]);i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.prime[1],sizeof(PRIVATE_KEY3.prime[1]));
  
  fprintf(pfprivate,"\n");

  date=PRIVATE_KEY3.primeExponent[0];

  fprintf(pfprivate, "primeExponent 1: ");
  for(i=0;i<sizeof(PRIVATE_KEY3.primeExponent[0]);i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.primeExponent[0],sizeof(PRIVATE_KEY3.primeExponent[0]));
  
  fprintf(pfprivate,"\n");

  date=PRIVATE_KEY3.primeExponent[1];

  fprintf(pfprivate, "primeExponent 2: ");
  for(i=0; i < sizeof(PRIVATE_KEY3.primeExponent[1]); i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.primeExponent[1],sizeof(PRIVATE_KEY3.primeExponent[1]));
  
  fprintf(pfprivate,"\n");

  date=PRIVATE_KEY3.coefficient;

  fprintf(pfprivate,"coefficient : ");
  for(i=0; i < sizeof(PRIVATE_KEY3.coefficient); i++,date++)
  {

	  fprintf(pfprivate," 0x%02x",*date);
  }

  fprintf(pfprivate,"\n");
//   WriteBigInteger(pfprivate,PRIVATE_KEY3.coefficient,sizeof(PRIVATE_KEY3.coefficient));

  fclose(pfpublic);
  fclose(pfprivate);

  return 1;

}

static void InitRandomStruct(randomStruct)
R_RANDOM_STRUCT *randomStruct;
{
  static unsigned char seedByte = 0;
  unsigned int bytesNeeded;
  
  R_RandomInit (randomStruct);
  
  /* Initialize with all zero seed bytes, which will not yield an actual
       random number output.
   */
  while (1) {
    R_GetRandomBytesNeeded (&bytesNeeded, randomStruct);
    if (bytesNeeded == 0)
      break;
    
    R_RandomUpdate (randomStruct,&seedByte,1);
  }
}

static void PrintError (task, type)
char *task;
int type;
{
  char *typeString, buf[80];

  if (type == 0) {
    puts (task);
    return;
  }
  
  /* Convert the type to a string if it is recognized.
   */
  switch (type) {
  case RE_KEY:
    typeString = "Recovered DES key cannot decrypt encrypted content";
    break;
  case RE_LEN:
    typeString = "Encrypted key length or signature length is out of range";
    break;
  case RE_MODULUS_LEN:
    typeString = "Modulus length is out of range";
    break;
  case RE_PRIVATE_KEY:
    typeString = "Private key cannot encrypt message digest, or cannot decrypt encrypted key";
    break;
  case RE_PUBLIC_KEY:
    typeString = "Public key cannot encrypt data encryption key, or cannot decrypt signature";
    break;
  case RE_SIGNATURE:
    typeString = "Signature is incorrect";
    break;
    
  default:
    sprintf (buf, "Code 0x%04x", type);
    typeString = buf;
  }

  printf ("ERROR: %s while %s\n", typeString, task);  
  fflush (stdout);
}
static void WriteBigInteger (file, integer, integerLen)
FILE *file;
unsigned char *integer;
unsigned int integerLen;
{
  while (*integer == 0 && integerLen > 0) {
    integer++;
    integerLen--;
  }
  
  if (integerLen == 0) {
    /* Special case, just print a zero. */
    fprintf (file, "00\n");
    return;
  }
  
  for (; integerLen > 0; integerLen--)
    fprintf (file, "%02x ", (unsigned int)(*integer++));

  fprintf (file, "\n");
}
//加密
static void encryption(infn,outfn,randomStruct)
char *infn;
char *outfn;
R_RANDOM_STRUCT *randomStruct;
{
	
	FILE *pfIn,*pfOut,*ecpkey,*inivec;
    
   R_ENVELOPE_CTX context;
   R_RSA_PUBLIC_KEY *publicKey;
   int encryptionAlgorithm;
   unsigned char encryptedKey[MAX_ENCRYPTED_KEY_LEN], *encryptedKeys[1],
    iv[8], partIn[24], partOut[31];

   unsigned int encryptedKeyLen, partInLen, partOutLen;
   char encrypfn[127];
   char inivecfn[127];

   encryptionAlgorithm = EA_DES_CBC;
   publicKey = Publickey;
   encryptedKeys[0] = encryptedKey;

	if((pfIn=fopen(infn,"rb"))==NULL)
	{
		printf("open thi infile failed!");
	
		return ;
	}

	if((pfOut = fopen(outfn,"wb")) ==NULL)
	{
		printf("open this outfile failed!");
		
		fclose(pfIn);

        return;
	}

	
    if(R_SealInit(&context,encryptedKeys,&encryptedKeyLen,iv,1,
		           &publicKey,encryptionAlgorithm,randomStruct))
	{	
		fclose(pfIn);
		fclose(pfOut);
		return ;
	}


	while(!feof(pfIn))
	{
		partInLen=fread(partIn,1,sizeof(partIn),pfIn);
        if(R_SealUpdate(&context,partOut,&partOutLen,partIn,partInLen))
			return;
		fwrite(partOut,1,partOutLen,pfOut);
	}

	if(R_SealFinal(&context,partOut,&partOutLen))
  		return;

	fwrite(partOut,1,partOutLen,pfOut);

	fclose(pfIn);
	fclose(pfOut);
    do
	{
	   printf("please enter filename for save encrypted key!\n");
	   scanf("%s",encrypfn);
	   printf("please enter filename for initializing vector\n");
	   scanf("%s",inivecfn);

    }while((strlen(encrypfn) == 0) || (strlen(inivecfn) == 0));

	if((ecpkey=fopen(encrypfn,"wb")) ==NULL)
	{
		printf("open encryped file failed!");

		return;
	}
    
	if((inivec=fopen(inivecfn,"wb"))==NULL)
	{
		printf("open ini vector file failed!");
        fclose(ecpkey);
		return ;
	}

    fwrite(encryptedKey,1,encryptedKeyLen,ecpkey);
	fwrite(iv,1,8,inivec);

	fclose(ecpkey);
	fclose(inivec);

    R_memset ((POINTER)&context, 0, sizeof (context));
    R_memset ((POINTER)partIn, 0, sizeof (partIn));


}
static void unencryption()
{
	FILE *pfIn,*pfOut,*pfKey,*pfVec;

    R_ENVELOPE_CTX context;
    R_RSA_PRIVATE_KEY *privateKey;
    int encryptionAlgorithm, status;
    unsigned char encryptedKey[MAX_ENCRYPTED_KEY_LEN], iv[8], partIn[24],
    partOut[31];
    unsigned int encryptedKeyLen, ivLen, partInLen, partOutLen;
	int ret;

	char ecyfile[127];
	char uncfyfile[127];
	char UnKeyfile[127];
	char Vecfile[127];

    encryptionAlgorithm = EA_DES_CBC;
	privateKey = Privatekey;

	printf("please enter the encrypted file to open!\n");
	scanf("%s",ecyfile);
	printf("please enter the filename file for save decrypted!\n");
	scanf("%s",uncfyfile);
	printf("please enter the key file to open!\n");
	scanf("%s",UnKeyfile);
	printf("please enter the vector file to open!\n");
	scanf("%s",Vecfile);

	if((pfIn = fopen(ecyfile,"rb")) == NULL)
	{
		printf("open encrypted file failed!");

		return ;

	}
	if((pfOut = fopen(uncfyfile,"wb")) == NULL)
	{
		printf("create decrypted file failed!\n");
   
		fclose(pfIn);
		return ;

	}

	if((pfKey = fopen(UnKeyfile,"rb")) == NULL)
	{
		printf("open key file failed!\n");
   
		fclose(pfIn);
		fclose(pfOut);
		return ;

	}
	if((pfVec = fopen(Vecfile,"rb")) == NULL)
	{
		printf("open Vector file failed!\n");
   
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		return ;

	}
    
    if((encryptedKeyLen=fread(encryptedKey,1,sizeof(encryptedKey),pfKey)) ==0)
	{
		
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		fclose(pfVec);
		return ;

	}
	if((ivLen=fread(iv,1,sizeof(iv),pfVec)) == 0)
	{
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		fclose(pfVec);
        return ;

	}

    if ((ret=R_OpenInit(&context, encryptionAlgorithm, encryptedKey, encryptedKeyLen, iv,
          privateKey)) != 0)
	{
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		fclose(pfVec);
        return ;
	}

	while(!feof(pfIn))
	{
		partInLen = fread(partIn,1,sizeof(partIn),pfIn);

		if(R_OpenUpdate(&context, partOut, &partOutLen, partIn, partInLen))
		{
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		fclose(pfVec);
        return ;
		}
		fwrite(partOut,1,partOutLen,pfOut);

	}
    if ((status = R_OpenFinal (&context, partOut, &partOutLen)) != 0)
	{
		fclose(pfIn);
		fclose(pfOut);
		fclose(pfKey);
		fclose(pfVec);
        return ;
	
	}
	fwrite(partOut,1,partOutLen,pfOut);
    R_memset ((POINTER)&context, 0, sizeof (context));
    R_memset ((POINTER)partOut, 0, sizeof (partOut));

	fclose(pfIn);
	fclose(pfOut);
	fclose(pfKey);
	fclose(pfVec);
   
}

⌨️ 快捷键说明

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