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

📄 dhdemo.c

📁 私家珍藏的RSA Laborary有关加密的函数库
💻 C
📖 第 1 页 / 共 2 页
字号:
      (&PARAMS2, primeBits, subPrimeBits, randomStruct)) {
    PrintError ("generating parameters", status);
    return;
  }

  PrintMessage ("Parameters 2 are now ready to use.");
  PARAMS2_READY = 1;
  
  WriteParams2 ();
}

static void WriteParams2 ()
{
  FILE *file;
  char filename[256];
  
  while (1) {
    GetCommand
      (filename, sizeof (filename),
       "Enter filename to save the parameters (blank to not save): ");
    if (! *filename)
      return;
    
    if (filename[0] == '-' && filename[1] == '\0') {
      /* use stdout */
      file = stdout;
      break;
    }
    if ((file = fopen (filename, "w")) != NULL)
      /* successfully opened */
      break;
    
    PrintError ("ERROR: Cannot open a file with that name.  Try again.", 0);
  }

  fprintf (file, "Parameters:\n");
  fprintf (file, "  prime: ");
  WriteBigInteger (file, PARAMS2.prime, PARAMS2.primeLen);
  fprintf (file, "  generator: ");
  WriteBigInteger (file, PARAMS2.generator, PARAMS2.generatorLen);

  if (file != stdout)
    fclose (file);
}

/* Write the byte string 'integer' to 'file', skipping over leading zeros.
 */
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");
}

/* Use the prompt to ask the user to use parameters 1 or 2 and
     point params to the answer.
   Return 0 on success or 1 if user cancels by entering a blank.
 */
static int GetParams (params, prompt)
R_DH_PARAMS **params;
char *prompt;
{
  char command[80];
  
  while (1) {
    GetCommand (command, sizeof (command), prompt);

    switch (*command) {
    case '\0':
      return (1);
      
    case '1':
      *params = &PARAMS1;
      return (0);
      
    case '2':
      if (!PARAMS2_READY) {
        PrintError
          ("ERROR: Parameters 2 have not been generated yet.  Try Again.", 0);
        break;
      }
      else {
        *params = &PARAMS2;
        return (0);
      }
      
    default:
      if (PARAMS2_READY)
        PrintError ("ERROR: Please enter 1 or 2.  Try again.", 0);
      else
        PrintError ("ERROR: Please enter 1.  Try again.", 0);
      break;
    }
  }
}

/* Read a file of up to length maxBlockLen bytes, storing it in
     block and returning its length in blockLen.
   Ask for the filename using the given prompt string.
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int ReadBlock (block, blockLen, maxBlockLen, prompt) 
unsigned char *block;
unsigned int *blockLen;
unsigned int maxBlockLen;
char *prompt;
{
  FILE *file;
  int status;
  char filename[256];
  unsigned char dummy;
  
  while (1) {
    GetCommand (filename, sizeof (filename), prompt);
    if (! *filename)
      return (1);
    
    if ((file = fopen (filename, "rb")) != NULL)
      /* successfully opened */
      break;
    
    PrintError ("ERROR: Cannot open a file with that name.  Try again.", 0);
  }
  
  /* fread () returns the number of items read in.  Expect an end of file
       after the read.
   */
  *blockLen = fread (block, 1, maxBlockLen, file);
  if (*blockLen == maxBlockLen)
    /* Read exactly maxBlockLen bytes, so reading one more will set 
         end of file if there were exactly maxBlockLen bytes in the file.
     */
    fread (&dummy, 1, 1, file);
  
  if (!feof (file)) {
    PrintError ("ERROR: Cannot read file or file is too large.", 0);
    status = 1;
  }
  else
    status = 0;
  
  fclose (file);
  return (status);
}

/* Write block oflength blockLen to a file.
   Ask for the filename using the given prompt string.
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int WriteBlock (block, blockLen, prompt) 
unsigned char *block;
unsigned int blockLen;
char *prompt;
{
  FILE *file;
  int status;
  char filename[256];
  
  while (1) {
    GetCommand (filename, sizeof (filename), prompt);
    if (! *filename)
      return (1);
    
    if (filename[0] == '-' && filename[1] == '\0') {
      /* use stdout */
      file = stdout;
      break;
    }
    if ((file = fopen (filename, "wb")) != NULL)
      /* successfully opened */
      break;
    
    PrintError ("ERROR: Cannot open a file with that name.  Try again.", 0);
  }
  
  status = 0;
  if (fwrite (block, 1, blockLen, file) < blockLen) {
    PrintError ("ERROR: Cannot write file.", 0);
    status = 1;
  }
  else {
    if (file == stdout)
      /* Printing to screen, so print a new line. */
      printf ("\n");
  }

  if (file != stdout)
    fclose (file);
  return (status);
}

static void PrintMessage (message)
char *message;
{
  if (!SILENT_PROMPT) {
    puts (message);
    fflush (stdout);
  }
}

/* If type is zero, simply print the task string, otherwise convert the
     type to a string and print task and type.
 */
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_CONTENT_ENCODING:
    typeString = "(Encrypted) content has RFC 1113 encoding error";
    break;
  case RE_DIGEST_ALGORITHM:
    typeString = "Message-digest algorithm is invalid";
    break;
  case RE_KEY:
    typeString = "Recovered DES key cannot decrypt encrypted content or encrypt signature";
    break;
  case RE_KEY_ENCODING:
    typeString = "Encrypted key has RFC 1113 encoding error";
    break;
  case RE_MODULUS_LEN:
    typeString = "Modulus length is invalid";
    break;
  case RE_NEED_RANDOM:
    typeString = "Random structure is not seeded";
    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 DES key, or cannot decrypt signature";
    break;
  case RE_SIGNATURE:
    typeString = "Signature on content or block is incorrect";
    break;
  case RE_SIGNATURE_ENCODING:
    typeString = "(Encrypted) signature has RFC 1113 encoding error";
    break;
    
  default:
    sprintf (buf, "Code 0x%04x", type);
    typeString = buf;
  }

  printf ("ERROR: %s while %s\n", typeString, task);  
  fflush (stdout);
}

static void GetCommand (command, maxCommandSize, prompt)
char *command;
unsigned int maxCommandSize;
char *prompt;
{
  unsigned int i;
  
  if (!SILENT_PROMPT) {
    printf ("%s\n", prompt);  
    fflush (stdout);
  }

  fgets (command, maxCommandSize, stdin);
  
  /* Replace the line terminator with a '\0'.
   */
  for (i = 0; command[i] != '\0'; i++) {
    if (command[i] == '\012' || command[i] == '\015' ||
        i == (maxCommandSize - 1)) {
      command[i] = '\0';
      return;
    }
  }
}

⌨️ 快捷键说明

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