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

C++ EVP_CipherFinal_ex函数代码示例

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

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



在下文中一共展示了EVP_CipherFinal_ex函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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) ;
}
开发者ID:GDXN,项目名称:test,代码行数:54,代码来源:testevp.c


示例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);
}
开发者ID:world100,项目名称:11111,代码行数:28,代码来源:cipher.c


示例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;
	}
开发者ID:pombredanne,项目名称:cybozulib,代码行数:11,代码来源:crypto.hpp


示例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;    
}
开发者ID:jeffjee617,项目名称:secureVPN,代码行数:36,代码来源:fun.c


示例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;
        }
开发者ID:zhuzhu9910,项目名称:AcademicProjects,代码行数:29,代码来源:utilities.c


示例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;
}
开发者ID:rndCombo,项目名称:contestants_code_BIBIFI,代码行数:33,代码来源:CryptoBuffer.cpp


示例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;
}
开发者ID:2Quico,项目名称:netbox,代码行数:58,代码来源:BCipher.cpp


示例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);
}
开发者ID:zillians,项目名称:supercell_common,代码行数:55,代码来源:Crypto.cpp


示例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;
    }
开发者ID:byronhe,项目名称:modern-crypto-toolbox,代码行数:59,代码来源:symm.cpp


示例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;
}
开发者ID:Vonage,项目名称:openssl,代码行数:42,代码来源:afalgtest.c


示例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;
}
开发者ID:jemmy655,项目名称:corefx,代码行数:11,代码来源:pal_evp_cipher.cpp


示例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;
}
开发者ID:TechMiX,项目名称:BVAM,代码行数:53,代码来源:vanitydb.cpp


示例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;

}
开发者ID:ciz,项目名称:openssl,代码行数:57,代码来源:p12_decr.c


示例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;
}
开发者ID:jymigeon,项目名称:openiked,代码行数:13,代码来源:crypto.c


示例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;
}
开发者ID:KevinCooper,项目名称:buildit,代码行数:51,代码来源:functions.c


示例16: pbe_crypt

static int pbe_crypt(const X509_ALGOR *algor,
                     const uint8_t *pass_raw, size_t pass_raw_len,
                     const uint8_t *in, size_t in_len,
                     uint8_t **out, size_t *out_len,
                     int is_encrypt) {
  uint8_t *buf;
  int n, ret = 0;
  EVP_CIPHER_CTX ctx;
  unsigned block_size;

  EVP_CIPHER_CTX_init(&ctx);

  if (!pbe_cipher_init(algor->algorithm, pass_raw, pass_raw_len,
                       algor->parameter, &ctx, is_encrypt)) {
    OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM);
    return 0;
  }
  block_size = EVP_CIPHER_CTX_block_size(&ctx);

  if (in_len + block_size < in_len) {
    OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_TOO_LONG);
    goto err;
  }

  buf = OPENSSL_malloc(in_len + block_size);
  if (buf == NULL) {
    OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_MALLOC_FAILURE);
    goto err;
  }

  if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) {
    OPENSSL_free(buf);
    OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
    goto err;
  }
  *out_len = n;

  if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) {
    OPENSSL_free(buf);
    OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
    goto err;
  }
  *out_len += n;
  *out = buf;
  ret = 1;

err:
  EVP_CIPHER_CTX_cleanup(&ctx);
  return ret;
}
开发者ID:RobinWuDev,项目名称:Qt,代码行数:50,代码来源:pkcs8.c


示例17: EVP_CIPHER_CTX_init

int AesFileEnc::do_crypt(FILE *in, FILE *out, int do_encrypt, unsigned char* key)
{
	/* Allow enough space in output buffer for additional block */
	unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
	int inlen, outlen;
	EVP_CIPHER_CTX ctx;

	std::cout << "tutaj";
	//	std::cout <<key<<std::endl;
	//unsigned char key[] = "0123456789abcdeF";
	std::cout <<key<< std::endl;


	//unsigned char iv[] = "1234567887654321";
	EVP_CIPHER_CTX_init(&ctx);
	
			EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
				do_encrypt);
			
	unsigned char *iv = this->iv(EVP_CIPHER_CTX_iv_length(&ctx));	
			std::cout<< this->keyLength << std::endl;
			std::cout<< EVP_CIPHER_CTX_iv_length(&ctx) <<std::endl;
			OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx) == this->keyLength);
			//OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == this->keyLength);
			EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
	
			for(;;)
					{
					inlen = fread(inbuf, 1, 1024, in);
					if(inlen <= 0) break;
					if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
							{
	
							EVP_CIPHER_CTX_cleanup(&ctx);
							return 0;
							}
					fwrite(outbuf, 1, outlen, out);
					}
			if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
					{

					EVP_CIPHER_CTX_cleanup(&ctx);
					return 0;
					}
			fwrite(outbuf, 1, outlen, out);

			EVP_CIPHER_CTX_cleanup(&ctx);
			return 1;
			}
