如何将Base64String转换为Byte[]

本文关键字:Byte 转换 Base64String | 更新日期: 2023-09-27 17:57:30

我试图在一个程序中使用AES加密,在另一个程序中将其解密。我有加密文件的工作代码,但我需要提取密钥用于其他程序进行解密。

当我这样做时:

string key = String.Empty;
byte[] aesKey;
using (var aes = Aes.Create())
{
    aesKey = aes.Key;
    key = Convert.ToBase64String(aes.Key);
}

并在我的加密中使用aesKey,它是有效的。当我尝试从Base64字符串转换为Byte[]时,它似乎不起作用,最终不会得到相同的字节。你是如何转换的?

string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
aesKey = System.Text.Encoding.UTF8.GetBytes(sKey);

如何将Base64String转换为Byte[]

您应该使用相应的From方法。

byte[] aesKey = Convert.FromBase64String(sKey);