简体   繁体   English

AES 64密钥生成

[英]AES 64 key generation

I am trying to generate 64 HEX digits to be used as a AES 256 key with no success. 我试图生成64个十六进制数字用作AES 256密钥,但没有成功。 Can somebody point out the mistakes and a better way to generate the same. 有人可以指出错误以及产生错误的更好方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace Test
{
    public class Program
    {
        static System.Text.StringBuilder builder = new System.Text.StringBuilder();
        public static void Main(string[] args)
        {
            String randomNumber = Convert.ToBase64String (GenerateRandomNumber(32));
            Console.WriteLine(randomNumber);

           string input = randomNumber;
            char[] values = input.ToCharArray();
            foreach (char letter in values)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(letter);
                // Convert the decimal value to a hexadecimal value in string form.
                string hexOutput = String.Format("{0:X}", value);
                // Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
                builder.Append(hexOutput);
            }

            Console.WriteLine(builder);
        }


        public static byte[] GenerateRandomNumber(int length)
        {
            using (var randomNumberGenerator = new RNGCryptoServiceProvider())
            {
                var randomNumber = new byte[length];
                randomNumberGenerator.GetBytes(randomNumber);
                return randomNumber;
            }
        }
    }
}

I don't see why you need to convert it to a base64 string first. 我不明白为什么您需要首先将其转换为base64字符串。 It could be as simple as this: 它可以像这样简单:

public class Program
{
    public static void Main(string[] args)
    {
        var key = GenerateRandomNumber(32);
        var hexEncodedKey = BitConverter.ToString(key).Replace("-", "");
        Console.WriteLine(hexEncodedKey);
    }

    public static byte[] GenerateRandomNumber(int length)
    {
        using (var randomNumberGenerator = RandomNumberGenerator.Create())
        {
            var randomNumber = new byte[length];
            randomNumberGenerator.GetBytes(randomNumber);
            return randomNumber;
        }
    }
}

Your biggest technical problem is that you used {0:X} when you meant {0:X2} . 您最大的技术问题是您使用{0:X}时使用了{0:X} {0:X2} If the value is 10 the former produces "A" and the latter "0A". 如果值为10,则前者将产生“ A”,而后者将产生“ 0A”。 Since you've lost where all of the interior zeroes are your number isn't recoverable. 由于您迷失了所有内部零的位置,因此您的数字将无法恢复。

internal static string ByteArrayToHex(this byte[] bytes)
{
    StringBuilder builder = new StringBuilder(bytes.Length * 2);

    foreach (byte b in bytes)
    {
        builder.Append(b.ToString("X2"));
    }

    return builder.ToString();
}

(Code copied from https://github.com/dotnet/corefx/blob/7cad8486cbabbce0236bdf530e30db7036335524/src/Common/tests/System/Security/Cryptography/ByteUtils.cs#L37-L47 ) (从https://github.com/dotnet/corefx/blob/7cad8486cbabbce0236bdf530e30db7036335524/src/Common/tests/System/Security/Cryptography/ByteUtils.cs#L37-L47复制的代码)

But it's also pretty unclear why you're rerouting through Base64+ToCharArray+ToInt32. 但是也不清楚为什么要通过Base64 + ToCharArray + ToInt32进行路由。 You're replacing values in the 0-255 range (bytes) with values in the [A-Za-z0-9/=+] rangeset, equivalent to 0-63 (Base64 and all); 您正在用[A-Za-z0-9 / = +]范围集中的值替换0-255范围(字节)中的值,该值等效于0-63(Base64和所有); so you a) wouldn't have a very random key and b) it'll be too long. 因此您a)不会有非常随机的密钥,b)它将太长。

.NET框架已经具有用于生成对称密钥的方法Aes.GenerateKey(),请查看此MSDN文档: Aes类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM