• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ EVP_EncryptUpdate函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中EVP_EncryptUpdate函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_EncryptUpdate函数的具体用法?C++ EVP_EncryptUpdate怎么用?C++ EVP_EncryptUpdate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了EVP_EncryptUpdate函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: aes_encrypt

int aes_encrypt(EVP_CIPHER_CTX *e,int in,int out )	 /* this function encryptes the  file:fd is passed as parameter */
{
	char inbuf [SIZE];
	char outbuf[SIZE+AES_BLOCK_SIZE];	
	int inlen = 0,flen=0,outlen =0;
	
		
	if(!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)) 				/* allows reusing of e for multiple cipher cycles */
	{
		perror("\n Error,ENCRYPR_INIT:");
		return 1;
	}
	while((inlen = read(in,inbuf,SIZE)) > 0)
	{
		if(!EVP_EncryptUpdate(e,(unsigned char*) outbuf, &outlen,(unsigned char*) inbuf,inlen)) 		/* Update cipher text */
		{
			perror("\n ERROR,ENCRYPR_UPDATE:");
			return 1;
		}
		if(write(out,outbuf,outlen) != outlen)	
		{
			perror("\n ERROR,Cant write encrypted bytes to outfile:");
			return 1;
		}	
	}
	if(!EVP_EncryptFinal_ex(e, (unsigned char*) outbuf, &flen)) 			/* updates the remaining bytes */
	{
		perror("\n ERROR,ENCRYPT_FINAL:");
		return 1;
	}
	if(write(out,outbuf,flen) != flen)
	{
		perror("\n ERROR,Wriring final bytes of data:");
		return 1;
	}
	return 0;	
}
开发者ID:stevezhougs,项目名称:mpro,代码行数:37,代码来源:aes2.c


示例2: OldEncryptAES256

// General secure AES 256 CBC encryption routine
bool OldEncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext)
{
    // max ciphertext len for a n bytes of plaintext is
    // n + AES_BLOCK_SIZE - 1 bytes
    int nLen = sPlaintext.size();
    int nCLen = nLen + AES_BLOCK_SIZE;
    int nFLen = 0;

    // Verify key sizes
    if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) {
        LogPrintf("crypter EncryptAES256 - Invalid key or block size: Key: %d sIV:%d\n", sKey.size(), sIV.size());
        return false;
    }

    // Prepare output buffer
    sCiphertext.resize(nCLen);

    // Perform the encryption
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();

    if (!ctx) return false;

    bool fOk = true;

    EVP_CIPHER_CTX_init(ctx);
    if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*) &sKey[0], (const unsigned char*) &sIV[0]);
    if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*) &sCiphertext[0], &nCLen, (const unsigned char*) &sPlaintext[0], nLen);
    if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*) (&sCiphertext[0])+nCLen, &nFLen);
    EVP_CIPHER_CTX_cleanup(ctx);

    EVP_CIPHER_CTX_free(ctx);

    if (!fOk) return false;

    sCiphertext.resize(nCLen + nFLen);
    return true;
}
开发者ID:syscoin,项目名称:syscoin2,代码行数:38,代码来源:crypto_tests.cpp


示例3: crypto_aes_encrypt

int crypto_aes_encrypt (crypto_aes_t *crypto,
                        const void *src,
                        unsigned int src_size,
                        void *dst,
                        unsigned int *dst_size)
{
    EVP_CIPHER_CTX *e = &(crypto->enc);
    int psize = 0;
    int fsize = 0;

    /* allows reusing of 'e' for multiple encryption cycles */
    if (!EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL)) {
        pthread_mutex_unlock(&(crypto->lock));
        return(-1);
    }

    /* update ciphertext, c_len is filled with the length of ciphertext
     * generated, *len is the size of plaintext in bytes
     */
    if (!EVP_EncryptUpdate(e, dst, &psize, src, src_size)) {
        pthread_mutex_unlock(&(crypto->lock));
        return(-2);
    }

    /* update ciphertext with the final remaining bytes */
    if (!EVP_EncryptFinal_ex(e, dst + psize, &fsize)) {
        pthread_mutex_unlock(&(crypto->lock));
        return(-3);
    }

    if (dst_size != NULL)
        *dst_size = psize + fsize;

    pthread_mutex_unlock(&(crypto->lock));
    return(0);
}
开发者ID:darjeeling,项目名称:misc-common,代码行数:36,代码来源:aes_openssl.c


