| 989 | //�ͻ��˲ü�����evp�ӿڣ�����ֻ�ڷ���������ʹ��evp�ӿڵİ汾 |
| 990 | |
| 991 | int AESEncrypt(const unsigned char* pInput, unsigned int uiInputLen,std::string* rbOutput, |
| 992 | const char* pKey, unsigned int uiKeyLen) { |
| 993 | if (!pInput || !pKey || !rbOutput) |
| 994 | { |
| 995 | return CRYPT_ERR_INVALID_PARAM; |
| 996 | } |
| 997 | |
| 998 | //EVP �ӿڻᱣ֤ʹ��ƽ̨���е�����ѧ���٣�����AES-NI |
| 999 | const EVP_CIPHER *cipher = EVP_aes_128_cbc(); |
| 1000 | |
| 1001 | EVP_CIPHER_CTX ctx; |
| 1002 | EVP_CIPHER_CTX_init(&ctx); |
| 1003 | EVP_CIPHER_CTX_set_padding(&ctx,1);//ʹ��pkcs padding |
| 1004 | |
| 1005 | unsigned char key[16], iv[16]; //aes_128�� key size=128bit , iv size=128bit |
| 1006 | memset(key, 0, sizeof(key)); |
| 1007 | memset(iv, 0, sizeof(iv)); |
| 1008 | memcpy(key, pKey, uiKeyLen > 16U ? 16U : uiKeyLen); |
| 1009 | memcpy(iv, key, sizeof(key)); |
| 1010 | bool ret=CRYPT_ERR_NO_MEMORY; |
| 1011 | do{ |
| 1012 | if(1 != EVP_EncryptInit_ex(&ctx, cipher, NULL, key, iv)){ |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | rbOutput->assign(uiInputLen + EVP_CIPHER_block_size(cipher),0); |
| 1017 | int len=rbOutput->size(); |
| 1018 | int ciphertext_len=0; |
| 1019 | |
| 1020 | if(1 != EVP_EncryptUpdate(&ctx, (unsigned char *) &(*rbOutput)[0], &len, pInput, uiInputLen)){ |
| 1021 | break; |
| 1022 | } |
| 1023 | ciphertext_len = len; |
| 1024 | |
| 1025 | if(1 != EVP_EncryptFinal_ex(&ctx, (unsigned char *) &(*rbOutput)[0] + len, &len)){ |
| 1026 | break; |
| 1027 | } |
| 1028 | ciphertext_len += len; |
| 1029 | rbOutput->resize(ciphertext_len); |
| 1030 | ret=CRYPT_OK; |
| 1031 | }while(0); |
| 1032 | |
| 1033 | /* Clean up */ |
| 1034 | OPENSSL_cleanse(key,sizeof(key)); |
| 1035 | OPENSSL_cleanse(iv,sizeof(iv)); |
| 1036 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | int AESDecrypt(const unsigned char* pInput, unsigned int uiInputLen, std::string* sOutput, |
| 1041 | const char* pKey, unsigned int uiKeyLen) { |
no test coverage detected