Current location - Quotes Website - Signature design - Does anyone know how to write the code for the RSA encryption algorithm in C language? Thanks
Does anyone know how to write the code for the RSA encryption algorithm in C language? Thanks

#region RSA encryption function //################################## #########################################

//RSA encryption

//Explanation that KEY must be in XML line format, and the returned string is a string

//There is one point that needs 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();

rsa.FromXmlString(xmlPublicKey);

PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);

CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);

Result = Convert.ToBase64String(CypherTextBArray);

return Result;

} // RSA encryption function byte[]

public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)

{

byte[] CypherTextBArray; string Result;< /p>

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsa.FromXmlString(xmlPublicKey);

CypherTextBArray = rsa.Encrypt(EncryptString, false);

Result = Convert.ToBase64String(CypherTextBArray);

return Result;

} #endregion

#region RSA decryption function //RSA decryption function string

public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)

{

byte[] PlainTextBArray;

byte[] DypherTextBArray;

string Result;

System.Security. Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsa.FromXmlString(xmlPrivateKey);

PlainTextBArray = Convert.FromBase64String(m_strDecryptString);

DypherTextBArray = rsa .Decrypt(PlainTextBArray, false);

Result = (new UnicodeEncoding()).GetString(DypherTextBArray);

return Result;

}

//RSA decryption function byte public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)

{

byte[] DypherTextBArray;

string Result;

System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsa.FromXmlString(xmlPrivateKey);

DypherTextBArray = rsa.Decrypt (DecryptString, false);

Result = (new UnicodeEncoding()).GetString(DypherTextBArray);

return Result;

} #endregion

#endregion