示例4: in

/* INFO: Encrypting the message

         So now that we have set up the program we need to define the "encrypt"
         function. This will take as parameters the plaintext, the length of
         the plaintext, the key to be used, and the IV. We'll also take in a
         buffer to put the ciphertext in(which we assume to be long enough),
         and will return the length of the ciphertext that we have written. 

         Encrypting consists of the following stages:
         * Setting up a context
         * Initialising the encryption operation
         * Providing plaintext bytes to be encrypted
         * Finalising the encryption operation

         During initialisation we will provide an EVP_CIPHER object. In this
         case we are using EVP_aes_256_cbc(), which uses the AES algorithm with
         a 256-bit key in CBC mode. Refer to EVP#Working with Algorithms and
         Modes for further details. */
static int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
	unsigned char *iv, unsigned char *ciphertext) {
	EVP_CIPHER_CTX *ctx;

	int len;

	int ciphertext_len;

	/* INFO: Create and initialise the context */
	if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

	/* INFO: Initialise the encryption operation. IMPORTANT - ensure you
                 use a key and IV size appropriate for your cipher.In this
	         example we are using 256 bit AES(i.e. a 256 bit key). The IV
	         size for *most* modes is the same as the block size. For AES
	         this is 128 bits */
	if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
	handleErrors();

	/* INFO: Provide the message to be encrypted, and obtain the encrypted
	         output. EVP_EncryptUpdate can be called multiple times if
	         necessary */
	if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
	handleErrors();
	ciphertext_len = len;

	/* INFO: Finalise the encryption. Further ciphertext bytes may be
	         written at this stage. */
	if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
	ciphertext_len += len;

	/* info: Clean up */
	EVP_CIPHER_CTX_free(ctx);

	return ciphertext_len;
}
开发者ID:archipelagos,项目名称:openssl,代码行数:54,代码来源:aes.c


示例5: aes_encrypt

static bool aes_encrypt(void *dst, const void *src, size_t len,
			const struct enckey *enckey, const struct iv *iv)
{
	EVP_CIPHER_CTX evpctx;
	int outlen;

	/* Counter mode allows parallelism in future. */
	if (EVP_EncryptInit(&evpctx, EVP_aes_128_ctr(),
			    memcheck(enckey->k.u.u8, sizeof(enckey->k)),
			    memcheck(iv->iv, sizeof(iv->iv))) != 1)
		return false;

	/* No padding, we're a multiple of 128 bits. */
	if (EVP_CIPHER_CTX_set_padding(&evpctx, 0) != 1)
		return false;

	EVP_EncryptUpdate(&evpctx, dst, &outlen, memcheck(src, len), len);
	assert(outlen == len);
	/* Shouldn't happen (no padding) */
	if (EVP_EncryptFinal(&evpctx, dst, &outlen) != 1)
		return false;
	assert(outlen == 0);
	return true;
}
开发者ID:throckmortonsign,项目名称:lightning,代码行数:24,代码来源:test_onion.c


示例6: malloc

/*
 * Encrypt *len bytes of data
 * All data going in & out is considered binary (unsigned char[])
 */
unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len, int *retlen)
{
  /* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE -1 bytes */
	int rc=0;
  int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
	//fprintf(stderr, "Line: %d -- len: %d , c_len: %d , f_len: %d\n", __LINE__, *len, c_len, f_len);
  unsigned char *ciphertext = malloc(c_len);

  /* allows reusing of 'e' for multiple encryption cycles */
	rc=EVP_EncryptInit_ex(e, NULL, NULL, NULL, NULL);
//  rc=EVP_EncryptInit_ex(e, EVP_aes_256_cbc(), NULL, key, iv);
	assert(rc==1);
  /* update ciphertext, c_len is filled with the length of ciphertext generated,
    *len is the size of plaintext in bytes */
  rc=EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);
	assert(rc==1);

  /* update ciphertext with the final remaining bytes */
	  rc=EVP_EncryptFinal_ex(e, ciphertext+c_len, &f_len);
		assert(rc==1);

  *len = c_len + f_len;
  return ciphertext;
}
开发者ID:renecannao,项目名称:aes-multi,代码行数:28,代码来源:aes-multi.c


示例7: do_encrypt

