在C#中为windows存储应用程序生成AES密钥

本文关键字:程序生成 AES 密钥 应用 存储 中为 windows | 更新日期: 2023-09-27 18:26:26

我需要在windows商店应用程序之前,在c#中为AES加密生成一个256位密钥。Ised执行了以下代码,并且正在获取一个exeception。请指出我哪里做错了。

public string GenAESkey()
        {
            UInt32 keySize = 32;
            CryptographicKey key;
            SymmetricKeyAlgorithmProvider Algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);
            IBuffer keymaterial = CryptographicBuffer.GenerateRandom((keySize + 7) / 8);
            try
            {
                key = Algorithm.CreateSymmetricKey(keymaterial);
                return key.ToString();
            }
            catch
            {
                return null;
            }

错误为"值不在预期范围内。"@Algorithm.CreateSymmetricKey(keymaterial);命令。

在C#中为windows存储应用程序生成AES密钥

我的坏。问题出在这里。

UInt32 keySize = 32;

将其更改为

UInt32 keySize = 256;

并且evrything是良好的