c#,js, RSA加密签名常用方法汇总;
1,c# .net framework RSA 加密签名;
// privatekey 私钥字符串
byte[] keyData = Convert.FromBase64String(privatekey);
using (CngKey cngKey = CngKey.Import(keyData, CngKeyBlobFormat.Pkcs8PrivateBlob))
using (RSACng rsa = new RSACng(cngKey))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
}
2,c# .net 5 RSA 加密签名;
//跨平台可用;
// privatekey 私钥字符串 byte[] keyData = Convert.FromBase64String(privatekey); int len = keyData.Length; var r = System.Security.Cryptography.RSA.Create(); r.ImportPkcs8PrivateKey(keyData, out len);
//message 待加密字符串 byte[] data = System.Text.Encoding.UTF8.GetBytes(message); r.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
3,js RSA 加密签名;
//Math.SHA256
var pk = '公钥';
var sign = new JSEncrypt();
sign.setPublickKey(pk)
//sign.setPrivateKey(pk);
var signature = sign.sign('message', CryptoJS.SHA256, "sha256");
console.info(signature);