本文整理匯總了C++中EVP_CipherFinal_ex函數的典型用法代碼示例。如果您正苦於以下問題:C++ EVP_CipherFinal_ex函數的具體用法?C++ EVP_CipherFinal_ex怎麽用?C++ EVP_CipherFinal_ex使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了EVP_CipherFinal_ex函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C++代碼示例。
示例1: main
int main(int N, char ** S)
{
unsigned char key[]= "helloworld" ;
unsigned char iv[]= "12345678" ;
char * destp ;
int iuse, iuse2 ;
EVP_CIPHER_CTX ctx;
OpenSSL_add_all_ciphers() ;
printf("test: (%s) len %zd.\n", test, strlen(test)) ;
EVP_CIPHER_CTX_init(&ctx) ;
// encode
EVP_CipherInit_ex(&ctx, EVP_bf_cbc(), NULL, NULL, NULL, 1);
EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, -1);
destp= buffer ;
EVP_CipherUpdate(&ctx, destp, &iuse, test, strlen(test) +1) ;
destp += iuse ;
EVP_CipherFinal_ex(&ctx, destp, &iuse) ;
destp += iuse ;
iuse= ( destp - buffer ) ;
EVP_CIPHER_CTX_cleanup(&ctx);
// decode
EVP_CipherInit_ex(&ctx, EVP_bf_cbc(), NULL, NULL, NULL, 0);
EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, -1);
destp= buffer2 ;
EVP_CipherUpdate(&ctx, destp, &iuse2, buffer, iuse ) ;
destp += iuse2 ;
EVP_CipherFinal_ex(&ctx, destp, &iuse2) ;
destp += iuse2 ;
iuse2= ( destp - buffer2 ) ;
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_cleanup() ;
encode_asc85(printbuf, sizeof(printbuf), test, strlen(test) +1) ;
printf("SRC: %s\n", printbuf) ;
encode_asc85(printbuf, sizeof(printbuf), buffer, iuse) ;
printf("ENC: %s\n", printbuf) ;
encode_asc85(printbuf, sizeof(printbuf), buffer2, iuse2) ;
printf("DEC: %s\nout: %s\n", printbuf, buffer2) ;
}
示例2: LUA_FUNCTION
static LUA_FUNCTION(openssl_evp_cipher_final)
{
EVP_CIPHER_CTX* c = CHECK_OBJECT(1, EVP_CIPHER_CTX, "openssl.evp_cipher_ctx");
char out[EVP_MAX_BLOCK_LENGTH];
int outl = sizeof(out);
CIPHER_MODE mode;
int ret = 0;
lua_rawgetp(L, LUA_REGISTRYINDEX, c);
mode = lua_tointeger(L, -1);
if (mode == DO_CIPHER)
ret = EVP_CipherFinal_ex(c, (byte*)out, &outl);
else if (mode == DO_ENCRYPT)
ret = EVP_EncryptFinal_ex(c, (byte*)out, &outl);
else if (mode == DO_DECRYPT)
ret = EVP_DecryptFinal_ex(c, (byte*)out, &outl);
else
luaL_error(L, "never go here");
lua_pop(L, 1);
if (ret == 1)
{
lua_pushlstring(L, out, outl);
return 1;
}
return openssl_pushresult(L, ret);
}
示例3: finalize
/*
return -1 if padding
@note don't use
*/
int finalize(char *outBuf)
{
int outLen = 0;
int ret = EVP_CipherFinal_ex(&ctx_, cybozu::cast<uint8_t*>(outBuf), &outLen);
if (ret != 1) return -1;
return outLen;
}
示例4: do_crypt
/********************do the cryption part*************************/
int do_crypt(unsigned char *key, unsigned char *iv, char *msg, int *l, int crypt) {
unsigned char outbuf[BUFSIZE + EVP_MAX_BLOCK_LENGTH];
int len = *l, outlen, tmplen, i;
unsigned char input[BUFSIZE];
memcpy(input, msg, len);
if (DEBUG) {
printf("\nbefore crypted payload: ");
for(i = 0; i < len; i++) printf("%02x", *(input+i));
putchar(10);
}
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv, crypt);
if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, input, len)) {
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
if(!EVP_CipherFinal_ex(&ctx, outbuf + outlen, &tmplen)) {
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += tmplen;
if (DEBUG) {
printf("\ncrypted payload: ");
for(i = 0; i < outlen; i++) printf("%02x", *(outbuf+i));
printf("\n");
}
memcpy(msg, outbuf, outlen);
*l = outlen;
EVP_CIPHER_CTX_cleanup(&ctx);
return 1;
}
示例5: crypt
int crypt(unsigned char *inbuf, int inlen, unsigned char *outbuf, unsigned char key[],unsigned char iv[], int do_encrypt)
{
int outlen, mlen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv, do_encrypt);
outlen = 0;
if(inlen <= 0) return 0;
if(!EVP_CipherUpdate(&ctx, outbuf + outlen, &mlen, inbuf, inlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += mlen;
if(!EVP_CipherFinal_ex(&ctx, outbuf + outlen, &mlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += mlen;
EVP_CIPHER_CTX_cleanup(&ctx);
return outlen;
}
示例6: EVP_CIPHER_CTX_init
bool CryptoBuffer::doCrypt(const char *in, int inlen, bool isEncrypt, QByteArray &out)
{
const int OUTBUF_SIZE = 8*1024;
unsigned char outbuf[OUTBUF_SIZE + EVP_MAX_BLOCK_LENGTH];
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
auto ctxGuard = makeGuard([&ctx] { EVP_CIPHER_CTX_cleanup(&ctx); });
if (!EVP_CipherInit_ex(&ctx, d->cipher, NULL, d->key, d->iv, isEncrypt))
return DEBUGRET(false, "EVP_CipherInit_Ex failed");
const unsigned char *ptr = reinterpret_cast<const unsigned char*>(in);
int restlen = inlen;
int outlen;
while (restlen > 0)
{
int readlen = std::min(restlen, OUTBUF_SIZE);
if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, ptr, readlen))
return DEBUGRET(false, "EVP_CipherUpdate failed");
out.append(reinterpret_cast<const char*>(outbuf), outlen);
ptr += readlen;
restlen -= readlen;
}
if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
return DEBUGRET(false, "EVP_CipherFinal_ex failed");
out.append(reinterpret_cast<const char*>(outbuf), outlen);
return true;
}
示例7: EVP_CipherInit_ex
HRESULT CBCipher::do_crypt(VARIANT varInput, VARIANT varOutput, VARIANT* pVal, int enc)
{
if(!m_ctx.cipher)return SetErrorInfo(s_strAlgoError);
if(m_iKeySize == EVP_CIPHER_key_length(m_ctx.cipher))
EVP_CipherInit_ex(&m_ctx, m_ctx.cipher, NULL, m_pKey, m_pIV, enc);
else
{
EVP_CipherInit_ex(&m_ctx, m_ctx.cipher, NULL, NULL, NULL, enc);
EVP_CIPHER_CTX_set_key_length(&m_ctx, m_iKeySize);
EVP_CipherInit_ex(&m_ctx, NULL, NULL, m_pKey, m_pIV, enc);
}
if(!m_bPadding)EVP_CIPHER_CTX_set_padding(&m_ctx, 0);
HRESULT hr;
CBComPtr<IStream> pSrcStream;
CBComPtr<IStream> pDescStream1;
IStream* pDescStream;
CBTempStream mStream;
hr = CBStream::GetStream(&varInput, &pSrcStream);
if(FAILED(hr))return hr;
if(varOutput.vt != VT_ERROR)
{
hr = CBStream::GetStream(&varOutput, &pDescStream1, FALSE);
if(FAILED(hr))return hr;
pDescStream = pDescStream1;
}else pDescStream = &mStream;
BYTE inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
ULONG inlen, outlen, wrlen;
while(1)
{
hr = pSrcStream->Read(inbuf, sizeof(inbuf), &inlen);
if(FAILED(hr))return hr;
if(inlen == 0)break;
if(!EVP_CipherUpdate(&m_ctx, outbuf, (int*)&outlen, inbuf, inlen))
return E_FAIL;
hr = pDescStream->Write(outbuf, outlen, &wrlen);
if(FAILED(hr))return hr;
}
if(!EVP_CipherFinal_ex(&m_ctx, outbuf, (int*)&outlen))
return E_FAIL;
hr = pDescStream->Write(outbuf, outlen, &wrlen);
if(FAILED(hr))return hr;
if(mStream.GetLength())
return mStream.GetVariant(pVal);
return S_OK;
}
示例8: OpenSSL_add_all_ciphers
bool Crypto_t::symmetricCipher(const std::vector<unsigned char>& in_buffer, int nid, const std::string& key, const std::string& iv, bool encode, std::vector<unsigned char>& buffer)
{
// load all cipher modules
OpenSSL_add_all_ciphers();
// Select the specific cipher module
const EVP_CIPHER* cipher = EVP_get_cipherbynid(nid);
if (!cipher) return false;
// Each cipher has its own taste for the key and IV. So we need to check if the input key and IV is appropriate
// for the specific cipher module
if (key.size() < static_cast<size_t>(EVP_CIPHER_key_length(cipher)) || iv.size() < static_cast<size_t>(EVP_CIPHER_iv_length(cipher)))
{
return false;
}
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, cipher, NULL, (const unsigned char*)key.c_str(), (const unsigned char*)iv.c_str(), encode);
size_t block_size = EVP_CIPHER_block_size(cipher);
unsigned char* encrypt_buffer = (unsigned char*) malloc(block_size + in_buffer.size());
// Read the raw buffer and convert to encrypt one. And then collect to the output buffer
int out_count = 0;
buffer.clear();
bool fail = false;
while (true)
{
if (!EVP_CipherUpdate(&ctx, encrypt_buffer, &out_count, &in_buffer[0], in_buffer.size()))
{
fail = true;
break;
}
for (int i = 0; i < out_count; i++)
buffer.push_back(encrypt_buffer[i]);
// handling the last block
unsigned char* block = encrypt_buffer + out_count;
if (!EVP_CipherFinal_ex(&ctx, block, &out_count))
{
fail = true;
break;
}
for (int i = 0; i < out_count; i++)
buffer.push_back(block[i]);
break;
}
// free resource
free(encrypt_buffer);
EVP_CIPHER_CTX_cleanup(&ctx);
return (fail == true) ? (false) : (true);
}
示例9: Encrypt_AesGcm128
//key:128
//encrypt output:nonce(12)+tag(16)+cipher_text
//no aad
//
int Encrypt_AesGcm128(const uint8_t * plain_text, uint32_t plain_text_len,
const SecureString & key, string & cipher_text){
cipher_text.clear();
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CIPHER * cipher=EVP_aes_128_gcm();
if(1 != EVP_EncryptInit_ex(&ctx, cipher, NULL, NULL, NULL))
return LIB_ERR;
unsigned char nonce[GCM_NONCE_LENGTH]={};
RAND_bytes(nonce,4);
struct timeval t={0,0};
if(0==gettimeofday(&t,NULL)){
memcpy(&nonce[4],&t.tv_sec,4);
memcpy(&nonce[8],&t.tv_nsec,4);
}else{
RAND_bytes(&nonce[4],GCM_NONCE_LENGTH-4);
}
if(1 != EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, GCM_NONCE_LENGTH, NULL))
return LIB_ERR;
if(1 != EVP_EncryptInit_ex(&ctx, NULL, NULL, key.data(), &nonce[0]))
return LIB_ERR;
cipher_text.reserve(GCM_NONCE_LENGTH+GCM_TAG_LENGTH+plain_text_len);
cipher_text.append(&nonce[0],GCM_NONCE_LENGTH);
cipher_text.append(GCM_TAG_LENGTH,0);//fill tag later
cipher_text.resize(GCM_NONCE_LENGTH+GCM_TAG_LENGTH+plain_text_len);
//do the real encryption.
int out_len=plain_text_len;
const int ret = EVP_CipherUpdate(&ctx,&cipher_text[GCM_NONCE_LENGTH+GCM_TAG_LENGTH],&out_len, plain_text,plain_text_len);
if(1!=ret){
cipher_text.clear();
return LIB_ERR;
}
out_len=0;
if(1 != EVP_CipherFinal_ex(&ctx,cipher_text.end(),out_len) ){
cipher_text.clear();
return LIB_ERR;
}
/* Get the tag */
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, &cipher_text[GCM_NONCE_LENGTH])){
cipher_text.clear();
return LIB_ERR;
}
return OK;
}
示例10: test_afalg_aes_128_cbc
static int test_afalg_aes_128_cbc(void)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher = EVP_aes_128_cbc();
unsigned char key[] = "\x5F\x4D\xCC\x3B\x5A\xA7\x65\xD6\
\x1D\x83\x27\xDE\xB8\x82\xCF\x99";
unsigned char iv[] = "\x2B\x95\x99\x0A\x91\x51\x37\x4A\
\xBD\x8F\xF8\xC5\xA7\xA0\xFE\x08";
unsigned char in[BUFFER_SIZE];
unsigned char ebuf[BUFFER_SIZE + 32];
unsigned char dbuf[BUFFER_SIZE + 32];
int encl, encf, decl, decf;
int ret = 0;
if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
return 0;
RAND_bytes(in, BUFFER_SIZE);
if (!TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1))
|| !TEST_true(EVP_CipherUpdate(ctx, ebuf, &encl, in, BUFFER_SIZE))
|| !TEST_true(EVP_CipherFinal_ex(ctx, ebuf+encl, &encf)))
goto end;
encl += encf;
if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
|| !TEST_true(EVP_CipherInit_ex(ctx, cipher, e, key, iv, 0))
|| !TEST_true(EVP_CipherUpdate(ctx, dbuf, &decl, ebuf, encl))
|| !TEST_true(EVP_CipherFinal_ex(ctx, dbuf+decl, &decf)))
goto end;
decl += decf;
if (!TEST_int_eq(decl, BUFFER_SIZE)
|| !TEST_mem_eq(dbuf, BUFFER_SIZE, in, BUFFER_SIZE))
goto end;
ret = 1;
end:
EVP_CIPHER_CTX_free(ctx);
return ret;
}
示例11: CryptoNative_EvpCipherFinalEx
extern "C" int32_t CryptoNative_EvpCipherFinalEx(EVP_CIPHER_CTX* ctx, uint8_t* outm, int32_t* outl)
{
int outLength;
int32_t ret = EVP_CipherFinal_ex(ctx, outm, &outLength);
if (ret == SUCCESS)
{
*outl = outLength;
}
return ret;
}
示例12: EVP_CIPHER_CTX_init
QByteArray VanityDB::AESCrypt(const QByteArray input, QString password, bool decrypt, QString salt) {
QByteArray output;
unsigned char ckeyHash[32];
unsigned char ivector[32];
int cLen = 0, fLen = 0;
EVP_CIPHER_CTX cipherContext;
EVP_CIPHER_CTX_init( &cipherContext );
output.resize(input.length() + AES_BLOCK_SIZE + 100);
SHA256(reinterpret_cast<const unsigned char*>(password.toStdString().c_str()),
password.length(),
ckeyHash);
SHA256(reinterpret_cast<const unsigned char*>(QString(password + salt).toStdString().c_str()),
password.length() + salt.length(),
ivector);
if (!decrypt) {
EVP_EncryptInit_ex(&cipherContext,
EVP_aes_256_cbc(), NULL,
reinterpret_cast<const unsigned char*>(ckeyHash), ivector);
EVP_EncryptUpdate(&cipherContext,
reinterpret_cast<unsigned char*>(output.data()),
&cLen,
reinterpret_cast<const unsigned char*>(input.constData()),
input.length());
EVP_CipherFinal_ex(&cipherContext,
reinterpret_cast<unsigned char*>(output.data()) + cLen,
&fLen);
} else {
EVP_DecryptInit_ex(&cipherContext,
EVP_aes_256_cbc(), NULL,
reinterpret_cast<const unsigned char*>(ckeyHash), ivector);
EVP_DecryptUpdate(&cipherContext,
reinterpret_cast<unsigned char*>(output.data()),
&cLen,
reinterpret_cast<const unsigned char*>(input.constData()),
input.length());
EVP_DecryptFinal_ex(&cipherContext,
reinterpret_cast<unsigned char*>(output.data()) + cLen,
&fLen);
}
EVP_CIPHER_CTX_cleanup(&cipherContext);
output.resize(cLen + fLen);
return output;
}
示例13: EVP_CIPHER_CTX_new
/*
* Encrypt/Decrypt a buffer based on password and algor, result in a
* OPENSSL_malloc'ed buffer
*/
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
const char *pass, int passlen,
const unsigned char *in, int inlen,
unsigned char **data, int *datalen, int en_de)
{
unsigned char *out = NULL;
int outlen, i;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (ctx == NULL) {
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
goto err;
}
/* Decrypt data */
if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
algor->parameter, ctx, en_de)) {
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
goto err;
}
if ((out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(ctx)))
== NULL) {
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
OPENSSL_free(out);
out = NULL;
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_EVP_LIB);
goto err;
}
outlen = i;
if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
OPENSSL_free(out);
out = NULL;
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
goto err;
}
outlen += i;
if (datalen)
*datalen = outlen;
if (data)
*data = out;
err:
EVP_CIPHER_CTX_free(ctx);
return out;
}
示例14: cipher_final
void
cipher_final(struct iked_cipher *encr, void *out, size_t *outlen)
{
int olen;
olen = 0;
if (!EVP_CipherFinal_ex(encr->encr_ctx, out, &olen)) {
ca_sslerror(__func__);
*outlen = 0;
return;
}
*outlen = (size_t)olen;
}
示例15: do_crypt
int do_crypt(FILE *in, FILE *out, int do_encrypt, unsigned char *key_data,
int key_data_len, unsigned char *salt) {
/* Allow enough space in output buffer for additional block */
int i, nrounds = 2;
unsigned char key[16], iv[16];
unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
memset(inbuf, 0, 1024);
int inlen, outlen;
EVP_CIPHER_CTX ctx;
/* Bogus key and IV: we'd normally set these from
* another source.
*/
i = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, key_data,
key_data_len, nrounds, key, iv);
if (i != 16) {
printf("Key size is %d bits - should be 256 bits\n", i);
return -1;
}
/* Don't set key or IV right away; we want to check lengths */
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, do_encrypt);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx) == 16);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == 16);
/* Now we can set key and IV */
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
for (;;) {
inlen = fread(inbuf, 1, 1024, in);
if (inlen <= 0)
break;
memset(outbuf, 0, 1024 + EVP_MAX_BLOCK_LENGTH);
if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) {
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
memset(inbuf, 0, 1024);
}
if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) {
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(in);
fclose(out);
invalid_token();
}
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(in);
fclose(out);
return 1;
}