【发布时间】:2023-01-29 16:37:47
【问题描述】:
我想根据以下列表对我的应用程序进行加密: 1- 首先我想用 sha256 算法加密我的简单字符串 2秒我想用我拥有的私钥签署加密文本 3- 第三,我想将结果字节数组转换为 base64,以便在我的 api 请求中作为标头传递
下面是我的代码,它没有创建正确的签名字符串:
byte[] signedBytes, originalData;
string temp_inBase64;
using (SHA256 hash = SHA256Managed.Create())
{
Encoding enc = Encoding.UTF8;
originalData = hash.ComputeHash(enc.GetBytes(message));
}
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString("xxxxxx"); //Final
try
{
signedBytes = rsa.SignData(originalData, new SHA256CryptoServiceProvider());
temp_inBase64 = Convert.ToBase64String(signedBytes);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return temp_inBase64;
原始数据是我传递给函数的消息, 但是验证结果不正确,,
谁能帮我?
【问题讨论】:
-
请问您可以对您的代码应用正常缩进吗?目前很难阅读。它还会真的如果您可以提供示例数据,包括您从哪里获得期望,请提供帮助。 (顺便说一句,如果您只是在计算后直接返回值,而不是将其保存在局部变量中,我认为代码会更清晰。我认为
temp_inBase64没有添加任何值。) -
这回答了你的问题了吗? Base64 encoding of a SHA256 string