. NET core RSA help class,. netcorersa help

Source: Internet
Author: User
Tags modulus

. NET core RSA help class,. netcorersa help

SolutionOperation is not supported on this platformException

Directly run the Code:

Public class RSAHelper {// <summary> // Private Key signature /// </summary> /// <param name = "signStr"> </param> // /<param name = "privateKey"> </param> // <returns> </returns> public static string RSASign (string signStr, string privateKey) {try {// net // var rsa = new RSACryptoServiceProvider (); // rsa. fromXmlString (privateKey); // byte [] signBytes = rsa. signData (UTF8Encoding. UTF8.GetBytes (signStr), "md5"); // re Turn Convert. toBase64String (signBytes); // net core 2.0 var rsa = RSA. create (); rsa. fromXmlStringExtensions (privateKey); byte [] bytes = rsa. signData (UTF8Encoding. UTF8.GetBytes (signStr), HashAlgorithmName. MD5, RSASignaturePadding. pkcs1); return Convert. toBase64String (bytes);} catch (Exception e) {throw e ;}} /// <summary> /// Public Key signature /// </summary> /// <param name = "encrypt_info"> </param> // <param Name = "publicKey"> </param> // <returns> </returns> public static string RSAEncrypt (string strEncryptInfo, string publicKey) {try {// net // RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); // rsa. fromXmlString (strPublicKey); // byte [] bytes = rsa. encrypt (UTF8Encoding. UTF8.GetBytes (strEncryptInfo), false); // return Convert. toBase64String (bytes); // net core 2.0 var rsa = RSA. create (); PublicKey = RSAPublicKeyJava2DotNet (publicKey); rsa. fromXmlStringExtensions (publicKey); byte [] bytes = rsa. encrypt (UTF8Encoding. UTF8.GetBytes (strEncryptInfo), RSAEncryptionPadding. pkcs1); return Convert. toBase64String (bytes);} catch (Exception e) {throw e ;}} /// <summary> // RSA private key format conversion // </summary> // <param name = "privateKey"> </param> // <returns> </returns> public static string rs1_vat EKeyJava2DotNet (string privateKey) {if (string. isNullOrEmpty (privateKey) {return string. empty;} RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters) PrivateKeyFactory. createKey (Convert. fromBase64String (privateKey); return string. format ("<RSAKeyValue> <Modulus> {0} </Modulus> <Exponent> {1} </Exponent> <P >{2} </P> <Q> {3} </Q> <DP >{4} </DP> <DQ >{5} </DQ> <InverseQ >{6 }</InverseQ> <D> {7} </D> </RSAKeyValue> ", Convert. toBase64String (privateKeyParam. modulus. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. publicExponent. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. p. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. q. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. DP. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. DQ. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. QInv. toByteArrayUnsigned (), Convert. toBase64String (privateKeyParam. exponent. toByteArrayUnsigned ()));} /// <summary> // RSA public key format conversion /// </summary> /// <param name = "publicKey"> </param> // <returns> Format Conversion Result </returns> public static string RSAPublicKeyJava2DotNet (string publicKey) {if (string. isNullOrEmpty (publicKey) {return string. Empty;} RsaKeyParameters publicKeyParam = (RsaKeyParameters) PublicKeyFactory. createKey (Convert. fromBase64String (publicKey); return string. format ("<RSAKeyValue> <Modulus> {0} </Modulus> <Exponent> {1} </Exponent> </RSAKeyValue>", Convert. toBase64String (publicKeyParam. modulus. toByteArrayUnsigned (), Convert. toBase64String (publicKeyParam. exponent. toByteArrayUnsigned ();} // <summary> // public key verification // </s Ummary> /// <param name = "plainText"> </param> /// <param name = "publicKey"> </param> // <param name =" signedData "> </param> // <returns> </returns> public static bool ValidateRsaSign (string plainText, string publicKey, string signedData) {try {// net // RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); // rsa. fromXmlString (publicKey); // return rsa. verifyData (UTF8Encoding. UTF8.GetBytes (p LainText), "md5", Convert. fromBase64String (signedData); // net core 2.0 var rsa = RSA. create (); rsa. fromXmlStringExtensions (publicKey); return rsa. verifyData (UTF8Encoding. UTF8.GetBytes (plainText), Convert. fromBase64String (signedData), HashAlgorithmName. MD5, RSASignaturePadding. pkcs1);} catch (Exception e) {throw e ;}}/// <summary> // System. security. cryptography. RSA Extension Method // </summary> I Nternal static class RSAExtensions {// Operation is not supported on this platform exception will occur in both of the following ways: // RSA. create (). fromXmlString (privateKey) // new RSACryptoServiceProvider (). fromXmlString (privateKey) /// <summary> /// extended FromXmlString /// </summary> /// <param name = "rsa"> </param> /// <param name = "xmlString"> </param> public static void FromXmlStringExtensions (this RSA rsa, string xmlString) {RS AParameters parameters = new RSAParameters (); XmlDocument xmlDoc = new XmlDocument (); xmlDoc. loadXml (xmlString); if (xmlDoc. documentElement. name. equals ("RSAKeyValue") {foreach (XmlNode node in xmlDoc. documentElement. childNodes) {switch (node. name) {case "Modulus": parameters. modulus = (string. isNullOrEmpty (node. innerText )? Null: Convert. FromBase64String (node. InnerText); break; case "Exponent": parameters. Exponent = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "P": parameters. P = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "Q": parameters. Q = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "DP": parameters. DP = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "DQ": parameters. DQ = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "InverseQ": parameters. InverseQ = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. FromBase64String (node. InnerText); break; case "D": parameters. D = (string. IsNullOrEmpty (node. InnerText )? Null: Convert. fromBase64String (node. innerText); break ;}} else {throw new Exception ("Invalid xml rsa key. ");} rsa. importParameters (parameters );} /// <summary> /// extended ToXmlString /// </summary> /// <param name = "rsa"> </param> /// <param name = "includePrivateParameters"> </param> // <returns> </returns> public static string ToXmlStringExtensions (this RSA rsa, bool includePrivateParameters) {RSA Parameters parameters = rsa. exportParameters (includePrivateParameters); return string. format ("<RSAKeyValue> <Modulus> {0} </Modulus> <Exponent> {1} </Exponent> <P >{2} </P> <Q> {3} </Q> <DP >{4} </DP> <DQ >{5} </DQ> <InverseQ >{6 }</InverseQ> <D> {7} </D> </RSAKeyValue> ", parameters. modulus! = Null? Convert. ToBase64String (parameters. Modulus): null, parameters. Exponent! = Null? Convert. ToBase64String (parameters. Exponent): null, parameters. P! = Null? Convert. ToBase64String (parameters. P): null, parameters. Q! = Null? Convert. ToBase64String (parameters. Q): null, parameters. DP! = Null? Convert. ToBase64String (parameters. DP): null, parameters. DQ! = Null? Convert. ToBase64String (parameters. DQ): null, parameters. InverseQ! = Null? Convert. ToBase64String (parameters. InverseQ): null, parameters. D! = Null? Convert. ToBase64String (parameters. D): null );}}View Code

 

Refer:

Http://www.cnblogs.com/dudu/p/dotnet-core-rsa-openssl.html

Https://github.com/dotnet/core/issues/874

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.