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

📄 r_enhanc.c

📁 des和rsa加密算法程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	keyLen = (encryptionAlgorithm == EA_DES_CBC) ? 8 : 24;	if((status = R_GenerateBytes (key, keyLen, randomStruct)) == 0) {		if((status = R_GenerateBytes (iv, 8, randomStruct)) == 0) {			if(encryptionAlgorithm == EA_DES_EDE2_CBC)					/* Make both E keys the same */				R_memcpy ((POINTER)(key + 16), (POINTER)key, 8);			if((status = CipherInit (context, encryptionAlgorithm, key, keyLen, iv, 1)) == 0) {				for(i = 0; i < publicKeyCount; ++i) {					if(RSAPublicEncrypt(encryptedKeys[i], &encryptedKeyLens[i], key, keyLen,							 publicKeys[i], randomStruct)) {						status = RE_PUBLIC_KEY;						break;					}				}			}			if(status == 0)				context->bufferLen = 0;		}	}	/* Clear sensitive information. */	R_memset(key, 0, sizeof(key));	return(status);}/* partOut buffer should be at least partInLen + 7 */int R_SealUpdate (context, partOut, partOutLen, partIn, partInLen)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 */{	unsigned int temp;	temp = 8 - context->bufferLen;	if(partInLen < temp) {						/* Just accumulate into buffer. */		*partOutLen = 0;		R_memcpy((POINTER)(context->buffer + context->bufferLen), (POINTER)partIn, partInLen);        context->bufferLen += partInLen;    /* Bug Fix - 02/09/95, SK */		return(ID_OK);	}	/* Fill the buffer and encrypt. */	R_memcpy((POINTER)(context->buffer + context->bufferLen), (POINTER)partIn, temp);	EncryptBlk(context, partOut, context->buffer, 8);	partOut += 8;	*partOutLen = 8;	partIn += temp;	partInLen -= temp;	/* Encrypt as many 8-byte blocks as possible. */	temp = 8 * (partInLen / 8);	EncryptBlk(context, partOut, partIn, temp);	*partOutLen += temp;	partIn += temp;	partInLen -= temp;	/* Length now less than 8, so copy remainder to buffer for next time. */	R_memcpy((POINTER)context->buffer, partIn, context->bufferLen = partInLen);	return(ID_OK);}/* 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;                        /* little trick to pad the block */	R_memset((POINTER)(context->buffer + context->bufferLen), (int)padLen, padLen);	EncryptBlk(context, partOut, context->buffer, 8);	*partOutLen = 8;	/* Restart the context. */	RestartCipher(context);	context->bufferLen = 0;	return(ID_OK);}/* 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);	context->encryptionAlgorithm = encryptionAlgorithm;	if(RSAPrivateDecrypt(key, &keyLen, encryptedKey, encryptedKeyLen, privateKey)) {		status = RE_PRIVATE_KEY;	}else{		if(encryptionAlgorithm == EA_DES_CBC) {			if(keyLen != 8) status = RE_PRIVATE_KEY;			else{				if((status = CipherInit (context, encryptionAlgorithm, key, keyLen, iv, 0)) == 0)					context->bufferLen = 0;			}		}else{			if(keyLen != 24) status = RE_PRIVATE_KEY;			else {				if((status = CipherInit (context, encryptionAlgorithm, key, keyLen, iv, 0)) == 0)					context->bufferLen = 0;			}		}	}	/* Clear sensitive information. */	R_memset(key, 0, sizeof(key));	return(status);}/* Assume partOut buffer is at least partInLen + 7.	 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. */		*partOutLen = 0;		R_memcpy((POINTER)(context->buffer + context->bufferLen), partIn, partInLen);		context->bufferLen += partInLen;		return(ID_OK);	}	/* 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), partIn, tempLen);	EncryptBlk (context, partOut, context->buffer, 8);	partOut += 8;	*partOutLen = 8;	partIn += tempLen;	partInLen -= tempLen;	/* Decrypt as many 8 byte blocks as possible, leaving at least one byte		 in partIn.      */	tempLen = 8 * ((partInLen - 1) / 8);	EncryptBlk (context, partOut, partIn, tempLen);	partIn += tempLen;	*partOutLen += tempLen;	partInLen -= tempLen;			/* Length is between 1 and 8, so copy into buffer. */	R_memcpy((POINTER)context->buffer, partIn, context->bufferLen = partInLen);	return (ID_OK);}/* 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;	if(context->bufferLen == 0)			/* There was no input data to decrypt */		*partOutLen = 0;	else {		if(context->bufferLen != 8) {			status = RE_KEY;		}else{			/* Decrypt and strip any padding from the final block. */			EncryptBlk (context, lastPart, context->buffer, 8);			padLen = lastPart[7];			if(padLen == 0 || padLen > 8)				status = RE_KEY;			else{				if(R_memcmp((POINTER)&lastPart[8 - padLen], PADDING[padLen], padLen) != 0)					status = RE_KEY;				else					R_memcpy (partOut, lastPart, *partOutLen = 8 - padLen);			}				/* Restart the context. */			if(status == 0) {				RestartCipher(context);				context->bufferLen = 0;			}		}	}	/* Clear sensitive information. */	R_memset(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);	if(recode)		R_EncodePEMBlock(encodedContent, encodedContentLen, content, contentLen);	R_EncodePEMBlock(encodedSignature, encodedSignatureLen, signature, signatureLen);	return(ID_OK);}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;	if((status = R_SignInit(&context, digestAlgorithm)) == 0)		if((status = R_SignUpdate(&context, block, blockLen)) == 0)			status = R_SignFinal(&context, signature, signatureLen, privateKey);	/* Clear 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{		*contentLen = encodedContentLen;		content = encodedContent;	}	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 */

⌨️ 快捷键说明

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