如何正确使用 kotlin 解密 c++ aes Rijndael CBC?

How to decrypt c++ the aes Rijndael CBC using kotlin properly?

Md5哈希值(盐+密码)

hashed: "89d1ed22aac58f5bbea53b2fde81a946" (String)(32 characters)

原始字符串值

orginal: "test" (String)

加密密钥和 IV(字节)

Key: "encryptionkeyhere" (String)

16 bit key: 646C646873766D666C766D000000000 (Bytes)(Converted to 16 Bytes)

16 bit IV: 00000000000000000000000000000000 (Bytes)

加密的十六进制值

084987B6C979950A11EBE33A5499B091D127CD208E95BAE5C6B5DE5FAE65AFB68EB5C083BB808FDFD98C16694E6FCA9F2E15DF5A63FBD7E4E7EFFB242D0D56B1A3C35F76F977A70F5A9A1EAD0FC3A9E61242CBA0AF848FFC5C8C4342F4011E0436D81F6B064E086C802E175F662C43A798ADC38D25684E99E926ED30B900FF89CA66760B7DBDFBF3087378620A69981FB87346512DC6596E7420763C238EC7E2015941F3E613070E737A0D191F4730C3CF70C270B5FA13E44407B7D6D7567B6126241777D80874320B969B8818371CEC91DF97AA42ABE5BCF015D9B17BDC18F2F3E1E7E25794A97C44F760B68F1D24FD96036DFC92AF400D53D1292C1FBDD0296AFE28401D48B2EA486C781B4729E55C8794505C7AC3AB2A35C7B893DE4128A3A59335BC76071E404A9B7D600C19CBB0CB640071C7AF9CB135FC46DD29080707ED30BC9CDA6AE65ACDD84CF52B299408D82333F1A8DBD1F58213671063D7C8C1BB9002563A04BD53B03D802888F299024AD8C9BE2A0749EF920362A4C86129401DB29C2D2A17A960C2038C0DC6CCD18F361A629BBDCA17BFF0EB1EB737AE9F00CAB8CFBE2050749E6F0D6EAB7DC849380E85BC55DA82AF7354D31D8431A47FB385CC5CA01FE1DE064AE3F2426A5135938AD923C4A28661CBDA7F1397326D7921158FB06A2B4A1C6F8D141B2749D8A8B20883A7A168D811057CDA88D595EEC694558C446673B708BABFFB5FA584B2AFE527A9C3B162BD38405FC08250AB5EF1D15C7ED94AB71796A5582E25ADFBF83D7BD146C9BF5FE68FA3ADEA4C0D77A4314D06336F6D503A8C097A0FD54051CDD49ED8ECA34E3150D61C01FBB53208C62D701BEFEB15419776B9FB18437FE2C1B4A9BABFDE3CE83457FF6F3F87F83B9B702450D0409886B9D9490BD665BB1CB3E5E460ACDD7BC51958A3870985D58E4585B

C++加密代码

void CRijndael::Encrypt(char const* in, char* result, size_t n, int iMode)
{
    if(false==m_bKeyInit)
        throw exception(sm_szErrorMsg1);
    //n should be > 0 and multiple of m_blockSize
    if(0==n || n%m_blockSize!=0)
        throw exception(sm_szErrorMsg2);
    int i;
    char const* pin;
    char* presult;
    if(CBC == iMode) //CBC mode, using the Chain
    {
        for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
        {
            Xor(m_chain, pin);
            EncryptBlock(m_chain, presult);
            memcpy(m_chain, presult, m_blockSize);
            pin += m_blockSize;
            presult += m_blockSize;
        }
    }
    else if(CFB == iMode) //CFB mode, using the Chain
    {
        for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
        {
            EncryptBlock(m_chain, presult);
            Xor(presult, pin);
            memcpy(m_chain, presult, m_blockSize);
            pin += m_blockSize;
            presult += m_blockSize;
        }
    }
    else //ECB mode, not using the Chain
    {
        for(i=0,pin=in,presult=result; i<(int)( n/m_blockSize ); i++)
        {
            EncryptBlock(pin, presult);
            pin += m_blockSize;
            presult += m_blockSize;
        }
    }
}

Java/Kotlin Aes解密码

var key = buildKeyFromString(encryptionKey)
val iv = IvParameterSpec(key)
val secretKeySpec = SecretKeySpec(key, "AES")
val cipher = Cipher.getInstance("AES/CBC/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
return cipher.doFinal(encryptedByte)

现在的问题是当我使用kotlin代码解密时,初始字符串编码不正确

解密的字符串值

String: "????58f5bbea53b2fde81a946" (String)(Wrong value)

解密的十六进制值(32 字节)

Hex: "5C55005916125F540D170E353866356262656135336232666465383161393436"

预期的字符串值

Hashed: "89d1ed22aac58f5bbea53b2fde81a946" (String)(32 characters)

问题的原因是加密和解密使用不同的IV:加密使用零向量(0x00000000000000000000000000000000),解密使用密钥作为IV(0x646C646873766D666C766D0000000000)。

对了,贴出来的密文太长了。由于明文大小为 32 字节且未使用填充,因此密文大小也是 32 字节并且仅对应于发布密文的前 32 字节 (0x084987B6C979950A11EBE33A5499B091D127CD208E95BAE5C6B5DE5FAE65AFB6)。

我对 IV 和 secretkeyspec 使用了 key 变量。所以我将 IV 更改为 16 字节 0。

var key = buildKeyFromString(encryptionKey)
var ivBytes = 16 bytes zero
val iv = IvParameterSpec(ivBytes )
val secretKeySpec = SecretKeySpec(key, "AES")
val cipher = Cipher.getInstance("AES/CBC/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv)
return cipher.doFinal(encryptedByte)