开发者ID:MichalKupczynski,项目名称:kryptografia,代码行数:49,代码来源:AesFileEnc.cpp


示例18: decrypt_gcm

bool decrypt_gcm(uint8_t *key, uint8_t *iv, void *addr, size_t len, uint8_t *tag) {
    EVP_CIPHER_CTX ctx;
    int rc, tmp;

    EVP_CIPHER_CTX_init(&ctx);
    assert(EVP_DecryptInit_ex(&ctx, EVP_aes_256_gcm(), NULL, NULL, NULL));
    EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, IV_LEN, NULL);
    EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG,  TAG_LEN, tag);
    assert(EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv));
    assert(EVP_DecryptUpdate(&ctx, addr, &tmp, addr, len));
    rc = EVP_CipherFinal_ex(&ctx, NULL, &tmp);
    EVP_CIPHER_CTX_cleanup(&ctx);

    return rc == 1;
}
开发者ID:10xEngineer,项目名称:My-Wallet-iPhone,代码行数:15,代码来源:crypto.c


示例19: ossl_cipher_final

/*
 *  call-seq:
 *     cipher.final -> string
 *
 *  Returns the remaining data held in the cipher object. Further calls to
 *  Cipher#update or Cipher#final will return garbage. This call should always
 *  be made as the last call of an encryption or decryption operation, after
 *  after having fed the entire plaintext or ciphertext to the Cipher instance.
 *
 *  If an authenticated cipher was used, a CipherError is raised if the tag
 *  could not be authenticated successfully. Only call this method after
 *  setting the authentication tag and passing the entire contents of the
 *  ciphertext into the cipher.
 */
static VALUE
ossl_cipher_final(VALUE self)
{
    EVP_CIPHER_CTX *ctx;
    int out_len;
    VALUE str;

    GetCipher(self, ctx);
    str = rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
    if (!EVP_CipherFinal_ex(ctx, (unsigned char *)RSTRING_PTR(str), &out_len))
	ossl_raise(eCipherError, NULL);
    assert(out_len <= RSTRING_LEN(str));
    rb_str_set_len(str, out_len);

    return str;
}
开发者ID:hilben,项目名称:ruby_test,代码行数:30,代码来源:ossl_cipher.c


示例20: PKCS12_pbe_crypt

unsigned char *
PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass, int passlen,
    unsigned char *in, int inlen, unsigned char **data, int *datalen, int en_de)
{
	unsigned char *out;
	int outlen, i;
	EVP_CIPHER_CTX ctx;

	EVP_CIPHER_CTX_init(&ctx);
	/* Decrypt data */
	if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
	    algor->parameter, &ctx, en_de)) {
		out = NULL;
		PKCS12error(PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
		goto err;
	}

	if (!(out = malloc(inlen + EVP_CIPHER_CTX_block_size(&ctx)))) {
		PKCS12error(ERR_R_MALLOC_FAILURE);
		goto err;
	}

	if (!EVP_CipherUpdate(&ctx, out, &i, in, inlen)) {
		free(out);
		out = NULL;
		PKCS12error(ERR_R_EVP_LIB);
		goto err;
	}

	outlen = i;
	if (!EVP_CipherFinal_ex(&ctx, out + i, &i)) {
		free(out);
		out = NULL;
		PKCS12error(PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
		goto err;
	}
	outlen += i;
	if (datalen)
		*datalen = outlen;
	if (data)
		*data = out;

err:
	EVP_CIPHER_CTX_cleanup(&ctx);
	return out;

}
开发者ID:2trill2spill,项目名称:nextgen,代码行数:47,代码来源:p12_decr.c



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C++ EVP_CipherInit函数代码示例发布时间:2022-05-30
下一篇:
C++ EVP_Cipher函数代码示例发布时间: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