static int do_encrypt(EVP_CIPHER_CTX* aes256, pubnub_bymebl_t msg, uint8_t const* key, uint8_t const* iv, pubnub_bymebl_t *encrypted)
{
    int len = 0;

    if (!EVP_EncryptInit_ex(aes256, EVP_aes_256_cbc(), NULL, key, iv)) {
        ERR_print_errors_cb(print_to_pubnub_log, NULL);
        PUBNUB_LOG_ERROR("Failed to initialize AES-256 encryption\n");
        return -1;
    }
    if (!EVP_EncryptUpdate(aes256, encrypted->ptr, &len, msg.ptr, msg.size)) {
        ERR_print_errors_cb(print_to_pubnub_log, NULL);
        PUBNUB_LOG_ERROR("Failed to AES-256 encrypt the mesage\n");
        return -1;
    }
    encrypted->size = len;
    if (!EVP_EncryptFinal_ex(aes256, encrypted->ptr + len, &len)) {
        ERR_print_errors_cb(print_to_pubnub_log, NULL);
        PUBNUB_LOG_ERROR("Failed to finalize AES-256 encryption\n");
        return -1;
    }
    encrypted->size += len;
    
    return 0;
}
开发者ID:sveljko,项目名称:c-core,代码行数:24,代码来源:pbaes256.c


示例8: OPENSSL_HEADER

CK_RV PKCS11_Encryption_OpenSSL::EncryptUpdate(Cryptoki_Session_Context* pSessionCtx, CK_BYTE_PTR pPart, CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen)   
{
    OPENSSL_HEADER();
    OpenSSLEncryptData *pEnc;
    
    if(pSessionCtx == NULL || pSessionCtx->EncryptionCtx == NULL) return CKR_SESSION_CLOSED;

    pEnc = (OpenSSLEncryptData*)pSessionCtx->EncryptionCtx;
    
    if(pEnc->IsSymmetric)
    {
        int outLen = *pulEncryptedPartLen;
        
        OPENSSL_CHECKRESULT(EVP_EncryptUpdate((EVP_CIPHER_CTX*)pEnc->Key->ctx, pEncryptedPart, &outLen, pPart, ulPartLen));
        
        *pulEncryptedPartLen = outLen;
    }
    else
    {
        size_t encLen = *pulEncryptedPartLen;
        
        OPENSSL_CHECKRESULT(EVP_PKEY_encrypt((EVP_PKEY_CTX*)pEnc->Key->ctx, pEncryptedPart, &encLen, pPart, ulPartLen));

        *pulEncryptedPartLen = encLen;
    }

    OPENSSL_CLEANUP();

    if(retVal != CKR_OK)
    {
        TINYCLR_SSL_FREE(pEnc);
        pSessionCtx->EncryptionCtx = NULL;
    }

    OPENSSL_RETURN();
}
开发者ID:AustinWise,项目名称:Netduino-Micro-Framework,代码行数:36,代码来源:OpenSSL_PKCS11_encryption.cpp


示例9: EVP_CIPHER_CTX_init

bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
{
    if (!fKeySet)
        return false;

    // max ciphertext len for a n bytes of plaintext is
    // n + AES_BLOCK_SIZE - 1 bytes
    int nLen = vchPlaintext.size();
    int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
    vchCiphertext = std::vector<unsigned char> (nCLen);

    EVP_CIPHER_CTX ctx;

    EVP_CIPHER_CTX_init(&ctx);
    EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);

    EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
    EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);

    EVP_CIPHER_CTX_cleanup(&ctx);

    vchCiphertext.resize(nCLen + nFLen);
    return true;
}
开发者ID:dianna-project,项目名称:libcoin,代码行数:24,代码来源:Crypter.cpp


示例10: gen_ossl_encrypt

static int
gen_ossl_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen,
				 uint8 *res)
{
	ossldata   *od = c->ptr;
	int			outlen;

	if (!od->init)
	{
		EVP_CIPHER_CTX_init(&od->evp_ctx);
		if (!EVP_EncryptInit_ex(&od->evp_ctx, od->evp_ciph, NULL, NULL, NULL))
			return PXE_CIPHER_INIT;
		if (!EVP_CIPHER_CTX_set_key_length(&od->evp_ctx, od->klen))
			return PXE_CIPHER_INIT;
		if (!EVP_EncryptInit_ex(&od->evp_ctx, NULL, NULL, od->key, od->iv))
			return PXE_CIPHER_INIT;
		od->init = true;
	}

	if (!EVP_EncryptUpdate(&od->evp_ctx, res, &outlen, data, dlen))
		return PXE_ERR_GENERIC;

	return 0;
}
开发者ID:cconvey,项目名称:postgres,代码行数:24,代码来源:openssl.c


