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

📄 r_enhanc.c

📁 RSA加密实现
💻 C
📖 第 1 页 / 共 3 页
字号:
     context->bufferLen = partInLen);  return (0);}/* Assume partOut buffer is at least 8 bytes. */int R_SealFinal (context, partOut, partOutLen)R_ENVELOPE_CTX *context;                                         /* context */unsigned char *partOut;                         /* last encrypted data part */unsigned int *partOutLen;             /* length of last encrypted data part */{  unsigned int padLen;  /* Pad and encrypt final block.   */  padLen = 8 - context->bufferLen;  R_memset    ((POINTER)(context->buffer + context->bufferLen), (int)padLen, padLen);  CipherUpdate (context, partOut, context->buffer, 8);  *partOutLen = 8;  /* Restart the context.   */  CipherRestart (context);  context->bufferLen = 0;  return (0);}/* Assume caller has already ASCII decoded the encryptedKey if necessary. */int R_OpenInit  (context, encryptionAlgorithm, encryptedKey, encryptedKeyLen, iv, privateKey)R_ENVELOPE_CTX *context;                                     /* new context */int encryptionAlgorithm;                       /* data encryption algorithm */unsigned char *encryptedKey;               /* encrypted data encryption key */unsigned int encryptedKeyLen;                    /* length of encrypted key */unsigned char iv[8];                               /* initialization vector */R_RSA_PRIVATE_KEY *privateKey;               /* recipient's RSA private key */{  int status;  unsigned char key[MAX_ENCRYPTED_KEY_LEN];  unsigned int keyLen;    if (encryptedKeyLen > MAX_ENCRYPTED_KEY_LEN)    return (RE_LEN);    do {    context->encryptionAlgorithm = encryptionAlgorithm;    if (RSAPrivateDecrypt        (key, &keyLen, encryptedKey, encryptedKeyLen, privateKey)) {      status = RE_PRIVATE_KEY;      break;    }    if (encryptionAlgorithm == EA_DES_CBC) {          if (keyLen != 8) {        status = RE_PRIVATE_KEY;        break;      }    }    else {      if (keyLen != 24) {        status = RE_PRIVATE_KEY;        break;      }    }        if ((status = CipherInit (context, encryptionAlgorithm, key, iv, 0)) != 0)      break;    context->bufferLen = 0;  } while (0);    /* Zeroize sensitive information.   */  R_memset ((POINTER)key, 0, sizeof (key));  return (status);}/* Assume partOut buffer is at least partInLen + 7, since this may flush     buffered input. Always leaves at least one byte in buffer. */int R_OpenUpdate (context, partOut, partOutLen, partIn, partInLen)R_ENVELOPE_CTX *context;                                         /* context */unsigned char *partOut;                         /* next recovered data part */unsigned int *partOutLen;             /* length of next recovered data part */unsigned char *partIn;                          /* next encrypted data part */unsigned int partInLen;               /* length of next encrypted data part */{  unsigned int tempLen;  tempLen = 8 - context->bufferLen;  if (partInLen <= tempLen) {    /* Just accumulate into buffer.     */    R_memcpy      ((POINTER)(context->buffer + context->bufferLen), (POINTER)partIn,       partInLen);    context->bufferLen += partInLen;    *partOutLen = 0;    return (0);  }  /* Fill the buffer and decrypt.  We know that there will be more left       in partIn after decrypting the buffer.   */  R_memcpy    ((POINTER)(context->buffer + context->bufferLen), (POINTER)partIn,     tempLen);  CipherUpdate (context, partOut, context->buffer, 8);  partIn += tempLen;  partInLen -= tempLen;  partOut += 8;  *partOutLen = 8;  /* Decrypt as many 8 byte blocks as possible, leaving at least one byte       in partIn.   */  tempLen = 8 * ((partInLen - 1) / 8);  CipherUpdate (context, partOut, partIn, tempLen);  partIn += tempLen;  partInLen -= tempLen;  *partOutLen += tempLen;  /* Length is between 1 and 8, so copy into buffer.   */  R_memcpy    ((POINTER)context->buffer, (POINTER)partIn,     context->bufferLen = partInLen);  return (0);}/* Assume partOut buffer is at least 7 bytes. */int R_OpenFinal (context, partOut, partOutLen)R_ENVELOPE_CTX *context;                                         /* context */unsigned char *partOut;                         /* last recovered data part */unsigned int *partOutLen;             /* length of last recovered data part */{  int status;  unsigned char lastPart[8];  unsigned int padLen;  status = 0;  do {    if (context->bufferLen == 0)      /* There was no input data to decrypt */      *partOutLen = 0;    else {      if (context->bufferLen != 8) {        status = RE_KEY;        break;      }      /* Decrypt and strip padding from final block which is in buffer.       */      CipherUpdate (context, lastPart, context->buffer, 8);          padLen = lastPart[7];      if (padLen == 0 || padLen > 8) {        status = RE_KEY;        break;      }      if (R_memcmp           ((POINTER)&lastPart[8 - padLen], PADDING[padLen], padLen) != 0) {        status = RE_KEY;        break;      }            R_memcpy ((POINTER)partOut, (POINTER)lastPart, *partOutLen = 8 - padLen);    }    /* Restart the context.     */    CipherRestart (context);    context->bufferLen = 0;  } while (0);  /* Zeroize sensitive information.   */  R_memset ((POINTER)lastPart, 0, sizeof (lastPart));  return (status);}int R_SignPEMBlock   (encodedContent, encodedContentLen, encodedSignature, encodedSignatureLen,   content, contentLen, recode, digestAlgorithm, privateKey)unsigned char *encodedContent;                           /* encoded content */unsigned int *encodedContentLen;               /* length of encoded content */unsigned char *encodedSignature;                       /* encoded signature */unsigned int *encodedSignatureLen;           /* length of encoded signature */unsigned char *content;                                          /* content */unsigned int contentLen;                               /* length of content */int recode;                                                /* recoding flag */int digestAlgorithm;                            /* message-digest algorithm */R_RSA_PRIVATE_KEY *privateKey;                  /* signer's RSA private key */{  int status;  unsigned char signature[MAX_SIGNATURE_LEN];  unsigned int signatureLen;    if ((status = R_SignBlock       (signature, &signatureLen, content, contentLen, digestAlgorithm,        privateKey)) != 0)    return (status);  R_EncodePEMBlock     (encodedSignature, encodedSignatureLen, signature, signatureLen);  if (recode)    R_EncodePEMBlock    (encodedContent, encodedContentLen, content, contentLen);  return (0);}int R_SignBlock  (signature, signatureLen, block, blockLen, digestAlgorithm, privateKey)unsigned char *signature;                                      /* signature */unsigned int *signatureLen;                          /* length of signature */unsigned char *block;                                              /* block */unsigned int blockLen;                                   /* length of block */int digestAlgorithm;                            /* message-digest algorithm */R_RSA_PRIVATE_KEY *privateKey;                  /* signer's RSA private key */{  R_SIGNATURE_CTX context;  int status;  do {    if ((status = R_SignInit (&context, digestAlgorithm)) != 0)      break;    if ((status = R_SignUpdate (&context, block, blockLen)) != 0)      break;    if ((status = R_SignFinal (&context, signature, signatureLen, privateKey))        != 0)      break;  } while (0);  /* Zeroize sensitive information. */  R_memset ((POINTER)&context, 0, sizeof (context));  return (status);}int R_VerifyPEMSignature   (content, contentLen, encodedContent, encodedContentLen, encodedSignature,   encodedSignatureLen, recode, digestAlgorithm, publicKey)unsigned char *content;                                          /* content */unsigned int *contentLen;                              /* length of content */unsigned char *encodedContent;                /* (possibly) encoded content */unsigned int encodedContentLen;                /* length of encoded content */unsigned char *encodedSignature;                       /* encoded signature */unsigned int encodedSignatureLen;            /* length of encoded signature */int recode;                                                /* recoding flag */int digestAlgorithm;                            /* message-digest algorithm */R_RSA_PUBLIC_KEY *publicKey;                     /* signer's RSA public key */{  unsigned char signature[MAX_SIGNATURE_LEN];  unsigned int signatureLen;    if (encodedSignatureLen > MAX_PEM_SIGNATURE_LEN)    return (RE_SIGNATURE_ENCODING);    if (recode) {    if (R_DecodePEMBlock        (content, contentLen, encodedContent, encodedContentLen))      return (RE_CONTENT_ENCODING);  }  else {    content = encodedContent;    *contentLen = encodedContentLen;  }      if (R_DecodePEMBlock      (signature, &signatureLen, encodedSignature, encodedSignatureLen))    return (RE_SIGNATURE_ENCODING);    return (R_VerifyBlockSignature           (content, *contentLen, signature, signatureLen, digestAlgorithm,           publicKey));}int R_VerifyBlockSignature   (block, blockLen, signature, signatureLen, digestAlgorithm, publicKey)unsigned char *block;                                              /* block */unsigned int blockLen;                                   /* length of block */unsigned char *signature;                                      /* signature */unsigned int signatureLen;                           /* length of signature */int digestAlgorithm;                            /* message-digest algorithm */R_RSA_PUBLIC_KEY *publicKey;                     /* signer's RSA public key */{  R_SIGNATURE_CTX context;  int status;  do {    if ((status = R_VerifyInit (&context, digestAlgorithm)) != 0)      break;    if ((status = R_VerifyUpdate (&context, block, blockLen)) != 0)      break;    if ((status = R_VerifyFinal (&context, signature, signatureLen, publicKey))        != 0)      break;  } while (0);  /* Zeroize sensitive information. */  R_memset ((POINTER)&context, 0, sizeof (context));  return (status);}int R_SealPEMBlock   (encryptedContent, encryptedContentLen, encryptedKey, encryptedKeyLen,   encryptedSignature, encryptedSignatureLen, iv, content, contentLen,   digestAlgorithm, publicKey, privateKey, randomStruct)unsigned char *encryptedContent;              /* encoded, encrypted content */unsigned int *encryptedContentLen;                                /* length */

⌨️ 快捷键说明

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