Question

I was trying to use XTEA encryption on NETMF using the code from blog post Encryption Compatibility Between .NET Micro Framework and the Full .NET Framework. The code used on the micro framework is the following.

public static string Encrypt(string message)
{
    var key = "0x081632";
    var data = Encoding.UTF8.GetBytes(message);
    var xteaKey = Encoding.UTF8.GetBytes(key);
    var xtea = new Key_TinyEncryptionAlgorithm(xteaKey);
    var encryptedBytes = xtea.Encrypt(data, 0, data.Length, xteaKey);
    var encryptedString = ConvertBase64.ToBase64String(encryptedBytes);
    return encryptedString;
}

The Base64 result is sent to an Azure web service. However, I am unable to decrypt this. I have tried several situations, but the .NET code is returning different results for the same key and message on .NET and NETMF.

Is there a solution for this situation, or any other encryption scheme I can use? I prefer not to use RSA, as it can be a real performance hit on NETMF.

Decryption code.

var k = Encoding.UTF8.GetBytes("0x081632");
message = message.Replace("!", "+").Replace("*", "/"); // base64 on .netmf is different
var m = Convert.FromBase64String(message);
var xteaNet = new Key_TinyEncryptionAlgorithm(k); // class from blog
var decBytes = xteaNet.Decrypt(m, 0, m.Length, k);
var decString = Encoding.UTF8.GetString(decBytes);

It may be a little/big endian difference, but I'm not sure what parts of the code would have to be changed to allow for this.

For testing, I tried to encode with data 8 long, and key 8 long, and now the result on both systems are equal. Data is "01234567", key is "0x081632", result after base64 "2BwR4Xe2sIk=".

Était-ce utile?

La solution

I created a new .NET micro framework v4.1 console application using a copy/paste of the encrypt method.

In the same solution I added a new .NET 4.0 test project, which referenced the sample Key_TinyEncryptionAlgorithm class from the blog. I used the returned value from the Encrypt method 2BwR4Xe2sIk= and this was used as the test data for the unit test which decrypted the string successfully back into 01234567.

I would assume your transport mechanism is causing the problem. The message over the wire is being modified and so what arrives at your service is not equivalent to what left the device.

Perhaps just a classic serialization problem...

All x86 and machines are little-endian. The .NET Micro Framework emulator reports via SystemInfo.IsBigEndian that the emulated environment is little-endian as well.

It is common to tranmit data over a network in big-endian order though. If you want to reverse the bytes on the recieving end the following method should flip the order for you. I put this into the Key_TinyEncryptionAlgorithm class.

private static byte[] ReverseBytes(byte[] inArray)
{
  byte temp;
  int highCtr = inArray.Length - 1;

  for (int ctr = 0; ctr < inArray.Length / 2; ctr++)
  {
      temp = inArray[ctr];
      inArray[ctr] = inArray[highCtr];
      inArray[highCtr] = temp;
      highCtr -= 1;
   }
   return inArray;
}

And usage in the decryption method.

Console.WriteLine(BitConverter.ToString(m));
m = Key_TinyEncryptionAlgorithm.ReverseBytes(m);
Console.WriteLine(BitConverter.ToString(m));

Produces this output

D8-1C-11-E1-77-B6-B0-89

89-B0-B6-77-E1-11-1C-D8

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top