本文整理汇总了C++中EVP_DecryptUpdate函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_DecryptUpdate函数的具体用法?C++ EVP_DecryptUpdate怎么用?C++ EVP_DecryptUpdate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_DecryptUpdate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: generate_key
/** Decrypt a buffer.
* @param cipher cipher ID
* @param enc encrypted buffer
* @param enc_size number of bytes in @p enc
* @param plain on return contains plain text data
* @param plain_size size in bytes of @p plain
* @return number of bytes that were in the encrypted buffer (this can be shorter if the data
* did not exactly fit the AES block size.
*/
size_t
BufferDecryptor::decrypt(int cipher, const void *enc, size_t enc_size, void *plain, size_t plain_size)
{
#ifdef HAVE_LIBCRYPTO
if (keys_.find(cipher) == keys_.end()) {
generate_key(cipher);
}
const EVP_CIPHER *evp_cipher = cipher_by_id(cipher);
const size_t iv_size = EVP_CIPHER_iv_length(evp_cipher);
const unsigned char *iv = (const unsigned char *)enc;
unsigned char *enc_m = (unsigned char *)enc + iv_size;
enc_size -= iv_size;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if ( ! EVP_DecryptInit(ctx, evp_cipher, (const unsigned char *)keys_[cipher].c_str(), iv))
{
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Could not initialize cipher context");
}
int outl = plain_size;
if ( ! EVP_DecryptUpdate(ctx,
(unsigned char *)plain, &outl, enc_m, enc_size))
{
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("DecryptUpdate failed");
}
int plen = 0;
if ( ! EVP_DecryptFinal(ctx, (unsigned char *)plain + outl, &plen) ) {
EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("DecryptFinal failed");
}
outl += plen;
EVP_CIPHER_CTX_free(ctx);
return outl;
#else
throw std::runtime_error("Decryption support not available");
#endif
}
示例2: spider_decrypt
int spider_decrypt(void) {
unsigned char outbuf[LOG_MAX];
int olen,tlen,n;
char inbuff[LOG_MAX + EVP_MAX_BLOCK_LENGTH];
char baz[LOG_MAX + EVP_MAX_BLOCK_LENGTH];
EVP_CIPHER_CTX ctx;
int i;
// hmmm. how are we going to chop this back into lines and populate
// the log array?
if (logfp) {
fclose(logfp);
}
logfp = fopen(LogPath, "rb");
if (logfp == NULL) {
fprintf(stderr, "logfp: %s\n", strerror(errno));
exit(1);
}
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit(&ctx, EVP_bf_cbc(), KEY, IV);
while ((n = fread(inbuff, 1, LOG_MAX + EVP_MAX_BLOCK_LENGTH, logfp)) > 0) {
if (EVP_DecryptUpdate(&ctx, outbuf, &olen, inbuff, n) != 1) {
return 0;
}
snprintf(baz, olen+1, "%s", outbuf);
bzero(&inbuff, LOG_MAX + EVP_MAX_BLOCK_LENGTH);
}
if ((i = EVP_DecryptFinal(&ctx, outbuf+olen, &tlen)) != 1) {
return 0;
}
bzero(baz, sizeof(baz));
snprintf(baz, tlen+1, "%s", outbuf+olen);
printf("%s", baz);
EVP_CIPHER_CTX_cleanup(&ctx);
return 1;
}
示例3: EVP_Update_loop
static int EVP_Update_loop(void *args)
{
loopargs_t *tempargs = *(loopargs_t **)args;
unsigned char *buf = tempargs->buf;
EVP_CIPHER_CTX *ctx = tempargs->ctx;
int outl, count;
if (decrypt)
for (count = 0; COND(nb_iter); count++)
EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
else
for (count = 0; COND(nb_iter); count++)
EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
if (decrypt)
EVP_DecryptFinal_ex(ctx, buf, &outl);
else
EVP_EncryptFinal_ex(ctx, buf, &outl);
return count;
}
示例4: decrypt_file
int decrypt_file(const char *path, const char *fileName, unsigned char *key) {
int outlen, inlen;
FILE *file, *temp;
file = fopen(fileName,"r+b");
if(file == NULL) {
return -2;
}
char tempName[PATH_SIZE];
cnct(path,"TemP.TemP",tempName);
temp = fopen(tempName,"wb");
unsigned char iv[8] = "DAJ-7l2"; /* вектор инициализации */
unsigned char inbuf[BUF_SIZE], outbuf[BUF_SIZE];
EVP_CIPHER_CTX ctx;
const EVP_CIPHER * cipher;
EVP_CIPHER_CTX_init(&ctx);
cipher = EVP_bf_cbc();
EVP_DecryptInit(&ctx, cipher, key, iv);
while(1) {
inlen = fread(inbuf, 1, BUF_SIZE, file);
if(inlen <= 0) break;
if(!EVP_DecryptUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
}
if(!EVP_DecryptFinal(&ctx, outbuf, &outlen))
return 1;
fwrite(outbuf, 1, outlen, temp);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(file);
fclose(temp);
remove(fileName);
rename(tempName,fileName);
return 0;
}
示例5: decrypt
int decrypt(const char *passphrase)
{
unsigned char key[EVP_MAX_KEY_LENGTH];
unsigned char iv[EVP_MAX_IV_LENGTH];
unsigned char salt[8] = {0};
unsigned char inbuf[256];
unsigned char outbuf[256 + EVP_MAX_BLOCK_LENGTH];
int nread, nwrite;
int outlen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
genKeyIV(0, passphrase, salt, key, iv);
EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key, iv);
for (;;) {
if ((nread = read(STDIN_FILENO, inbuf, 255)) < 0) {
perror("Fail to read");
return -1;
} else if (nread == 0)
break;
if (!EVP_DecryptUpdate(&ctx, outbuf, &outlen, inbuf, nread)) {
fprintf(stderr, "Fail to EncryptUpdate!\n");
EVP_CIPHER_CTX_cleanup(&ctx);
return -1;
}
if ((nwrite = write(STDOUT_FILENO, outbuf, outlen)) < 0) {
perror("Fail to write");
return -1;
}
}
if (!EVP_DecryptFinal_ex(&ctx, outbuf, &outlen)) {
EVP_CIPHER_CTX_cleanup(&ctx);
return -1;
}
if (write(STDOUT_FILENO, outbuf, outlen) < 0) {
perror("Fail to write");
return -1;
}
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
示例6: decrypt_str
int decrypt_str(unsigned char *buffer, const char *data, size_t dataLen, const char *key, size_t *outLen) {
EVP_CIPHER_CTX *ctx;
// TODO Bad
unsigned char iv[16] = {0};
int outlen1, outlen2;
if (!(ctx = EVP_CIPHER_CTX_new()))
return 0;
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1)
return 2;
if (EVP_DecryptUpdate(ctx, buffer, &outlen1, data, dataLen) != 1)
return 3;
if (EVP_DecryptFinal_ex(ctx, buffer + outlen1, &outlen2) != 1)
return 4;
*outLen = outlen1 + outlen2;
return 1;
}
示例7: decrypt
int decrypt(unsigned char *ciphertext,
int ciphertext_len,
unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/* Initialise the decryption 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_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if (1 !=EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext,ciphertext_len))
handleErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
示例8: decrypt
int
decrypt (int infd, int outfd)
{
unsigned char outbuf[IP_SIZE];
int olen, tlen, n;
unsigned char inbuff[OP_SIZE];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
//EVP_CIPHER_CTX_init (ctx);
EVP_DecryptInit (ctx, EVP_bf_cbc (), key, iv);
for (;;)
{
bzero (&inbuff, OP_SIZE);
if ((n = read (infd, inbuff, OP_SIZE)) == -1)
{
perror ("read error");
break;
}
else if (n == 0)
break;
bzero (&outbuf, IP_SIZE);
if (EVP_DecryptUpdate (ctx, outbuf, &olen, inbuff, n) != 1)
{
printf ("error in decrypt update\n");
return 0;
}
if (EVP_DecryptFinal (ctx, outbuf + olen, &tlen) != 1)
{
printf ("error in decrypt final\n");
return 0;
}
olen += tlen;
if ((n = write (outfd, outbuf, olen)) == -1)
perror ("write error");
}
//EVP_CIPHER_CTX_cleanup (ctx);
return 1;
}
示例9: aesDecrypt
size32_t aesDecrypt(MemoryBuffer &out, size32_t inSz, const void *inBytes, size32_t keyLen, const char *key, const char *iv)
{
if (0 == inSz)
return 0;
OwnedEVPCipherCtx ctx(EVP_CIPHER_CTX_new());
if (!ctx)
throw makeEVPException(0, "Failed EVP_CIPHER_CTX_new");
const size32_t cipherBlockSz = 128;
// from man page - "should have sufficient room for (inl + cipher_block_size) bytes unless the cipher block size is 1 in which case inl bytes is sufficient"
size32_t outMaxSz = (cipherBlockSz==1) ? inSz : (inSz + cipherBlockSz/8);
size32_t startSz = out.length();
byte *outPtr = (byte *)out.reserveTruncate(outMaxSz);
/* Initialise the decryption 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_DecryptInit_ex(ctx, getAesCipher(keyLen), nullptr, (const unsigned char *)key, (const unsigned char *)iv))
throw makeEVPException(0, "Failed EVP_DecryptInit_ex");
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
int outSz;
if (1 != EVP_DecryptUpdate(ctx, outPtr, &outSz, (const unsigned char *)inBytes, inSz))
throw makeEVPException(0, "Failed EVP_DecryptUpdate");
int plaintext_len = outSz;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if (1 != EVP_DecryptFinal_ex(ctx, outPtr + outSz, &outSz))
throw makeEVPException(0, "Failed EVP_DecryptFinal_ex");
plaintext_len += outSz;
out.setLength(startSz+plaintext_len); // truncate length of 'out' to final size
return (size32_t)plaintext_len;
}
示例10: malloc
//The partner decryption function to above
unsigned char *blowfish_dec(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_DecryptInit_ex(&ctx, EVP_bf_ecb(), NULL, key, iv);
EVP_CIPHER_CTX_set_padding(&ctx, 0);
EVP_DecryptUpdate(&ctx, out, &outlen, data, size);
if(!EVP_DecryptFinal_ex(&ctx, out + outlen, &tmplen)) {
ssl_error("Didn't do decrypt final");
}
outlen += tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
return out;
}
示例11: aes_decrypt
array::array* aes_decrypt(array::array* data, array::array* iv, array::array* key) {
byte* ivec = new byte[iv->length];
memcpy(ivec, iv->data, iv->length);
byte* outdata = new byte[data->length * 2];
int outLen1 = 0, outLen2 = 0;
EVP_CIPHER_CTX ctx;
EVP_DecryptInit(&ctx,EVP_aes_256_cbc(),key->data,ivec);
EVP_DecryptUpdate(&ctx,outdata, &outLen1, data->data, data->length);
EVP_DecryptFinal(&ctx,outdata + outLen1, &outLen2);
delete[] ivec;
array::array* lol = array::create(outLen1 + outLen2, outdata);
delete[] outdata;
return lol;
}
示例12: CryptUnprotectMemory
BOOL CryptUnprotectMemory(LPVOID pData, DWORD cbData, DWORD dwFlags)
{
BYTE* pPlainText;
int cbOut, cbFinal;
WINPR_PROTECTED_MEMORY_BLOCK* pMemBlock;
if (dwFlags != CRYPTPROTECTMEMORY_SAME_PROCESS)
return FALSE;
if (!g_ProtectedMemoryBlocks)
return FALSE;
pMemBlock = (WINPR_PROTECTED_MEMORY_BLOCK*) ListDictionary_GetItemValue(g_ProtectedMemoryBlocks, pData);
if (!pMemBlock)
return FALSE;
/* AES Decryption */
cbOut = pMemBlock->cbData + AES_BLOCK_SIZE - 1;
pPlainText = (BYTE*) malloc(cbOut);
EVP_DecryptInit_ex(&(pMemBlock->dec), NULL, NULL, NULL, NULL);
EVP_DecryptUpdate(&(pMemBlock->dec), pPlainText, &cbOut, pMemBlock->pData, pMemBlock->cbData);
EVP_DecryptFinal_ex(&(pMemBlock->dec), pPlainText + cbOut, &cbFinal);
CopyMemory(pMemBlock->pData, pPlainText, pMemBlock->cbData);
SecureZeroMemory(pPlainText, pMemBlock->cbData);
free(pPlainText);
ListDictionary_Remove(g_ProtectedMemoryBlocks, pData);
/* AES Cleanup */
EVP_CIPHER_CTX_cleanup(&(pMemBlock->enc));
EVP_CIPHER_CTX_cleanup(&(pMemBlock->dec));
free(pMemBlock);
return TRUE;
}
示例13: decipher_evp
static char * decipher_evp (const unsigned char *key, int keylen, const unsigned char *ciphertext, int cipherlen, const EVP_CIPHER *type, int *outlen, int ivsize)
{
unsigned char *outbuf;
unsigned char *iv = NULL;
unsigned long errcode;
int outlen2;
EVP_CIPHER_CTX a;
EVP_CIPHER_CTX_init(&a);
EVP_CIPHER_CTX_set_padding(&a, 0);
if (ivsize > 0)
iv = new_malloc(ivsize);
outbuf = new_malloc(cipherlen + 1024);
if (ivsize > 0)
memcpy(iv, ciphertext, ivsize);
EVP_DecryptInit_ex(&a, type, NULL, NULL, iv);
EVP_CIPHER_CTX_set_key_length(&a, keylen);
EVP_CIPHER_CTX_set_padding(&a, 0);
EVP_DecryptInit_ex(&a, NULL, NULL, key, NULL);
if (EVP_DecryptUpdate(&a, outbuf, outlen, ciphertext, cipherlen) != 1)
yell("EVP_DecryptUpdate died.");
if (EVP_DecryptFinal_ex(&a, outbuf + (*outlen), &outlen2) != 1)
yell("EVP_DecryptFinal_Ex died.");
*outlen += outlen2;
EVP_CIPHER_CTX_cleanup(&a);
ERR_load_crypto_strings();
while ((errcode = ERR_get_error()))
{
char r[256];
ERR_error_string_n(errcode, r, 256);
yell("ERROR: %s", r);
}
if (ivsize > 0)
new_free(&iv);
return outbuf;
}
示例14: decrypt_sym
int decrypt_sym(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) handleErrors();
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) handleErrors();
plaintext_len = len;
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
示例15: codec_aes_decrypt
/**
* AES-ECB-PKCS5Padding解密
*
* LUA示例:
* local codec = require('codec')
* local src = [[...]] --BASE64密文
* local key = [[...]] --16位数字串
* local bs = codec.base64_decode(src)
* local dst = codec.aes_decrypt(bs, key)
*/
static int codec_aes_decrypt(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_DecryptInit_ex(&ctx, EVP_aes_128_ecb(), NULL, (unsigned char *)key, NULL);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP decrypt init error");
}
int n, wn;
char dst[len];
memset(dst, 0, len);
ret = EVP_DecryptUpdate(&ctx, (unsigned char *)dst, &wn, (unsigned char *)src, len);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP decrypt update error");
}
n = wn;
ret = EVP_DecryptFinal_ex(&ctx, (unsigned char *)(dst + n), &wn);
if(ret != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return luaL_error(L, "EVP decrypt final error");
}
EVP_CIPHER_CTX_cleanup(&ctx);
n += wn;
lua_pushlstring(L, dst, n);
return 1;
}