使用 SHA1RSA 和现有的 rsa 私钥 c# 对字符串进行签名
Sign string with SHA1RSA and existing rsa private key c#
我有一个 rsa 私钥,我想用它来签署数据。我写了这段代码,但它不起作用:
public string Sign(string text)
{
var sha = new SHA1Managed();
var encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(text);
var digest = sha.ComputeHash(bytes);
var cert = new X509Certificate2(Convert.FromBase64String(PermissionKey));
var rsa = (RSACryptoServiceProvider)cert.PrivateKey;
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
var SignedHashValue = rsa.SignData(digest, sha);
return Convert.ToBase64String(SignedHashValue).Replace("=", "") + "\n";
}
我得到这个异常:
'Cannot find the requested object.
当我尝试创建 X509Certificate2 时:
var cert = new X509Certificate2(Convert.FromBase64String(PermissionKey));
PS:
我厌倦了在 C# 中模拟 android 代码。这是主要的 android:
public static PrivateKey makePrivateKeyFromPermissionKey(String permissionKey)
throws InvalidKeySpecException, NoSuchProviderException, NoSuchAlgorithmException {
KeyFactory keyFactory;
byte[] certificate = decode(
permissionKey
.replaceAll("\n", "")
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "")
);
if (Build.VERSION.SDK_INT < 27) {
keyFactory = KeyFactory.getInstance("RSA", "BC");
} else {
keyFactory = KeyFactory.getInstance("RSA");
}
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(certificate));
}
我找到了答案。我使用 CSharp_easy_RSA_PEM
库来读取私钥。
public static string Sign(string text)
{
var privateRSAkey = Crypto.DecodeRsaPrivateKey(PermissionKey);
var rsa = privateRSAkey;
var hasher = new SHA1CryptoServiceProvider();
var e = new UTF8Encoding(true);
var bytesFirmados = rsa.SignData(e.GetBytes(text), hasher);
return Convert.ToBase64String(bytesFirmados);
}
我有一个 rsa 私钥,我想用它来签署数据。我写了这段代码,但它不起作用:
public string Sign(string text)
{
var sha = new SHA1Managed();
var encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(text);
var digest = sha.ComputeHash(bytes);
var cert = new X509Certificate2(Convert.FromBase64String(PermissionKey));
var rsa = (RSACryptoServiceProvider)cert.PrivateKey;
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
var SignedHashValue = rsa.SignData(digest, sha);
return Convert.ToBase64String(SignedHashValue).Replace("=", "") + "\n";
}
我得到这个异常:
'Cannot find the requested object.
当我尝试创建 X509Certificate2 时:
var cert = new X509Certificate2(Convert.FromBase64String(PermissionKey));
PS: 我厌倦了在 C# 中模拟 android 代码。这是主要的 android:
public static PrivateKey makePrivateKeyFromPermissionKey(String permissionKey)
throws InvalidKeySpecException, NoSuchProviderException, NoSuchAlgorithmException {
KeyFactory keyFactory;
byte[] certificate = decode(
permissionKey
.replaceAll("\n", "")
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "")
);
if (Build.VERSION.SDK_INT < 27) {
keyFactory = KeyFactory.getInstance("RSA", "BC");
} else {
keyFactory = KeyFactory.getInstance("RSA");
}
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(certificate));
}
我找到了答案。我使用 CSharp_easy_RSA_PEM
库来读取私钥。
public static string Sign(string text)
{
var privateRSAkey = Crypto.DecodeRsaPrivateKey(PermissionKey);
var rsa = privateRSAkey;
var hasher = new SHA1CryptoServiceProvider();
var e = new UTF8Encoding(true);
var bytesFirmados = rsa.SignData(e.GetBytes(text), hasher);
return Convert.ToBase64String(bytesFirmados);
}