在 NETMF 上加密,在 .NET 上解密

发布于 2024-12-28 18:05:38 字数 1417 浏览 2 评论 0原文

我试图使用博客文章 .NET Micro Framework 与完整 .NET Framework 之间的加密兼容性。微框架上使用的代码如下。

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;
}

Base64 结果发送到 Azure Web 服务。但是,我无法解密这个。我尝试了几种情况,但 .NET 代码在 .NET 和 NETMF 上为相同的密钥和消息返回不同的结果。

对于这种情况有解决方案吗,或者我可以使用任何其他加密方案?我不喜欢使用 RSA,因为它可能会对 NETMF 造成真正的性能影响。

解密代码。

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);

这可能是一个小/大的字节序差异,但我不确定需要更改代码的哪些部分才能实现这一点。

为了测试,我尝试使用数据 8 长和密钥 8 长进行编码,现在两个系统上的结果是相同的。数据为“01234567”,密钥为“0x081632”,base64 后的结果为“2BwR4Xe2sIk=”。

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=".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2025-01-04 18:05:38

我使用加密方法的复制/粘贴创建了一个新的 .NET micro Framework v4.1 控制台应用程序。

在同一解决方案中,我添加了一个新的 .NET 4.0 测试项目,该项目引用了博客中的示例 Key_TinyEncryptionAlgorithm 类。我使用了 Encrypt 方法 2BwR4Xe2sIk= 返回的值,并将其用作单元测试的测试数据,该单元测试成功将字符串解密回 01234567

我认为您的传输机制导致了问题。线路上的消息正在被修改,因此到达您的服务的消息与离开设备的消息并不相同。

也许只是一个经典的序列化问题...

所有 x86 和机器都是小端字节序。 .NET Micro Framework 模拟器通过 SystemInfo.IsBigEndian 报告模拟环境也是小尾数法。

不过,在网络上以大端顺序传输数据是很常见的。如果您想反转接收端的字节,以下方法应该为您翻转顺序。我将其放入 Key_TinyEncryptionAlgorithm 类中。

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;
}

以及解密方法中的用法。

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

产生这个输出

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文