本文整理汇总了C++中EVP_EncryptUpdate函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_EncryptUpdate函数的具体用法?C++ EVP_EncryptUpdate怎么用?C++ EVP_EncryptUpdate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_EncryptUpdate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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);
}
示例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;
}
}
示例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;
}