示例11: apr_palloc

/*
 * AES encrypt plaintext
 */
unsigned char *oidc_crypto_aes_encrypt(request_rec *r, oidc_cfg *cfg,
		unsigned char *plaintext, int *len) {

	if (oidc_crypto_init(cfg, r->server) == FALSE)
		return NULL;

	/* max ciphertext len for a n bytes of plaintext is n + AES_BLOCK_SIZE -1 bytes */
	int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
	unsigned char *ciphertext = apr_palloc(r->pool, c_len);

	/* allows reusing of 'e' for multiple encryption cycles */
	if (!EVP_EncryptInit_ex(cfg->encrypt_ctx, NULL, NULL, NULL, NULL)) {
		oidc_error(r, "EVP_EncryptInit_ex failed: %s",
				ERR_error_string(ERR_get_error(), NULL));
		return NULL;
	}

	/* update ciphertext, c_len is filled with the length of ciphertext generated, len is the size of plaintext in bytes */
	if (!EVP_EncryptUpdate(cfg->encrypt_ctx, ciphertext, &c_len, plaintext,
			*len)) {
		oidc_error(r, "EVP_EncryptUpdate failed: %s",
				ERR_error_string(ERR_get_error(), NULL));
		return NULL;
	}

	/* update ciphertext with the final remaining bytes */
	if (!EVP_EncryptFinal_ex(cfg->encrypt_ctx, ciphertext + c_len, &f_len)) {
		oidc_error(r, "EVP_EncryptFinal_ex failed: %s",
				ERR_error_string(ERR_get_error(), NULL));
		return NULL;
	}

	*len = c_len + f_len;

	return ciphertext;
}
开发者ID:azenk,项目名称:mod_auth_openidc,代码行数:39,代码来源:crypto.c


示例12: QString

QString UBCryptoUtils::symetricEncrypt(const QString& clear)
{
    QByteArray clearData = clear.toUtf8();

    int cipheredLength = clearData.length() + AES_BLOCK_SIZE;
    int paddingLength = 0;
    unsigned char *ciphertext = (unsigned char *)malloc(cipheredLength);

    if(!EVP_EncryptInit_ex(&mAesEncryptContext, NULL, NULL, NULL, NULL))
        return QString();

    if(!EVP_EncryptUpdate(&mAesEncryptContext, ciphertext, &cipheredLength, (unsigned char *)clearData.data(), clearData.length()))
        return QString();

    /* update ciphertext with the final remaining bytes */
    if(!EVP_EncryptFinal_ex(&mAesEncryptContext, ciphertext + cipheredLength, &paddingLength))
        return QString();

    QByteArray cipheredData((const char *)ciphertext, cipheredLength + paddingLength);

    free(ciphertext);

    return QString::fromAscii(cipheredData.toBase64());
}
开发者ID:kigeia,项目名称:Sankore-3.1,代码行数:24,代码来源:UBCryptoUtils.cpp


示例13: handleErrors

void Crypt::encrypt(unsigned char* plaintext, int plaintextLength, unsigned char *key, unsigned char* iv, unsigned char* ciphertext, int* ciphertextLength) {

	EVP_CIPHER_CTX *ctx;

	int len;

	/* Create and initialise the context */
	if (!(ctx = EVP_CIPHER_CTX_new()))
		handleErrors();

	/* Initialise the encryption operation. IMPORTANT - ensure you use a key
	 * and IV size appropriate for your cipher
	 * In this example we are using 256 bit AES (i.e. a 256 bit key). The
	 * IV size for *most* modes is the same as the block size. For AES this
	 * is 128 bits */
//	if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
	if (1 != EVP_EncryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, key, iv))
		handleErrors();

	/* Provide the message to be encrypted, and obtain the encrypted output.
	 * EVP_EncryptUpdate can be called multiple times if necessary
	 */
	if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintextLength))
		handleErrors();
	(*ciphertextLength) = len;

	/* Finalise the encryption. Further ciphertext bytes may be written at
	 * this stage.
	 */
	if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
		handleErrors();
	(*ciphertextLength) += len;

	/* Clean up */
	EVP_CIPHER_CTX_free(ctx);
}
开发者ID:thiagoh,项目名称:cpp-netlib,代码行数:36,代码来源:Crypt.cpp


