RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // Creates a new RANDOM key. RSAParameters privatekey = rsa.ExportParameters(true); RSAParameters publickey = rsa.ExportParameters(false); string sign = SignData1(content, privatekey);
public static string SignData1(string message, RSAParameters privateKey) { //// The array to store the signed message in bytes byte[] signedBytes; using (var rsa = new RSACryptoServiceProvider()) { //// Write the message to a byte array using UTF8 as the encoding. var encoder = new UTF8Encoding(); byte[] originalData = encoder.GetBytes(message); try { //// Import the private key used for signing the message rsa.ImportParameters(privateKey); //// Sign the data, using SHA512 as the hashing algorithm signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA256")); } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } finally { //// Set the keycontainer to be cleared when rsa is garbage collected. rsa.PersistKeyInCsp = false; } } //// Convert the a base64 string before returning // return signedBytes; return Convert.ToBase64String(signedBytes); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)