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

📄 rsaref.txt

📁 RSA加密实现
💻 TXT
📖 第 1 页 / 共 4 页
字号:
         their lengths in encryptedKeyLens. (Note that each         encryptedKeys member should be a pointer, initialized by         the application.)         The encryptedKeyLens members will not be greater thanMAX_ENCRYPTED_KEY_LEN.encryptionAlgorithm is the algorithm with which data are encrypted,and must be one of the values listed in Appendix D.randomStruct must have been seeded.Return value:      0     successRE_ENCRYPTION_ALGORITHM  encryptionAlgorithm is invalid       RE_PUBLIC_KEY     publicKey cannot encrypt data encryption                           key      RE_NEED_RANDOM     randomStruct is not seededR_SealUpdateint R_SealUpdate (  R_ENVELOPE_CTX *context,                               /* context */  unsigned char *partOut,               /* next encrypted data part */  unsigned int *partOutLen,   /* length of next encrypted data part */  unsigned char *partIn,                          /* next data part */  unsigned int partInLen                /* length of next data part */);R_SealUpdate continues a sealing operation, decrypting partIn, thenext data part, with the specified data encryption algorithm, andreturning partOut, the next encrypted data part. It may be called anynumber of times.partOutLen will always be a multiple of 8, and it will not be greaterthan partInLen+7. If partInLen is a multiple of 8, then partOutLenwill be the same as partInLen.(As a special case, if partInLen is a multiple of 24, then partOutLenwill be the same as partInLen; this is helpful when the output is tobe encoded in ASCII, since the length of each part input toR_EncodePEMBlock should be a multiple of 3.)Return value:      0     successR_SealFinalint R_SealFinal (  R_ENVELOPE_CTX *context,                               /* context */  unsigned char *partOut,               /* last encrypted data part */  unsigned int *partOutLen    /* length of last encrypted data part */);R_SealFinal completes a sealing operation, returning partOut, thelast encrypted data part.partOutLen will always be 8.Return value:      0     success3.4 Opening a digital envelopeRSAREF opens digital envelopes with three procedures: R_OpenInit,R_OpenUpdate, and R_OpenFinal. These procedures are typically calledby message-processing applications.An application first calls R_OpenInit, giving an integer specifyingwhich data encryption algorithm to apply (see Appendix D), aninitialization vector, the recipient's RSA private key, and anencrypted data encryption key. R_OpenInit sets up a context for theopening operation and decrypts the encrypted data encryption key withthe private key. It returns the context.The application then calls R_OpenUpdate any number of times, givingthe context and the next encrypted data part. R_OpenUpdate decryptsthe encrypted part and returns the next recovered data part. (Dependingon how data are supplied, it may return more or less data than aresupplied.)After all the parts are supplied, the application calls R_OpenFinal,giving the context. R_OpenFinal returns the last recovered data part.As described for the sealing operations, an application may callR_OpenUpdate again after R_OpenFinal to decrypt other data under thesame data encryption key and initialization vector.R_OpenInitint R_OpenInit (  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 */);R_OpenInit begins an "opening" operation, setting up a new contextand decrypting encryptedKey with privateKey.iv is the initialization vector for the data encryption algorithm.encryptionAlgorithm is the algorithm with which the data isencrypted, and must be one of the values listed in Appendix D.encryptedKeyLen must not be greater than MAX_ENCRYPTED_KEY_LEN.Return value:      0     success              RE_LEN     encryptedKeyLen out of rangeRE_ENCRYPTION_ALGORITHM  encryptionAlgorithm is invalid      RE_PRIVATE_KEY     privateKey cannot decrypt encrypted keyR_OpenUpdateint R_OpenUpdate (  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 */);R_OpenUpdate continues an opening operation, decrypting partIn, thenext encrypted data part, and returning partOut, the next recovereddata part. It may be called any number of times.partOutLen will always be a multiple of 8, and it will not be greaterthan partInLen+7.Return value:      0     successR_OpenFinalint R_OpenFinal (  R_ENVELOPE_CTX *context,                               /* context */  unsigned char *partOut,               /* last recovered data part */  unsigned int *partOutLen    /* length of last recovered data part */);R_SealFinal completes a sealing operation, returning partOut, thelast recovered data part.partOutLen will not be greater than 7.Return value:      0     success              RE_KEY     recovered data encryption key cannot decrypt                           encrypted data3.5 Digesting a messageRSAREF digests messages with three procedures: R_DigestInit,R_DigestUpdate, and R_DigestFinal. These procedures have noparticular PEM application, but may be useful in PKCS #7.An application first calls R_DigestInit, giving an integer specifyingwhich message-digest algorithm to apply (see Appendix D).R_DigestInit sets up a context for the digesting operation, andreturns the context.The application then calls R_DigestUpdate any number of times, givingthe context and the next data part. R_DigestUpdate digests the part.After all the parts are supplied, the application callsR_DigestFinal, giving the context. R_DigestFinal returns the messagedigest.An application may call R_DigestUpdate again after R_DigestFinal todigest other data, without setting up a new context.R_DigestInitint R_DigestInit (  R_DIGEST_CTX *context,                             /* new context */  int digestAlgorithm                   /* message-digest algorithm */);R_DigestInit begins a message-digest operation, setting up a newcontext.digestAlgorithm is the algorithm with which data are digested, andmust be one of the values listed in Appendix D.Return value:      0     success RE_DIGEST_ALGORITHM     digestAlgorithm is invalidR_DigestUpdateint R_DigestUpdate (  R_DIGEST_CTX *context,                                 /* context */  unsigned char *partIn,                          /* next data part */  unsigned int partInLen                /* length of next data part */);R_DigestUpdate continues a message-digest operation, digesting thenext data part with the specified message-digest algorithm. It may becalled any number of times.Return value:      0     successR_DigestFinalint R_DigestFinal (  R_DIGEST_CTX *context,                                 /* context */  unsigned char *digest,                          /* message digest */  unsigned int *digestLen               /* length of message digest */);R_DigestFinal completes a message-digest operation, storing themessage digest in digest and its length in bytes in digestLen.digestLen will not be greater than MAX_DIGEST_LEN.Return value:      0     success4. ENCODING AND DECODINGRSAREF encodes and decodes blocks of data in printable ASCIIaccording to RFC 1421 with two procedures: R_EncodePEMBlockand R_DecodePEMBlock. They are typically called bymessage-processing applications to format and parse fieldsof the encapsulated header of a privacy-enhanced message, aswell as the message content.To encode a block in printable ASCII, an application callsR_EncodePEMBlock, giving a pointer to the block and the block length.R_EncodePEMBlock encodes the block in printable ASCII and returns theencoded block.To decode a block encoded in printable ASCII, an application callsR_DecodePEMBlock, giving a pointer to the encoded block, and theencoded block length. R_DecodePEMBlock decodes the encoded block andreturns the decoded block.An application can process data in parts with these procedures,provided that the length of each input part (except possibly thelast) is a multiple of the "quantum" size: three bytes forencoding, four bytes for decoding.R_EncodePEMBlockint R_EncodePEMBlock (  unsigned char *encodedBlock,                     /* encoded block */  unsigned int *encodedBlockLen,         /* length of encoded block */  unsigned char *block,                                    /* block */  unsigned int blockLen                          /* length of block */);R_EncodePEMBlock encodes block in printable ASCII according to RFC1421, storing the encoding in encodedBlock.encodedBlock will be an ASCII string, encoded according to RFC 1421.(It will not contain any line delimiters; the application must breakthe string into lines.) encodedBlockLen will not be greater thanENCODED_CONTENT_LEN(blockLen).When processing data in parts, blockLen should be a multiple of 3,except possibly for the last part.Return value:      0     success             nonzero     reserved for future compatibilityR_DecodePEMBlockint R_DecodePEMBlock (block, blockLen, encodedBlock, encodedBlockLen)  unsigned char *block,                                    /* block */  unsigned int *blockLen,                        /* length of block */  unsigned char *encodedBlock,                     /* encoded block */  unsigned int encodedBlockLen           /* length of encoded block */);R_DecodePEMBlock decodes a block encoded according to RFC 1421. Itsoperation is the reverse of R_EncodePEMBlock.blockLen will not be greater thanDECODED_CONTENT_LEN(encodedBlockLen).When processing data in parts, encodedBlockLen should be a multipleof 4, except possibly for the last part.Return value:      0     success         RE_ENCODING     encodedBlock has RFC 1421 encoding error5. KEY-PAIR GENERATIONRSAREF generates key pairs with the procedure R_GeneratePEMKeys.R_GeneratePEMKeys is typically called by key generation applications.To generate a new key pair, an application calls R_GeneratePEMKeys,giving the length in bits of the modulus, the choice of publicexponent (3 or 65537), and a random structure. R_GeneratePEMKeysgenerates an RSA key pair and returns the public and private keys.R_GeneratePEMKeysint R_GeneratePEMKeys (  R_RSA_PUBLIC_KEY *publicKey,                /* new RSA public key */  R_RSA_PRIVATE_KEY *privateKey,             /* new RSA private key */  R_RSA_PROTO_KEY *protoKey,                   /* RSA prototype key */  R_RANDOM_STRUCT *randomStruct                 /* random structure */);R_GeneratePEMKeys generates a random RSA key pair, storing theresulting RSA public key in publicKey and the resulting RSA privatekey in privateKey.Other parameters are as follows:            protoKey     The RSA prototype key specifying the length                           in bits of the RSA modulus and the public                           exponent. (See Appendix B.)        randomStruct     Random structure from which the key pair is                           derived. It must have been seeded.Return value:      0     success      RE_MODULUS_LEN     modulus length invalid      RE_NEED_RANDOM     randomStruct is not seeded6. DIFFIE-HELLMAN KEY AGREEMENTTo generate new Diffie-Hellman parameters, an application callsR_GenerateDHParams, giving the length in bits of the Diffie-Hellmanprime and a random structure. R_GenerateDHParams generates theparameters. Several users may share given Diffie-Hellman parameters,or they may be unique to a given user.To set up a key agreement, communicating applications callR_SetupDHAgreement, giving these parameters:  -  the Diffie-Hellman parameters  -  a random structureR_SetupDHAgreement generates a new "public value" and a new "privatevalue" for each party. The applications then exchange their publicvalues.To compute the agreed-upon key, the applications callR_ComputeDHAgreedKey, giving these parameters:  -  the Diffie-Hellman parameters  -  the other party's public value  -  the private valueR_ComputeDHAgreedKey computes the agreed-upon key.The applications may encrypt subsequent data with the agreed-uponkey. When the length of the Diffie-Hellman prime is large enough, itis considered impractical for someone who sees the Diffie-Hellmanparameters and the exchanged public values to determine toagreed-upon key, so the subsequent encryption is secure.R_GenerateDHParamsint R_GenerateDHParams (  R_DH_PARAMS *params,             /* new Diffie-Hellman parameters */  unsigned int primeBits,               /* length in bits of prime  */  unsigned int subPrimeBits,         /* length in bits of subprime  */  R_RANDOM_STRUCT *randomStruct                 /* random structure */);R_GenerateDHParams generates random Diffie-Hellman parameters,storing the result in params. primeBits specifies the length in bitsof the Diffie-Hellman prime p, and subPrimeBits specifies the lengthin bits of the prime q that divides p-1. The resulting generator ghas order q.The resulting params->primeLen and params->generatorLen will be atmost DH_PRIME_LEN (bits); params->prime and params->generator shouldpoint to arrays at least that long.randomStruct must have been seeded.Return value:      0     success      RE_MODULUS_LEN     prime length invalid      RE_NEED_RANDOM     randomStruct is not seededR_SetupDHAgreementint R_SetupDHAgreement (  unsigned char *publicValue,                   /* new public value */  unsigned char *privateValue,                 /* new private value */  unsigned int privateValueLen,          /* length of private value */  R_DH_PARAMS *params,                 /* Diffie-Hellman parameters */  R_RANDOM_STRUCT *randomStruct                 /* random structure */);R_SetupDHAgreement sets up a Diffie-Hellman key agreement by

⌨️ 快捷键说明

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