gpt4 book ai didi

c# - AES 管理的加密值总是以 ==?

转载 作者:太空狗 更新时间:2023-10-29 18:20:19 24 4
gpt4 key购买 nike

我已经用网上的几种方法编写了一个加密例程。我目前正在测试加密并注意到每个加密值都以 '==' 结尾?

有谁知道为什么会这样?

这是我正在使用的代码。键是一个 32 个字符的值,IV 是一个 16 个字符的值。

private static readonly byte[] key = Encoding.Default.GetBytes(getKey());
private static readonly byte[] iv = Encoding.Default.GetBytes("$ruVe4E!eM#kupuc");

/// <summary>
///
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static string Encrypt(string val)
{
string result = string.Empty;

var aes = getEncryptionType();

using (MemoryStream msEncrypt = new MemoryStream())
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, iv))
{
using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using(StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(val);
}
}
}
result = Convert.ToBase64String(msEncrypt.ToArray());
}
aes.Clear();
return result;
}

getEncryptionType 返回一个 AESManaged 类,如下所示:

private static AesManaged getEncryptionType()
{
AesManaged aes = new AesManaged();
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
return aes;
}

目前测试方法是这样的:

Random rnd = new Random();

for (int i = 0; i < 50000; i++)
{
int random = rnd.Next(1147483647, int.MaxValue);
Guid guid = dal.getToken(CryptoService.Encrypt(random.ToString()));

if (i % 100 == 0)
addLog(string.Format("{0} new values added", i.ToString()), LogType.Dialog);
}

最佳答案

这是 base 64 编码的标准。有关详细信息,请阅读维基百科文章的“填充”部分。

http://en.wikipedia.org/wiki/Base64

关于c# - AES 管理的加密值总是以 ==?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195513/

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