public static void CreatePfXfromPem( string crtFile, string keyFile, string pfxFile, string certificateFilePassword) { string dat; using (StreamReader stream = new StreamReader(crtFile)) { dat = stream.ReadToEnd(); } OpenSSL.Core.BIO certbio = new OpenSSL.Core.BIO(dat); OpenSSL.X509.X509Certificate x = new OpenSSL.X509.X509Certificate(certbio); string datakey; using (StreamReader stream = new StreamReader(keyFile)) { datakey = stream.ReadToEnd(); } OpenSSL.Crypto.CryptoKey key = OpenSSL.Crypto.CryptoKey.FromPrivateKey(datakey, certificateFilePassword); OpenSSL.Core.BIO a = OpenSSL.Core.BIO.File(pfxFile, "wb"); OpenSSL.Core.Stack <OpenSSL.X509.X509Certificate> ca = new OpenSSL.Core.Stack <OpenSSL.X509.X509Certificate>(); OpenSSL.X509.PKCS12 p12 = new OpenSSL.X509.PKCS12(certificateFilePassword, key, x, ca); p12.Write(a); key.Dispose(); p12.Dispose(); a.Dispose(); }
public static byte[] RsaEncrypt(byte[] toEncrypt) { using (OpenSSL.Core.BIO bio = new OpenSSL.Core.BIO(PublicKey)) using (RSA rsa = RSA.FromPublicKey(bio)) { return(rsa.PublicEncrypt(toEncrypt, RSA.Padding.None)); } }
public string RSAPrivateEncrypt(string keyFile, string rawData) { string strEncryptedData = string.Empty; using (OpenSSL.Core.BIO bioPrivateKey = OpenSSL.Core.BIO.File(keyFile, "r")) { using (OpenSSL.Crypto.RSA rsaServiceProvider = OpenSSL.Crypto.RSA.FromPrivateKey(bioPrivateKey)) { byte[] byteEncryptedData = rsaServiceProvider.PrivateEncrypt(new UTF8Encoding().GetBytes(rawData), OpenSSL.Crypto.RSA.Padding.PKCS1); strEncryptedData = BitConverter.ToString(byteEncryptedData).Replace("-", ""); } } return(strEncryptedData); }