示例14: PEM_SealUpdate

void
PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
    unsigned char *in, int inl)
{
	unsigned char buffer[1600];
	int i, j;

	*outl = 0;
	EVP_SignUpdate(&ctx->md, in, inl);
	for (;;) {
		if (inl <= 0)
			break;
		if (inl > 1200)
			i = 1200;
		else
			i = inl;
		EVP_EncryptUpdate(&ctx->cipher, buffer, &j, in, i);
		EVP_EncodeUpdate(&ctx->encode, out, &j, buffer, j);
		*outl += j;
		out += j;
		in += i;
		inl -= i;
	}
}
开发者ID:benwh4,项目名称:libressl,代码行数:24,代码来源:pem_seal.c


示例15: eccx08_BN_encrypt

/**
 *
 * \brief Encrypt a BIGNUM data using AES-256 OFB mode.
 *
 * \param[in/out] number A pointer to the BIGNUM structure. The
 *       encrypted data replaces the plain text in this
 *       structure
 * \param[in] iv A pointer to a 16-byte IV buffer
 * \param[in] aes_key A pointer to a 32-byte AES key buffer
 * \return 1 for success
 */
int eccx08_BN_encrypt(BIGNUM *number, uint8_t *iv, uint8_t *aes_key)
{
    int ret = 0;
    int len;
    int cipher_len;
    uint8_t *plaintext = NULL;
    uint8_t *ciphertext = NULL;

    EVP_CIPHER_CTX *ctx = NULL;

    eccx08_debug("eccx08_BN_encrypt()\n");

    len = BN_num_bytes(number);
    plaintext = (char *)OPENSSL_malloc(len);
    if (!plaintext) {
        goto err;
    }
    ciphertext = (char *)OPENSSL_malloc(len);
    if (!ciphertext) {
        goto err;
    }
    BN_bn2bin(number, plaintext);

    /* Create and initialise the context */
    if (!(ctx = EVP_CIPHER_CTX_new())) {
        eccx08_debug("eccx08_BN_encrypt() context init failed\n");
        goto err;
    }

    /* Initialise the encryption operation. IMPORTANT - ensure you use a key
     * and IV size appropriate for your cipher.
     * We are using 256 bit AES (i.e. a 256 bit key), OFB mode (plain_len == cipher_len). 
     * The IV size for *most* modes is the same as the block size. For AES this
     * is 128 bits */
    if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ofb(), NULL, aes_key, iv)) {
        eccx08_debug("eccx08_BN_encrypt() encrypt init failed\n");
        goto err;
    }

    if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, len)) {
        eccx08_debug("eccx08_BN_encrypt() encrypt update failed\n");
        goto err;
    }
    cipher_len = len;

    if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
        eccx08_debug("eccx08_BN_encrypt() encrypt final failed\n");
        goto err;
    }
    cipher_len += len;
    BN_bin2bn(ciphertext, cipher_len, number);
    ret = 1;

err:
    if (ctx) {
        EVP_CIPHER_CTX_free(ctx);
    }
    if (plaintext) {
        OPENSSL_free(plaintext);
    }
    if (ciphertext) {
        OPENSSL_free(ciphertext);
    }

    return ret;
}
开发者ID:TacoComfort,项目名称:cryptoauth-openssl-engine,代码行数:77,代码来源:eccx08_common.c


示例16: client_write

