2

I use C# RijndaelManaged of .net and C++ TinyAES encrypt data, most of the two result is the same, the result of C# has 16 bytes more than the result of C++ TinyAES, the other data is the same. I try to change C++ Rijndael project, but it does not work.

The follow is C# code 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();
        }

The follow is C++ code 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;
}

I check the data file encrypted, the 16 bytes data encrypted by C# that more than C++ is not IV, and I try to change some data for encrypting, sometimes, the size of data encryted by C# and C++ is the same, but dozens of data at the end of data file encrypted is different.

Fly
  • 21
  • 2
  • 1) What is the value of BUFSIZE? 2) Why is it commented out and replaced with 80 in the last line of c++ code? – michalsrb Apr 27 '18 at 07:16
  • the value of BUFSIZE is 1024, it's too long for result. the length of result is 64. 80 is a temporary data for this test, but it is enough. – Fly Apr 27 '18 at 07:29
  • What is the final size of the file saved by C# and by C++ version? – michalsrb Apr 27 '18 at 09:48
  • C# is 80, C++ is 64. – Fly Apr 27 '18 at 10:12
  • My guess would be that the C# code also saves the 16 bytes of IV in front of the encrypted data. Could you edit your answer to include the code that saves the data to file? – michalsrb Apr 27 '18 at 13:32
  • I have added the code that saves the data to file. And I check the data file encrypted, the 16 bytes data encrypted by C# that more than C++ is not IV, the other data is the same. And I try to change some data for encrypting, sometimes, the size of data encryted by C# and C++ is the same, but dozens of data in the end of data file encrypted is different. – Fly Apr 28 '18 at 02:04

0 Answers0