This article describes C# custom RSA encryption and decryption, RSA signature and verification classes. Share it for your reference. The specific analysis is as follows:
This C# class customizes RSA encryption and decryption, RSA signature and verification, and includes relevant functions required for RSA encryption, decryption and signature, with detailed annotation instructions.
using System; using ; using ; namespace { /// <summary> /// RSA encryption and decryption, RSA signature and verification /// </summary> public class RSACryption { public RSACryption() { } #region RSA Encryption and Decryption #region RSA key generation /// <summary> /// RSA's key generation Generates private key and public key /// </summary> /// <param name="xmlKeys"></param> /// <param name="xmlPublicKey"></param> public void RSAKey(out string xmlKeys,out string xmlPublicKey) { rsa=new RSACryptoServiceProvider(); xmlKeys=(true); xmlPublicKey = (false); } #endregion #region RSA encryption function //############################################################################## //RSA encryption //Instruction KEY must be an XML line, and the returned string is //There is something to be explained! ! This encryption method has a length limit! ! //############################################################################## //RSA encryption function string public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString ) { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); (xmlPublicKey); PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); CypherTextBArray = (PlainTextBArray, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } //RSA's encryption function byte[] public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString ) { byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); (xmlPublicKey); CypherTextBArray = (EncryptString, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } #endregion #region RSA's decryption function //RSA's decryption function string public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString ) { byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; rsa=new RSACryptoServiceProvider(); (xmlPrivateKey); PlainTextBArray =Convert.FromBase64String(m_strDecryptString); DypherTextBArray=(PlainTextBArray, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } //RSA's decryption function byte public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString ) { byte[] DypherTextBArray; string Result; rsa=new RSACryptoServiceProvider(); (xmlPrivateKey); DypherTextBArray=(DecryptString, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } #endregion #endregion #region RSA digital signature #region Get the Hash description table //Get the Hash description table, public bool GetHash(string m_strSource, ref byte[] HashData) { //Get the Hash description from the string byte[] Buffer; MD5 = ("MD5"); Buffer = ("GB2312").GetBytes(m_strSource); HashData = (Buffer); return true; } //Get the Hash description table public bool GetHash(string m_strSource, ref string strHashData) { //Get the Hash description from the string byte[] Buffer; byte[] HashData; MD5 = ("MD5"); Buffer = ("GB2312").GetBytes(m_strSource); HashData = (Buffer); strHashData = Convert.ToBase64String(HashData); return true; } //Get the Hash description table public bool GetHash( objFile, ref byte[] HashData) { //Get the Hash description from the file MD5 = ("MD5"); HashData = (objFile); (); return true; } //Get the Hash description table public bool GetHash( objFile, ref string strHashData) { //Get the Hash description from the file byte[] HashData; MD5 = ("MD5"); HashData = (objFile); (); strHashData = Convert.ToBase64String(HashData); return true; } #endregion #region RSA signature //RSA signature public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData) { RSA = new (); (p_strKeyPrivate); .RSAPKCS1SignatureFormatter RSAFormatter = new .RSAPKCS1SignatureFormatter(RSA); //The algorithm for setting the signature is MD5 ("MD5"); //Execute the signature EncryptedSignatureData = (HashbyteSignature); return true; } //RSA signature public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData) { byte[] EncryptedSignatureData; RSA = new (); (p_strKeyPrivate); .RSAPKCS1SignatureFormatter RSAFormatter = new .RSAPKCS1SignatureFormatter(RSA); //The algorithm for setting the signature is MD5 ("MD5"); //Execute the signature EncryptedSignatureData = (HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } //RSA signature public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData) { byte[] HashbyteSignature; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); RSA = new (); (p_strKeyPrivate); .RSAPKCS1SignatureFormatter RSAFormatter = new .RSAPKCS1SignatureFormatter(RSA); //The algorithm for setting the signature is MD5 ("MD5"); //Execute the signature EncryptedSignatureData = (HashbyteSignature); return true; } //RSA signature public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData) { byte[] HashbyteSignature; byte[] EncryptedSignatureData; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); RSA = new (); (p_strKeyPrivate); .RSAPKCS1SignatureFormatter RSAFormatter = new .RSAPKCS1SignatureFormatter(RSA); //The algorithm for setting the signature is MD5 ("MD5"); //Execute the signature EncryptedSignatureData = (HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } #endregion #region RSA signature verification public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData) { RSA = new (); (p_strKeyPublic); .RSAPKCS1SignatureDeformatter RSADeformatter = new .RSAPKCS1SignatureDeformatter(RSA); //Specify that the HASH algorithm is MD5 when decrypting ("MD5"); if((HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData) { byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); RSA = new (); (p_strKeyPublic); .RSAPKCS1SignatureDeformatter RSADeformatter = new .RSAPKCS1SignatureDeformatter(RSA); //Specify that the HASH algorithm is MD5 when decrypting ("MD5"); if((HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData) { byte[] DeformatterData; RSA = new (); (p_strKeyPublic); .RSAPKCS1SignatureDeformatter RSADeformatter = new .RSAPKCS1SignatureDeformatter(RSA); //Specify that the HASH algorithm is MD5 when decrypting ("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if((HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData) { byte[] DeformatterData; byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); RSA = new (); (p_strKeyPublic); .RSAPKCS1SignatureDeformatter RSADeformatter = new .RSAPKCS1SignatureDeformatter(RSA); //Specify that the HASH algorithm is MD5 when decrypting ("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if((HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } #endregion #endregion } }
I hope this article will be helpful to everyone's C# programming.