本文整理汇总了C++中EVP_EncryptInit_ex函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_EncryptInit_ex函数的具体用法?C++ EVP_EncryptInit_ex怎么用?C++ EVP_EncryptInit_ex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_EncryptInit_ex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: malloc
//An encryption function that uses blowish and a variable length key to encrypt
//'size' bytes of data. This function is only really used to encrypt verification
//digests of files
unsigned char *blowfish_enc(unsigned char *key, unsigned char *data, int size)
{
unsigned char* out = malloc(size);
int outlen;
int tmplen;
unsigned char iv[] = {0}; //TODO maybe not this?
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_bf_ecb(), NULL, key, iv);
EVP_CIPHER_CTX_set_padding(&ctx, 0);
EVP_EncryptUpdate(&ctx, out, &outlen, data, size);
if(!EVP_EncryptFinal_ex(&ctx, out + outlen, &tmplen)) {
ssl_error("Didn't do encrypt final");
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
return out;
}
示例2: srtp_aes_icm_openssl_set_iv
/*
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
* the offset
*/
static srtp_err_status_t srtp_aes_icm_openssl_set_iv (void *cv, uint8_t *iv, srtp_cipher_direction_t dir)
{
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
v128_t nonce;
/* set nonce (for alignment) */
v128_copy_octet_string(&nonce, iv);
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
v128_xor(&c->counter, &c->offset, &nonce);
debug_print(srtp_mod_aes_icm, "set_counter: %s", v128_hex_string(&c->counter));
if (!EVP_EncryptInit_ex(c->ctx, NULL,
NULL, NULL, c->counter.v8)) {
return srtp_err_status_fail;
} else {
return srtp_err_status_ok;
}
}
示例3: aes_init
/**
* Create an 256 bit key and IV using the supplied key_data. salt can be added for taste.
* Fills in the encryption and decryption ctx objects and returns 0 on success
**/
int aes_init(unsigned char *key_data, int key_data_len, unsigned char *salt, EVP_CIPHER_CTX *e_ctx)
{
int i, nrounds = 5;
unsigned char key[32], iv[32];
/*
* Gen key & IV for AES 256 CBC mode. A SHA1 digest is used to hash the supplied key material.
* nrounds is the number of times the we hash the material. More rounds are more secure but
* slower.
*/
i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, key_data, key_data_len, nrounds, key, iv);
if (i != 32) {
printf("Key size is %d bits - should be 256 bits\n", i);
return -1;
}
EVP_CIPHER_CTX_init(e_ctx);
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), NULL, key, iv);
return 0;
}
示例4: aes_encrypt_message
/*
* Encrypts the plaintext and sets it to out
* @param message: the plaintext string
* @param length: the number of chars for the plaintext
* @param encKey: the aes 256 key
* @param encIv: the aes 128 iv
* @param out: where the enc message is put
*/
unsigned int aes_encrypt_message(unsigned char *message, unsigned int length, unsigned char *encKey, unsigned char *encIv, unsigned char **out) {
unsigned char *encMsg = (unsigned char *)malloc(length*(unsigned char) + AES_BLOCK_SIZE);
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
if (!(ctx = EVP_CIPHER_CTX_new())) {
printf ("EVP_CIPHER_CTX_new() failed\n");
exit (EXIT_FAILURE);
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, encKey, encIv)) {
printf ("EVP_EncryptInit_ex failed\n");
exit (EXIT_FAILURE);
}
if (1 != EVP_EncryptUpdate(ctx, encMsg, &len, message, length)) {
printf ("EVP_ENcryptUpdate failed\n");
exit (EXIT_FAILURE);
}
ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, encMsg + len, &len)) {
printf ("EVP_EncryptFinal_ex() failed\n");
exit (EXIT_FAILURE);
}
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
*(encMsg + ciphertext_len) = '\0';
if (Base64Encode(encMsg, ciphertext_len, (char **)out) < 0) {
printf ("Base64Encode in aes_encrypt_message failed\n");
}
return ciphertext_len;
}
示例5: codec_aes_encrypt
/**
* AES-ECB-PKCS5Padding加密
*
* LUA示例:
* local codec = require('codec')
* local src = 'something'
* local key = [[...]] --16位数字串
* local bs = codec.aes_encrypt(src, key)
* local dst = codec.base64_encode(bs) --BASE64密文
*/
static int codec_aes_encrypt(lua_State *L)
{
size_t len;
const char *src = luaL_checklstring(L, 1, &len);
char *key = luaL_checkstring(L, 2);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int ret = EVP_EncryptInit_ex(&ctx, EVP_aes_128_ecb(), NULL, (unsigned char *)key, NULL);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP encrypt init error");
}
int dstn = len + 128, n, wn;
char dst[dstn];
memset(dst, 0, dstn);
ret = EVP_EncryptUpdate(&ctx, (unsigned char *)dst, &wn, (unsigned char *)src, len);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP encrypt update error");
}
n = wn;
ret = EVP_EncryptFinal_ex(&ctx, (unsigned char *)(dst + n), &wn);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP encrypt final error");
}
EVP_CIPHER_CTX_cleanup(&ctx);
n += wn;
lua_pushlstring(L, dst, n);
return 1;
}
示例6: encrypt
static int encrypt(void *plaintext, size_t plaintext_len, void *key, void *iv, void *ciphertext) {
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
// initialize the CryptoExcreter
if(!(ctx = EVP_CIPHER_CTX_new())) {
secreteLibSSLError();
}
/* 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)) {
secreteLibSSLError();
}
// encrypt pls
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) {
secreteLibSSLError();
}
ciphertext_len = len;
// finalize
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
secreteLibSSLError();
}
ciphertext_len += len;
// Clean up
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
示例7: encrypt
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;
/* 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))
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, plaintext_len))
handleErrors();
ciphertext_len = len;
/*
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;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
示例8: TripleDESDecrypt
/**
* 功能描述:3DES解密
* @param pData:原始数据
* @param ilen: 原始数据长度
* @param ppDecryptData: 解密后数据
* @return -1: 失败, 其他: 解密数据长度
**/
int TripleDESDecrypt(const char* pData, int ilen, char** ppDecryptData)
{
/*密钥*/
unsigned char key[24] = {43,14,54,109,109,8,84,87,116,30,19,68,35,51,83,72,16,2,83,48,117,85,9,80};
/*初始化向量*/
unsigned char iv[8] = {111,121,47,42,75,34,33,124};
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int rc = EVP_EncryptInit_ex(&ctx,EVP_des_ede3_cbc(),NULL,key,iv);
if (rc != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return -1;
}
int outlen = ilen + 1;
*ppDecryptData = (char*)malloc(outlen);
memset(*ppDecryptData, 0 , outlen);
rc = EVP_DecryptUpdate(&ctx, (unsigned char*)(*ppDecryptData), &outlen, (unsigned char*)pData, ilen);
if(rc != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
free(*ppDecryptData);
return -1;
}
int outlentmp = 0;
rc = EVP_DecryptFinal_ex(&ctx, (unsigned char*)(*ppDecryptData) + outlen,&outlentmp);
if(rc != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
free(*ppDecryptData);
return -1;
}
outlen += outlentmp;
EVP_CIPHER_CTX_cleanup(&ctx);
return outlen;
}
示例9: aesEncrypt
size32_t aesEncrypt(MemoryBuffer &out, size32_t inSz, const void *inBytes, size32_t keyLen, const char *key, const char iv[aesBlockSize])
{
if (0 == inSz)
return 0;
OwnedEVPCipherCtx ctx(EVP_CIPHER_CTX_new());
if (!ctx)
throw makeEVPException(0, "Failed EVP_CIPHER_CTX_new");
/* 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 (!iv) iv = staticAesIV;
if (1 != EVP_EncryptInit_ex(ctx, getAesCipher(keyLen), nullptr, (const unsigned char *)key, (const unsigned char *)iv))
throw makeEVPException(0, "Failed EVP_EncryptInit_ex");
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
const size32_t cipherBlockSz = 128;
size32_t outMaxSz = inSz + cipherBlockSz/8;
size32_t startSz = out.length();
byte *outPtr = (byte *)out.reserveTruncate(outMaxSz);
int outSz;
if (1 != EVP_EncryptUpdate(ctx, (unsigned char *)outPtr, &outSz, (unsigned char *)inBytes, inSz))
throw makeEVPException(0, "Failed EVP_EncryptUpdate");
int ciphertext_len = outSz;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, outPtr + outSz, &outSz))
throw makeEVPException(0, "Failed EVP_EncryptFinal_ex");
ciphertext_len += outSz;
out.setLength(startSz+ciphertext_len); // truncate length of 'out' to final size
return (size32_t)ciphertext_len;
}
示例10: handle_crypt_error
int Crypt::enc_data(uint8_t *plain_text, int plain_text_len, uint8_t *cipher_text, uint64_t k_nas_enc) {
EVP_CIPHER_CTX *ctx;
int len;
int cipher_text_len;
if (!(ctx = EVP_CIPHER_CTX_new())) {
handle_crypt_error();
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
handle_crypt_error();
}
if (1 != EVP_EncryptUpdate(ctx, cipher_text, &len, plain_text, plain_text_len)) {
handle_crypt_error();
}
cipher_text_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, cipher_text + len, &len)) {
handle_crypt_error();
}
cipher_text_len += len;
EVP_CIPHER_CTX_free(ctx);
return cipher_text_len;
}
示例11: encrypt_data
int
encrypt_data(const void *in, int inlen, void *out, int *outlen,
const void *iv, const void *key)
{
int len;
ERR_clear_error();
cryptutil_init();
/* In this 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(EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) {
log_crypt_err("Not able to initialize encrypt context for AES-256");
return 0;
}
/* Provide the message to be encrypted, and obtain the encrypted output */
if(EVP_EncryptUpdate(&ctx, out, &len, in, inlen) != 1) {
log_crypt_err("Not able to encrypt using AES-256");
return 0;
}
*outlen = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage
*/
if(EVP_EncryptFinal_ex(&ctx, out + len, &len) != 1) {
log_crypt_err("Not able to finalize encryption using AES-256");
return 0;
}
*outlen += len;
return 1;
}
示例12: aes_icm_openssl_set_iv
/*
* aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
* the offset
*/
err_status_t aes_icm_openssl_set_iv (aes_icm_ctx_t *c, void *iv, int dir)
{
const EVP_CIPHER *evp;
v128_t nonce;
/* set nonce (for alignment) */
v128_copy_octet_string(&nonce, iv);
debug_print(mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
v128_xor(&c->counter, &c->offset, &nonce);
debug_print(mod_aes_icm, "set_counter: %s", v128_hex_string(&c->counter));
switch (c->key_size) {
case AES_256_KEYSIZE:
evp = EVP_aes_256_ctr();
break;
#ifndef BORINGSSL
case AES_192_KEYSIZE:
evp = EVP_aes_192_ctr();
break;
#endif
case AES_128_KEYSIZE:
evp = EVP_aes_128_ctr();
break;
default:
return err_status_bad_param;
break;
}
if (!EVP_EncryptInit_ex(&c->ctx, evp,
NULL, c->key.v8, c->counter.v8)) {
return err_status_fail;
} else {
return err_status_ok;
}
}
示例13: EVP_EncryptInit_ex
bool CryptFileDevice::flush()
{
if (!m_encrypted)
return false;
if (m_wasFlushed)
return true;
if (m_buffer.isEmpty())
return false;
m_wasFlushed = true;
int len = m_buffer.length();
int maxCipherLen = len + AES_BLOCK_SIZE - (len % AES_BLOCK_SIZE) + AES_BLOCK_SIZE;
int finalLen = 0;
unsigned char *cipherText = new unsigned char[maxCipherLen];
EVP_EncryptInit_ex(&m_encCtx, NULL, NULL, NULL, NULL);
EVP_EncryptUpdate(&m_encCtx, cipherText, &maxCipherLen, (unsigned char *)m_buffer.data(), len);
EVP_EncryptFinal_ex(&m_encCtx, &cipherText[maxCipherLen], &finalLen);
len = maxCipherLen;
if (m_device->pos() >= m_device->size())
len += finalLen;
m_device->write((char *)cipherText, len);
delete[] cipherText;
m_blockFlush = true;
seek(pos() + m_buffer.length());
m_blockFlush = false;
m_wasSought = false;
m_buffer.clear();
return true;
}
示例14: EVP_CIPHER_CTX_init
int s3fs::Crypto::encrypt_block(const unsigned char plain[], int inlen, unsigned char outbuf[])
{
int outlen;
int tmplen;
EVP_CIPHER_CTX_init(&ctx);
EVP_CIPHER_CTX_set_padding(&ctx, 1L);
EVP_EncryptInit_ex(&ctx, EVP_aes_256_ctr() , NULL, key, iv);
if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, plain, inlen))
{
cerr << "An error has occurred while encrypting the plain text." << endl;
EVP_CIPHER_CTX_cleanup(&ctx);
}
if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen))
{
cerr << "An error has occurred while encrypting the plain text." << endl;
EVP_CIPHER_CTX_cleanup(&ctx);
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
return outlen;
}
示例15: EVP_BytesToKey
bool AESCipher::init2(unsigned char *key_data, int key_data_len)
{
int i, nrounds = 1;
unsigned char key[32], iv[32];
/*
* Gen key & IV for AES 256 CBC mode. A SHA1 digest is used to hash the supplied key material.
* nrounds is the number of times the we hash the material. More rounds are more secure but
* slower.
*/
i = EVP_BytesToKey(EVP_aes_256_cfb(), EVP_md5(), NULL, key_data, key_data_len, nrounds, key, iv);
if (i != 32) {
//printf("Key size is %d bits - should be 256 bits/n", i);
return false;
}
EVP_CIPHER_CTX_init(&m_ectx);
EVP_EncryptInit_ex(&m_ectx, EVP_aes_256_cfb(), NULL, key, iv);
EVP_CIPHER_CTX_init(&m_dctx);
EVP_DecryptInit_ex(&m_dctx, EVP_aes_256_cfb(), NULL, key, iv);
return true;
}