bool client_write(User *user)
{
	//If there is data in the output buffer, crypt it before writing
	if (!user->buffer.getWriteEmpty()) {
		std::vector<char> buf;

		user->buffer.getWriteData(buf);

		//More glue - Fador
		if(user->crypted) {
			//We might have to write some data uncrypted ToDo: fix
			if(user->uncryptedLeft) {
				user->bufferCrypted.addToWrite((uint8_t *)buf.data(),user->uncryptedLeft);
			}
			int p_len = buf.size()-user->uncryptedLeft, f_len = 0;
			if(p_len) {
				uint8_t *buffer = (uint8_t *)malloc(p_len+1);
				EVP_EncryptUpdate(&user->en, (uint8_t *)buffer, &p_len, (const uint8_t *)buf.data()+user->uncryptedLeft, buf.size()-user->uncryptedLeft);
				int written = p_len + f_len;
				user->bufferCrypted.addToWrite((uint8_t *)buffer,written);
				free(buffer);
			}
			user->uncryptedLeft = 0;
		} else {
			user->bufferCrypted.addToWrite((uint8_t *)buf.data(),buf.size());
			user->uncryptedLeft = 0;
		}

		//free(outBuf);
		user->buffer.clearWrite(buf.size());
	}

	//We have crypted data ready to be written
	if(!user->bufferCrypted.getWriteEmpty()) {
		std::vector<char> buf;
		user->bufferCrypted.getWriteData(buf);

		//Try to write the whole buffer
		const int written = send(user->fd, buf.data(), buf.size(), 0);

		//Handle errors
		if (written == SOCKET_ERROR) {
#ifdef WIN32
#define ERROR_NUMBER WSAGetLastError()
			if ((ERROR_NUMBER != WSATRY_AGAIN && ERROR_NUMBER != WSAEINTR && ERROR_NUMBER != WSAEWOULDBLOCK))
#else
#define ERROR_NUMBER errno
			if ((errno != EAGAIN && errno != EINTR))
#endif
			{
				LOG2(ERROR, "Error writing to client, tried to write " + dtos(buf.size()) + " bytes, code: " + dtos(ERROR_NUMBER));
				//delete user;
				user->logged = false;
				ServerInstance->usersToRemove().insert(user);
				return false;
			}
		} else {
			//Remove written amount from the buffer
			user->bufferCrypted.clearWrite(written);
		}

		//If we couldn't write everything at once, add EV_WRITE event calling this function again..
		if (!user->bufferCrypted.getWriteEmpty()) {
			event_add(user->getWriteEvent(), nullptr);
			return false;
		}
	}
	return true;
}
开发者ID:chtisgit,项目名称:mine152,代码行数:69,代码来源:sockets.cpp


示例17: EVP_EncryptUpdate

void ARC4::UpdateData(int len, uint8 *data)
{
    int outlen = 0;
    EVP_EncryptUpdate(&m_ctx, data, &outlen, data, len);
    EVP_EncryptFinal_ex(&m_ctx, data, &outlen);
}
开发者ID:1024wow,项目名称:TrinityCore,代码行数:6,代码来源:ARC4.cpp


示例18: crypto_des3_encrypt

void crypto_des3_encrypt(CryptoDes3 des3, uint32 length, const uint8* in_data, uint8* out_data)
{
	int len;
	EVP_EncryptUpdate(&des3->des3_ctx, out_data, &len, in_data, length);
}
开发者ID:HawthorneUK,项目名称:RDP-Replay,代码行数:5,代码来源:crypto.c


示例19: AesGcm128Translate

// Translates |input| of |input_len| using aes-gcm-128. The |input| will either
// be encrypted or decrypted based on |direction|. The |key|, which must be of
// size |kAesGcmKeyBytes|, and the |nonce|, which must be of size
// |kAesGcmNonceBytes|, will be used for the translation. A new zend string will
// be returned on success, or NULL (with a visible warning) on failure.
static zend_string* AesGcm128Translate(
    int direction, char* input, size_t input_len, char* key, char* nonce) {
  const EVP_CIPHER* aead = EVP_aes_128_gcm();

  zend_string* result = NULL;

  int expected_len = 0;
  int result_len = 0;

  EVP_CIPHER_CTX context;
  EVP_CIPHER_CTX_init(&context);

  do {
    if (direction == TRANSLATE_ENCRYPT) {
      if (EVP_EncryptInit_ex(&context, aead, 0, 0, 0) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateInitGcmError);
        break;
      }

      if (EVP_CIPHER_CTX_ctrl(&context, EVP_CTRL_GCM_SET_IVLEN, 12, 0) != 1 ||
          EVP_EncryptInit_ex(&context, 0, 0, key, nonce) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateKeyNonceError);
        break;
      }

      expected_len = input_len + 16 /* authentication tag */;
      result = zend_string_alloc(expected_len, 0);
      if (!result) {
        php_error_docref(NULL, E_ERROR, kTranslateAllocationError);
        break;
      }

      if (EVP_EncryptUpdate(&context, result->val, &result_len, input, input_len) != 1 ||
          EVP_EncryptFinal_ex(&context, result->val, &result_len) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateEncryptInputError);
        zend_string_release(result);
        result = NULL;
        break;
      }

      if (EVP_CIPHER_CTX_ctrl(&context, EVP_CTRL_GCM_GET_TAG, 16, result->val + input_len) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateEncryptAuthError);
        zend_string_release(result);
        result = NULL;
        break;
      }

      // Encryption successful!

    } else {
      if (EVP_DecryptInit_ex(&context, aead, 0, 0, 0) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateInitGcmError);
        break;
      }

      expected_len = input_len - 16;

      if (EVP_CIPHER_CTX_ctrl(&context, EVP_CTRL_GCM_SET_TAG, 16, input + expected_len) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateDecryptAuthError);
        break;
      }

      if (EVP_CIPHER_CTX_ctrl(&context, EVP_CTRL_GCM_SET_IVLEN, 12, 0) != 1 ||
          EVP_DecryptInit_ex(&context, 0, 0, key, nonce) != 1) {
        php_error_docref(NULL, E_ERROR, kTranslateKeyNonceError);
        break;
      }

      result = zend_string_alloc(expected_len, 0);
      if (!result) {
        php_error_docref(NULL, E_ERROR, kTranslateAllocationError);
        break;
      }

      if (EVP_DecryptUpdate(&context, result->val, &result_len, input, expected_len) != 1 ||
          EVP_DecryptFinal_ex(&context, result->val + expected_len, &result_len) != 1) {
        php_error_docref(NULL, E_WARNING, kTranslateDecryptionWarning);

        zend_string_release(result);
        result = NULL;
        break;
      }

      // Decryption successful!
    }
  } while(0);

  EVP_CIPHER_CTX_cleanup(&context);

  return result;
}
开发者ID:beverloo,项目名称:php-ece,代码行数:96,代码来源:php_ece.c


