public static string Decrypt(string utf8Data, string privateKey) { RsaPkcs8Util rsa = new RsaPkcs8Util(Encoding.UTF8, null, privateKey); var result = rsa.Encrypt(utf8Data, RSAEncryptionPadding.Pkcs1); return(result); }
public static string Encrypt(string str, string publicKey, string privateKey, int keySize) { using (var rsa = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey, keySize)) { return(rsa.Encrypt(str, RSAEncryptionPadding.Pkcs1)); } }
public static string Encrypt(string data, string publicKey) { RsaPkcs8Util rsa = new RsaPkcs8Util(Encoding.UTF8, publicKey); var result = rsa.Encrypt(data, RSAEncryptionPadding.Pkcs1); return(result); }
/// <summary> /// JavaRsa公钥加密,SHA256,字符长度不能大于公钥长度 /// </summary> /// <param name="text"></param> /// <param name="publicKey"></param> /// <param name="signFormat"></param> /// <returns></returns> public static string JavaRsaEncrypt(string text, string publicKey, RsaSignFormat signFormat = RsaSignFormat.Base64) { var rsaProvider = new RsaPkcs8Util(Encoding.UTF8, publicKey); var encryptData = rsaProvider.Encrypt(text, RSAEncryptionPadding.Pkcs1); if (signFormat == RsaSignFormat.Hex) // => byte => hex { encryptData = ToHexString(Convert.FromBase64String(encryptData)); } return(encryptData); }
/// <summary> /// RSA 加密,解密,签名和验签 /// </summary> public static void RSASignFunc(string privateKey, string publicKey, EnumUserType enumUserType) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); switch (enumUserType) { case EnumUserType.xml: RsaXmlUtil rsaXmlUtil = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey); var encrypt = rsaXmlUtil.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput = rsaXmlUtil.Decrypt(encrypt, RSAEncryptionPadding.Pkcs1); Console.Write("XML加解密是否成功:"); Console.WriteLine(encryptInput is "123456789"); var sign = rsaXmlUtil.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData = rsaXmlUtil.VerifyData("987654321", sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("XML签名是否成功"); Console.WriteLine(verifyData); break; case EnumUserType.pkcs1: RsaPkcs1Util rsaPkcs1Util = new RsaPkcs1Util(Encoding.UTF8, publicKey, privateKey); var encrypt2 = rsaPkcs1Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput2 = rsaPkcs1Util.Decrypt(encrypt2, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS1加解密是否成功:"); Console.WriteLine(encryptInput2 is "123456789"); var sign2 = rsaPkcs1Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData2 = rsaPkcs1Util.VerifyData("987654321", sign2, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS1签名是否成功"); Console.WriteLine(verifyData2); break; default: RsaPkcs8Util rsaPkcs8Util = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey); var encrypt3 = rsaPkcs8Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput3 = rsaPkcs8Util.Decrypt(encrypt3, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS8加解密是否成功:"); Console.WriteLine(encryptInput3 is "123456789"); var sign3 = rsaPkcs8Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData3 = rsaPkcs8Util.VerifyData("987654321", sign3, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS8签名是否成功"); Console.WriteLine(verifyData3); break; } }