qq_41606877
qq_41606877
采纳率0%
2018-04-20 09:01 阅读 2.6k

aes gcm解密问题(把java解密函数转换成C++版本)

5

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,但是数据是不对的,请高手看看为什么调用失败
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • qq_41606877 qq_41606877 2018-04-20 10:09

    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,但是数据是不对的,请高手看看为什么调用失败
    
    点赞 评论 复制链接分享

相关推荐