?? redemo.c
字號:
/* use stdout */ return; } if((file = fopen (filename, "rb")) != NULL) /* successfully opened */ break; PrintError("ERROR: Cannot open a file with that name. Try again.", 0); } if((fread(&PUBLIC_KEY2, sizeof(PUBLIC_KEY2), 1, file)) != 1) { PrintMessage("ERROR: Cannot Read Public Key from File."); } else { if((fread(&PRIVATE_KEY2, sizeof(PRIVATE_KEY2), 1, file)) != 1) PrintMessage("ERROR: Cannot Read Private Key from File."); } PrintMessage(" Public key 2 and private key 2 are now ready to use."); KEYPAIR2_READY = 1; ReadClose(file);}static void WriteKeypair2(void){ FILE *file; char filename[256]; while(1) { if(!GetCommand(filename, sizeof (filename), " Enter filename to save the keypair")) return; if (filename[0] == '-' && filename[1] == '\0') { /* use stdout */ return; } if((file = fopen (filename, "wb")) != NULL) /* successfully opened */ break; PrintError("ERROR: Cannot open a file with that name. Try again.", 0); } if((fwrite(&PUBLIC_KEY2, sizeof(PUBLIC_KEY2), 1, file)) != 1) { PrintMessage("ERROR: Cannot Write Public Key to File."); } else { if((fwrite(&PRIVATE_KEY2, sizeof(PRIVATE_KEY2), 1, file)) != 1) PrintMessage("ERROR: Cannot Write Private Key to File."); } WriteClose(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");}/* Ask the user to use public key 1, 2 or 3 and point publicKey to the answer. Return 0 on success or 1 if user cancels by entering a blank. */static int GetPublicKey (publicKey)R_RSA_PUBLIC_KEY **publicKey;{ char command[80]; while (1) { if(KEYPAIR2_READY) GetCommand(command, sizeof (command), " Public key 1 or 2?"); else *command = '1'; switch (*command) { case '\0': return (0); case '1': *publicKey = &PUBLIC_KEY1; return (1); case '2': if (!KEYPAIR2_READY) break; *publicKey = &PUBLIC_KEY2; return (1); default: if (KEYPAIR2_READY) PrintError ("ERROR: Please enter 1 or 2. Try again.", 0); break; } }}/* Ask the user to use private key 1, 2 or 3 and point privateKey to the answer. Return 0 on success or 1 if user cancels by entering a blank. */static int GetPrivateKey(privateKey)R_RSA_PRIVATE_KEY **privateKey;{ char command[80]; while (1) { if (KEYPAIR2_READY) GetCommand(command, sizeof (command), " Public key 1 or 2?"); else *command = '1'; switch (*command) { case '\0': return (0); case '1': *privateKey = &PRIVATE_KEY1; return (1); case '2': if (!KEYPAIR2_READY) break; *privateKey = &PRIVATE_KEY2; return (1); default: if (KEYPAIR2_READY) PrintError ("ERROR: Please enter 1 or 2. Try again.", 0); break; } }}/* Ask the user to use MD2 or MD5 and point digestAlgorithm to the answer. Return 0 on success or 1 if user cancels by entering a blank. */static int GetDigestAlgorithm (digestAlgorithm)int *digestAlgorithm;{ char command[80]; while (1) { GetCommand (command, sizeof (command), " MD2 or MD5 (2 or 5)?"); switch (*command) { case '\0': return (0); case '2': *digestAlgorithm = DA_MD2; return (1); case '5': *digestAlgorithm = DA_MD5; return (1); default: PrintError ("ERROR: Please enter 2 or 5. Try again.", 0); break; } }}/* Ask the user to use DES, DESX, DES-EDE2, or DES-EDE3, and point encryptionAlgorithm to the answer. Return 0 on success or 1 if user cancels by entering a blank. */static int GetEncryptionAlgorithm (encryptionAlgorithm)int *encryptionAlgorithm;{ char command[80]; while (1) { GetCommand(command, sizeof (command), " DES, DESX, DES-EDE2 or DES-EDE3 (1, X, 2 or 3)?"); upr(command); switch(*command) { case '\0': return (0); case '1': *encryptionAlgorithm = EA_DES_CBC; return (1); case 'X': *encryptionAlgorithm = EA_DESX_CBC; return (1); case '2': *encryptionAlgorithm = EA_DES_EDE2_CBC; return (1); case '3': *encryptionAlgorithm = EA_DES_EDE3_CBC; return (1); default: PrintError ("ERROR: Please enter 1, X, 2 or 3. Please Try again.", 0); break; } }}/* Ask for the filename using the given prompt string and open it for reading. Return 0 on success or 1 if error or if user cancels by entering a blank. */static int ReadInit (file, prompt)FILE **file;char *prompt;{ char filename[256]; while (1) { if(!GetCommand (filename, sizeof (filename), prompt)) return (1); if ((*file = fopen (filename, "rb")) != NULL) /* successfully opened */ break; PrintError ("ERROR: Cannot open a file with that name. Try again.", 0); } return (0);}/* Read a block of up to length maxPartOutLen bytes from file, storing it in partOut and returning its length in partOutLen. Return 0 on success or 1 if error or end of file. */static int ReadUpdate (file, partOut, partOutLen, maxPartOutLen)FILE *file;unsigned char *partOut;unsigned int *partOutLen;unsigned int maxPartOutLen;{ int status; /* fread () returns the number of items read in. */ *partOutLen = fread (partOut, 1, maxPartOutLen, file); status = 0; if (ferror (file)) { PrintError ("ERROR: Cannot read file.", 0); status = 1; } if (*partOutLen == 0 && feof (file)) status = 1; return (status);}/* 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; unsigned char *dummy; unsigned int dummyLen; if (ReadInit (&file, prompt)) return (1); if ((status = ReadUpdate (file, block, blockLen, maxBlockLen)) == 0) { 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. */ if (!ReadUpdate (file, dummy, &dummyLen, 1)) { PrintError ("ERROR: File is too large.", 0); status = 1; } } ReadClose(file); return (status);}/* Ask for the filename using the given prompt string and open it for writing. Return 0 on success or 1 if error or if user cancels by entering a blank.*/static int WriteInit (file, prompt)FILE **file;char *prompt;{ char filename[256]; while (1) { if(!GetCommand (filename, sizeof (filename), prompt)) 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); } return(0);}/* Write block of length partOutLen to a file. Return 0 on success or 1 if error. */static int WriteUpdate(file, partOut, partOutLen)FILE *file;unsigned char *partOut;unsigned int partOutLen;{ int status; status = 0; if(fwrite(partOut, 1, partOutLen, file) < partOutLen) { PrintError("ERROR: Cannot write file.", 0); status = 1; } return(status);}/* Write block of length 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; if(WriteInit(&file, prompt)) return (1); do{ if((status = WriteUpdate (file, block, blockLen)) != 0) break; if(file == stdout) /* Printing to screen, so print a new line. */ printf("\n"); }while(0); WriteClose(file); return(status);}/* 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, *msg[] = { "Recovered DES key cannot decrypt encrypted content", "Encrypted key length or signature length is out of range", "Modulus length is out of range", "Private key cannot encrypt message digest, or cannot decrypt encrypted key", "Public key cannot encrypt data encryption key, or cannot decrypt signature", "Signature is incorrect", "Unknown Error", NULL }; if(type == 0) { /* Non RSAEURO Related Error */ puts(task); /* Internal Deal with it */ return; } /* Convert the type to a string if it is recognized. */ switch(type) { case RE_KEY: typeString = msg[0]; break; case RE_LEN: typeString = msg[1]; break; case RE_MODULUS_LEN: typeString = msg[2]; break; case RE_PRIVATE_KEY: typeString = msg[3]; break; case RE_PUBLIC_KEY: typeString = msg[4]; break; case RE_SIGNATURE: typeString = msg[5]; break; default: printf("ERROR: Code 0x%04x, %s", type, msg[6]); fflush(stdout); return; } printf("ERROR: %s while %s\n", typeString, task); fflush(stdout);}static int GetCommand(command, maxCommandSize, prompt)char *command;unsigned int maxCommandSize;char *prompt;{ unsigned int i; printf("%s (blank to cancel): \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'; break; } } return(strlen(command));}static char *upr(char *s){ char *p = s; while(*s) { if((*s >= 'a') && (*s <= 'z')) *s += 'A'-'a'; s++; } return p;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -