val nonce = reader.readBits(96)
val tag = reader.readBits(128)
val ciphertext = reader2.readBits(bitsLeft)
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
val keySpec = SecretKeySpec(EncryptionToken, "AES")
val paramSpec = GCMParameterSpec(128, nonce)
cipher.init(Cipher.DECRYPT_MODE, keySpec, paramSpec);
cipher.update(ciphertext)
val plaintext = cipher.doFinal(tag)
这是kotlin代码,用的是java的解密库
求高手翻译成C++版,用现成的解密库也可以
我的C++
val nonce = reader.ReadBits(96);
val tag = reader.ReadBits(128);
var bitsLeft = reader.GetBitsLeft();
val ciphertext = reader2.ReadBits(bitsLeft);
int ciphelegth = bitsLeft/8;
std::vector out_tmp;
out_tmp.resize(ciphelegth);
mbedtls_gcm_context ctx;
mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
mbedtls_gcm_init(&ctx);
int ret = mbedtls_gcm_setkey(&ctx, cipher, (unsigned char*)EncryptionToken.c_str(),
128);
uint8 iv[16];
memset(iv, 0, 16);
memcpy(iv, &nonce[0], 12);
ret = mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_DECRYPT, ciphelegth, iv,
12, NULL, 0, &ciphertext[0], &out_tmp[0], 16, &tag[0]);
ret 也是0,但是数据是不对的,请高手看看为什么调用失败