C#和C ++之间Rijndael加密差异的结果

时间:2018-04-27 07:00:45

标签: c# c++ rijndaelmanaged

我使用C#RijndaelManaged的.net和C ++ TinyAES加密数据,两者的大部分结果是一样的,C#的结果比C ++ TinyAES的结果多16个字节,其他数据是一样的。我尝试更改C ++ Rijndael项目,但它不起作用。

以下是C#代码 C#:

RijndaelManaged RMCrypto = new RijndaelManaged();

    RMCrypto.KeySize = 256;

    byte[] RMKey = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73,      0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
            0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };

    byte[] RMIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

    System.IO.MemoryStream tmpEncryptedData = new System.IO.MemoryStream();

    CryptoStream CryptStream = new CryptoStream(tmpEncryptedData, RMCrypto.CreateEncryptor(RMKey, RMIV), CryptoStreamMode.Write);

    byte[] ttData = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
            0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
            0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
            0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };

    int ttDataLen = ttData.Length;
    CryptStream.Write(ttData, 0, ttDataLen);
    CryptStream.FlushFinalBlock();
    CryptStream.Close();

    WriteFile("EncryptedData", tmpEncryptedData.ToArray());

public void WriteFile(string file, byte[] data)
        {
            string path = "D:\\File\\";
            path += file;

            BinaryWriter bw;
            bw = new BinaryWriter(new FileStream(path, FileMode.Create));
            bw.Write(data, 0, data.Length);
            bw.Flush();
            bw.Close();
        }

以下是C ++代码 C ++:

    uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
            0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };

    uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

    uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
            0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
            0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
            0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };

    char compressedData[BUFSIZE] = { 0 };
    for (int i = 0; i < sizeof(in); i++)
    {
        compressedData[i] = in[i];
    }
    char encryptData[BUFSIZE] = { 0 };

    aes_encrypt_cbc(AES_CYPHER_256, (uint8_t*)compressedData, sizeof(in), (uint8_t*)key, (uint8_t*)iv);

    compressedDataSize, (uint8_t*)key);

    WriteFile("EncryptData", compressedData, /*BUFSIZE*/80);


// Write file
int WriteFile(const char* fileName, const char* buf, int length)
{
    FILE* fp = NULL;
    int ret = -1;

    fp = fopen(fileName, "wb");
    if (NULL == fp)
    {
        return -1;
    }

    ret = fwrite(buf, 1, length, fp);
    fclose(fp);

    return ret;
}

我检查加密的数据文件,C#加密的16字节数据,超过C ++不是IV,我尝试更改一些加密数据,有时,C#和C ++加密的数据大小是一样的,但加密数据文件末尾的数十个数据是不同的。

0 个答案:

没有答案