示例20: create_dkek_share

static void create_dkek_share(sc_card_t *card, const char *outf, int iter, char *password, int password_shares_threshold, int password_shares_total)
{
	EVP_CIPHER_CTX ctx;
	FILE *out = NULL;
	u8 filebuff[64], key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH],outbuff[64];
	u8 dkek_share[32];
	char *pwd = NULL;
	int r = 0, outlen, pwdlen = 0;

	if (password == NULL) {

		if (password_shares_threshold == -1) {
			ask_for_password(&pwd, &pwdlen);
		} else { // create password using threshold scheme
			r = generate_pwd_shares(card, &pwd, &pwdlen, password_shares_threshold, password_shares_total);
		}

	} else {
		pwd = password;
		pwdlen = strlen(password);
	}

	if (r < 0) {
		printf("Creating DKEK share failed");
		return;
	}

	memcpy(filebuff, magic, sizeof(magic) - 1);

	r = sc_get_challenge(card, filebuff + 8, 8);
	if (r < 0) {
		printf("Error generating random number failed with ", sc_strerror(r));
		return;
	}

	printf("Enciphering DKEK share, please wait...\n");
	EVP_BytesToKey(EVP_aes_256_cbc(), EVP_md5(), filebuff + 8, (u8 *)pwd, pwdlen, iter, key, iv);

	if (password == NULL) {
		OPENSSL_cleanse(pwd, pwdlen);
		free(pwd);
	}

	r = sc_get_challenge(card, dkek_share, sizeof(dkek_share));
	if (r < 0) {
		printf("Error generating random number failed with ", sc_strerror(r));
		return;
	}

	EVP_CIPHER_CTX_init(&ctx);
	EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key, iv);
	if (!EVP_EncryptUpdate(&ctx, filebuff + 16, &outlen, dkek_share, sizeof(dkek_share))) {
		printf("Error encrypting DKEK share\n");
		return;
	}

	if (!EVP_EncryptFinal_ex(&ctx, filebuff + 16 + outlen, &r)) {
		printf("Error encrypting DKEK share\n");
		return;
	}

	out = fopen(outf, "wb");

	if (out == NULL) {
		perror(outf);
		return;
	}

	if (fwrite(filebuff, 1, sizeof(filebuff), out) != sizeof(filebuff)) {
		perror(outf);
		return;
	}

	fclose(out);

	OPENSSL_cleanse(filebuff, sizeof(filebuff));
	EVP_CIPHER_CTX_cleanup(&ctx);

	printf("DKEK share created and saved to %s\n", outf);
}
开发者ID:bartoreebbo,项目名称:OpenSC,代码行数:80,代码来源:sc-hsm-tool.c



注:本文中的EVP_EncryptUpdate函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ EVP_MD_CTX_cleanup函数代码示例发布时间:2022-05-30
下一篇:
C++ EVP_EncryptInit_ex函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap