CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Crypt class using Chilkat

178 Views
Copy Code Show/Hide Line Numbers
public class Crypt
{
  public static string OpenSslSign(string privateKeyFile, string content)
  {
    var pkey = new PrivateKey();
    pkey.LoadPemFile(privateKeyFile);
    string pkeyXml = pkey.GetXml();
    var rsa = new Rsa();
    bool success = rsa.UnlockComponent("30-day trial");
    if (success != true) {
      return "";
    }
 
    //  Import the private key into the RSA component:
    success = rsa.ImportPrivateKey(pkeyXml);
    if (success != true) {
      Console.WriteLine(rsa.LastErrorText);
      return "";
    }
    //  OpenSSL uses BigEndian byte ordering:
    rsa.LittleEndian = false;
    //  The resulting signature will be a Base64 string:
    rsa.EncodingMode = "base64";
 
    string base64Sig = rsa.OpenSslSignStringENC("This is the text to be signed.");
    Console.WriteLine(base64Sig);
 
    return base64Sig;
  }
 
  public static string OpenSslVerify(string publicKeyFile, string signedContent)
  {
    var pkey = new PublicKey();
    pkey.LoadOpenSslPemFile(publicKeyFile);
    string pkeyXml = pkey.GetXml();
    var rsa = new Rsa
    {
      VerboseLogging = true,           
    };
    bool success = rsa.UnlockComponent("30-day trial");
    if (success != true)
    {
      return "";
    }
 
    //  Import the private key into the RSA component:
    success = rsa.ImportPublicKey(pkeyXml);
    if (success != true)
    {
      Console.WriteLine(rsa.LastErrorText);
      return "";
    }
    //  OpenSSL uses BigEndian byte ordering:
    //rsa.LittleEndian = false;
    //  The resulting signature will be a Base64 string:
    //rsa.EncodingMode = "base64";
    string realString = rsa.OpenSslVerifyStringENC(signedContent);
    Console.WriteLine(realString);
 
    return realString;
  }
}
by Mikael Henriksson
  August 31, 2010 @ 11:31pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate