qq_41606877 2018-04-20 09:01 采纳率: 0%
浏览 2844
已结题

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

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 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,但是数据是不对的,请高手看看为什么调用失败
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料