Get Public key Private key
X509Certificate2 C4 = datacertificate.getcertfromcerfile (path + "\\cer\\xx.pem");
String PublicKey = C4. PublicKey.Key.ToXmlString (false);//Public key
X509Certificate2 C3 = Datacertificate.getcertificatefrompfxfile (path + "\\cer\\yy.pfx", "password");
String Privatekey = C3. Privatekey.toxmlstring (TRUE);//Private key
sorteddictionary<string, string> sparatemp = new sorteddictionary<string, string> ();
Sparatemp.add ("Field name", "content");
String Sparatempjson = Jsonconvert.serializeobject (sparatemp);
String paydata = Program.rsaencrypt (PublicKey, Sparatempjson); Encryption
Backpaydata = Program.rsadecrypt (Privatekey, Backpaydata. Replace ("\ \", "")); Decrypt
Program.cs file encryption and decryption signature code
<summary>
RSA encryption to encrypt longer data, you can use the fragmentation and decryption method
</summary>
<param name= "Xmlpublickey" ></param>
<param name= "M_strencryptstring" ></param>
<returns></returns>
public static string Rsaencrypt (string Xmlpublickey, string plaintext)
{
X509Certificate2 _x509certificate2 = Retrievex509certificate ();
using (RSACryptoServiceProvider rsacryptography =new RSACryptoServiceProvider ())
{
Rsacryptography.fromxmlstring (Xmlpublickey);
byte[] Plaintextdata = encoder.getbytes (plaintext);
int maxblocksize = RSACRYPTOGRAPHY.KEYSIZE/8-11; Maximum encryption block length limit
if (plaintextdata.length <= maxblocksize)
Return convert.tobase64string (Rsacryptography.encrypt (Plaintextdata, false));
using (MemoryStream plaistream = new MemoryStream (plaintextdata))
using (MemoryStream crypstream = new MemoryStream ())
{
byte[] Buffer = new Byte[maxblocksize];
int BlockSize = Plaistream.read (Buffer, 0, maxblocksize);
while (BlockSize > 0)
{
byte[] Toencrypt = new Byte[blocksize];
Array.copy (Buffer, 0, Toencrypt, 0, BlockSize);
byte[] cryptograph = Rsacryptography.encrypt (Toencrypt, false);
Crypstream.write (cryptograph, 0, cryptograph.length);
BlockSize = Plaistream.read (Buffer, 0, maxblocksize);
}
Return convert.tobase64string (Crypstream.toarray (), base64formattingoptions.none);
}
}
}
public static string Rsadecrypt (string Xmlprivatekey, String ciphertext)
{
X509Certificate2 _x509certificate2 = Retrievex509certificate ();
using (RSACryptoServiceProvider rsacryptography = new RSACryptoServiceProvider ())
{
Rsacryptography.fromxmlstring (Xmlprivatekey);
byte[] Ciphertextdata = convert.frombase64string (ciphertext);
int maxblocksize = RSACRYPTOGRAPHY.KEYSIZE/8; Decryption block Maximum length limit
if (ciphertextdata.length <= maxblocksize)
Return encoder.getstring (Rsacryptography.decrypt (Ciphertextdata, false));
using (MemoryStream crypstream = new MemoryStream (ciphertextdata))
using (MemoryStream plaistream = new MemoryStream ())
{
byte[] Buffer = new Byte[maxblocksize];
int BlockSize = Crypstream.read (Buffer, 0, maxblocksize);
while (BlockSize > 0)
{
byte[] Todecrypt = new Byte[blocksize];
Array.copy (Buffer, 0, Todecrypt, 0, BlockSize);
byte[] plaintext = Rsacryptography.decrypt (Todecrypt, false);
Plaistream.write (plaintext, 0, plaintext.length);
BlockSize = Crypstream.read (Buffer, 0, maxblocksize);
}
Return encoder.getstring (Plaistream.toarray ());
}
}
}
Signature part according to the actual situation can choose to call method
#region RSA Digital Signatures
#region Get Hash Description table
//<summary>
//Get hash description form
///</summary>
//<param name= "Strsourc E "> string to be signed </param>
//<param name=" hashdata ">hash description </param>
//<returns></ RETURNS>
public bool Gethash (string strsource, ref byte[] hashdata)
{
Try
{
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create ("MD5");
Buffer = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (strsource);
Hashdata = Md5.computehash (Buffer);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
//<summary>
//Get Hash Description Table
//</summary>
//<param name= "strsource" > String to be signed </ Param>
//<param name= "strhashdata" >hash description </param>
///<returns></returns>
public static bool Gethash (string strsource, ref string strhashdata)
{
Try
{
//Get hash description from String
byte[] B Uffer;
Byte[] Hashdata;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create ("SHA1");
Buffer = System.Text.Encoding.GetEncoding ("Utf-8"). GetBytes (strsource);
Hashdata = Md5.computehash (Buffer);
Strhashdata = convert.tobase64string (Hashdata);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
//<summary>
//Get Hash Description Table
//</summary>
//<param name= "objfile" > Files to be signed </param
//<param name= "hashdata" >hash description </param>
///<returns></returns>
Public BOOL Gethash (System.IO.FileStream objfile, ref byte[] hashdata)
{
Try
{
//Get hash description from file
System.securi Ty. Cryptography.hashalgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create ("MD5");
Hashdata = Md5.computehash (objfile);
Objfile.close ();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
//<summary>
//Get Hash Description Table
//</summary>
//<param name= "objfile" > Files to be signed </param
//<param name= "strhashdata" >hash description </param>
///<returns></returns>
Public BOOL Gethash (System.IO.FileStream objfile, ref string strhashdata)
{
Try
{
//Get hash description from File
byte[] Hash Data;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create ("MD5");
Hashdata = Md5.computehash (objfile);
Objfile.close ();
Strhashdata = convert.tobase64string (Hashdata);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region RSA Signature
//<summary>
//RSA signature
//</summary>
//<param name= "Strkeyprivate" &G t; private key </PARAM>
//<param name= "Hashbytesignature" > Pending signature Hash description </param>
//<param name= " Encryptedsignaturedata "> Signature results </param>
//<returns></returns>
public bool Signatureformatter (String strkeyprivate, byte[] hashbytesignature, ref byte[] encryptedsignaturedata)
{
Try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (strkeyprivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter Rsaformatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter (RSA);
The algorithm for setting the signature is MD5
Rsaformatter.sethashalgorithm ("MD5");
Execute signature
Encryptedsignaturedata = Rsaformatter.createsignature (hashbytesignature);
return true;
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature
</summary>
<param name= "Strkeyprivate" > Private key </param>
<param name= "Hashbytesignature" > Pending signature Hash description </param>
<param name= "M_strencryptedsignaturedata" > Signature results </param>
<returns></returns>
public bool Signatureformatter (string strkeyprivate, byte[] hashbytesignature, ref string strencryptedsignaturedata)
{
Try
{
Byte[] Encryptedsignaturedata;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (strkeyprivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter Rsaformatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter (RSA);
The algorithm for setting the signature is MD5
Rsaformatter.sethashalgorithm ("MD5");
Execute signature
Encryptedsignaturedata = Rsaformatter.createsignature (hashbytesignature);
Strencryptedsignaturedata = convert.tobase64string (Encryptedsignaturedata);
return true;
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature
</summary>
<param name= "Strkeyprivate" > Private key </param>
<param name= "Strhashbytesignature" > Pending signature Hash description </param>
<param name= "Encryptedsignaturedata" > Signature results </param>
<returns></returns>
public bool Signatureformatter (string strkeyprivate, String strhashbytesignature, ref byte[] encryptedsignaturedata)
{
Try
{
Byte[] Hashbytesignature;
Hashbytesignature = convert.frombase64string (strhashbytesignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (strkeyprivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter Rsaformatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter (RSA);
The algorithm for setting the signature is MD5
Rsaformatter.sethashalgorithm ("MD5");
Execute signature
Encryptedsignaturedata = Rsaformatter.createsignature (hashbytesignature);
return true;
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature
</summary>
<param name= "Strkeyprivate" > Private key </param>
<param name= "Strhashbytesignature" > Pending signature Hash description </param>
<param name= "Strencryptedsignaturedata" > Signature results </param>
<returns></returns>
public static bool Signatureformatter (string strkeyprivate, String strhashbytesignature, ref string Strencryptedsignaturedata)
{
Try
{
Byte[] Hashbytesignature;
Byte[] Encryptedsignaturedata;
Hashbytesignature = convert.frombase64string (strhashbytesignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (strkeyprivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter Rsaformatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter (RSA);
The algorithm for setting the signature is MD5
Rsaformatter.sethashalgorithm ("SHA1");
Execute signature
Encryptedsignaturedata = Rsaformatter.createsignature (hashbytesignature);
Strencryptedsignaturedata = convert.tobase64string (Encryptedsignaturedata);
return true;
}
catch (Exception ex)
{
Throw ex;
}
}
#endregion
#region RSA Signature Verification
<summary>
RSA Signature Verification
</summary>
<param name= "Strkeypublic" > Public key </param>
<param name= "Hashbytedeformatter" >hash description </param>
<param name= "Deformatterdata" > Signature results </param>
<returns></returns>
public bool Signaturedeformatter (string strkeypublic, byte[] hashbytedeformatter, byte[] deformatterdata)
{
Try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (Strkeypublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter Rsadeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter (RSA);
Specifies that the hash algorithm is MD5 when decrypting
Rsadeformatter.sethashalgorithm ("MD5");
if (Rsadeformatter.verifysignature (Hashbytedeformatter, Deformatterdata))
{
return true;
}
Else
{
return false;
}
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature Verification
</summary>
<param name= "Strkeypublic" > Public key </param>
<param name= "Strhashbytedeformatter" >hash description </param>
<param name= "Deformatterdata" > Signature results </param>
<returns></returns>
public bool Signaturedeformatter (string strkeypublic, String strhashbytedeformatter, byte[] deformatterdata)
{
Try
{
Byte[] Hashbytedeformatter;
Hashbytedeformatter = convert.frombase64string (Strhashbytedeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (Strkeypublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter Rsadeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter (RSA);
Specifies that the hash algorithm is MD5 when decrypting
Rsadeformatter.sethashalgorithm ("MD5");
if (Rsadeformatter.verifysignature (Hashbytedeformatter, Deformatterdata))
{
return true;
}
Else
{
return false;
}
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature Verification
</summary>
<param name= "Strkeypublic" > Public key </param>
<param name= "Hashbytedeformatter" >hash description </param>
<param name= "Strdeformatterdata" > Signature results </param>
<returns></returns>
public bool Signaturedeformatter (string strkeypublic, byte[] hashbytedeformatter, string strdeformatterdata)
{
Try
{
Byte[] Deformatterdata;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (Strkeypublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter Rsadeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter (RSA);
Specifies that the hash algorithm is MD5 when decrypting
Rsadeformatter.sethashalgorithm ("MD5");
Deformatterdata = convert.frombase64string (Strdeformatterdata);
if (Rsadeformatter.verifysignature (Hashbytedeformatter, Deformatterdata))
{
return true;
}
Else
{
return false;
}
}
catch (Exception ex)
{
Throw ex;
}
}
<summary>
RSA Signature Verification
</summary>
<param name= "Strkeypublic" > Public key </param>
<param name= "Strhashbytedeformatter" >hash description </param>
<param name= "Strdeformatterdata" > Signature results </param>
<returns></returns>
public static bool Signaturedeformatter (string strkeypublic, String strhashbytedeformatter, String strdeformatterdata)
{
Try
{
Byte[] Deformatterdata;
Byte[] Hashbytedeformatter;
Hashbytedeformatter = convert.frombase64string (Strhashbytedeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider ();
Rsa. Fromxmlstring (Strkeypublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter Rsadeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter (RSA);
Specifies that the hash algorithm is MD5 when decrypting
Rsadeformatter.sethashalgorithm ("SHA1");
Deformatterdata = convert.frombase64string (Strdeformatterdata);
if (Rsadeformatter.verifysignature (Hashbytedeformatter, Deformatterdata))
{
return true;
}
Else
{
return false;
}
}
catch (Exception ex)
{
Throw ex;
}
}
#endregion
#endregion
Use OpenSSL to generate public and private key RSA encryption, decryption, and authentication signatures