gpt4 book ai didi

c# - 是否可以使用 WP7 AesManaged 重现 NIST/FIPS197 结果 (AES128)?

转载 作者:行者123 更新时间:2023-11-30 17:12:32 25 4
gpt4 key购买 nike

各位程序员好!我想让我的软件用户能够以最方便、通用和平台独立的方式将信息从他们的 Windows Phone 设备安全地传输到几个不同的服务。最好使用 Microsoft 已经提供的工具。为此,我查看了可通过 Windows Phone SDK 7.1 中的“System.Security.Cryptography”命名空间使用的 AesManaged 类。但是,到目前为止,我什至无法使用此类重现单个 NIST 示例。除其他外,我尝试了以下方法:

/* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf page 35 and 36 */
var passwordBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
var unencryptedBytes = new byte[]{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };

using (var aes = new AesManaged())
{
aes.BlockSize = 128; /* size in bits */
aes.KeySize = 128; /* size in bits */
aes.Key = passwordBytes;

using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
cryptoStream.FlushFinalBlock();

var stream = memoryStream.ToArray();
/*
should be
69-c4-e0-d8-6a-7b-04-30-d8-cd-b7-80-70-b4-c5-5a = 128 bit
according to http://testprotect.com/appendix/AEScalc and fips-197.pdf
*/
var output = BitConverter.ToString(stream);
/*
86-DB-4D-44-72-C0-16-E6-80-B9-D2-B2-3C-6D-00-40-98-4C-59-76-CF-41-DF-4E-A6-46-BB-DE-4C-13-E6-12
256 bit?
*/
}
}
}

我看到了几个使用 IV 和 Rfc2898DeriveBytes(例如 http://msdn.microsoft.com/de-de/library/system.security.cryptography.aesmanaged.aspx )的代码示例(例如 http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx ),但是每个系统默认理解的实际标准是什么?什么是默认的“Key Padding”算法以及如何在 Windows Phone 上使用它(可能是 https://www.ietf.org/rfc/rfc3394.txthttps://www.ietf.org/rfc/rfc5649.txt ?)。最后:我可以将默认设置(根据 http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.mode.aspx 的 CBC)更改为 Windows Phone 上的任何其他模式吗?

最佳答案

文档没有指出的是,它们对初始化向量使用全零。所以将 IV 设置为全零的 16 个字节并且它有效:

/* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf page 35 and 36 */
var passwordBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
var unencryptedBytes = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
var initvectorBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

using (var aes = new AesManaged())
{
aes.BlockSize = 128; /* size in bits */
aes.KeySize = 128; /* size in bits */
aes.Key = passwordBytes;
aes.IV = initvectorBytes;

using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(
memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
cryptoStream.FlushFinalBlock();

var stream = memoryStream.ToArray();
/* now you get:
69-c4-e0-d8-6a-7b-04-30-d8-cd-b7-80-70-b4-c5-5a = 128 bit
according to http://testprotect.com/appendix/AEScalc and fips-197.pdf
*/
var output = BitConverter.ToString(stream);
}
}
}

对于您的应用程序,您需要使 IV 不是全零。您将无法使用 AesManaged 更改模式,因为 AES 是使用 CBC 模式(或 ECB)实现的。如果您尝试将其设置为其他任何值,您将得到一个异常。您可以将 Padding 更改为您喜欢的任何内容。我会将其保留为 PKCS7(默认值)。

关于c# - 是否可以使用 WP7 AesManaged 重现 NIST/FIPS197 结果 (AES128)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10601711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com