如何创建byte []的长度为16使用FromBase64String [英] How to create byte[] with length 16 using FromBase64String

查看:160
本文介绍了如何创建byte []的长度为16使用FromBase64String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,创建一个字节[] 与长度为16(一个字节数组,有128位被用作主要的AES加密)。

I have a requirement to create a byte[] with length 16. (A byte array that has 128 bit to be used as Key in AES encryption).

下面是一个有效的字符串

Following is a valid string

"AAECAwQFBgcICQoLDA0ODw=="

什么是算法来决定一个字符串是否为128位?或者是试错,只有这样才能创造这样的128位字符串?

What is the algorithm that determines whether a string will be 128 bit? Or is trial and error the only way to create such 128 bit strings?

code

    static void Main(string[] args)
    {
        string firstString = "AAECAwQFBgcICQoLDA0ODw=="; //String Length = 24
        string secondString = "ABCDEFGHIJKLMNOPQRSTUVWX"; //String Length = 24
        int test = secondString.Length;

        byte[] firstByteArray = Convert.FromBase64String((firstString));
        byte[] secondByteArray = Convert.FromBase64String((secondString));

        int firstLength = firstByteArray.Length;
        int secondLength = secondByteArray.Length;


        Console.WriteLine("First Length: " + firstLength.ToString());
        Console.WriteLine("Second Length: " + secondLength.ToString());

        Console.ReadLine();
    }

结果

有关256位,我们需要六分之二百五十六= 42.66字符。这是四舍五入为43字符。 [为了使它能被4整除添加 = ]

For 256 bit, we need 256/6 = 42.66 chars. That is rounded to 43 char. [To make it divisible by 4 add =]

有关512位,我们需要六分之五百十二= 85.33字符。这是四舍五入为86字符。 [为了使它能被4整除添加 == ]

For 512 bit, we need 512/6 = 85.33 chars. That is rounded to 86 char. [To make it divisible by 4 add ==]

有关128位,我们需要六分之一百二十八= 21.33字符。这是四舍五入为22字符。 [为了使它能被4整除添加 == ]

For 128 bit, we need 128/6 = 21.33 chars. That is rounded to 22 char. [To make it divisible by 4 add ==]

推荐答案

一个base64字符串16字节永远是24个字符,并具有 == 末,作为填充。

A base64 string for 16 bytes will always be 24 characters and have == at the end, as padding.

(至少当它的解码使​​用.NET方法,该填充并不总是inlcuded为Base64字符串的所有应用,但.NET实现需要它。)

(At least when it's decodable using the .NET method. The padding is not always inlcuded in all uses of base64 strings, but the .NET implementation requires it.)

这篇关于如何创建byte []的长度为16使用FromBase64String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