예제 #1
0
        public static RsaCipher LoadFromPEM(string certFilename, string keyFilename = null)
        {
            var key = new RsaCipherKey();

            try
            {
                var certificate = ReadPEM("CERTIFICATE", certFilename);
                var x509        = new X509Certificate2(certificate);
                #if NETSTANDARD2_0
                key.Public = x509.GetRSAPublicKey();
                #endif

                #if NETFX
                key.Public = (RSACryptoServiceProvider)x509.PublicKey.Key;
                #endif

                if (keyFilename != null)
                {
                    var privateKey = ReadPEM("RSA PRIVATE KEY", keyFilename);
                    RSACryptoServiceProvider prov = DecodeRsaPrivateKey(privateKey);
                    key.Private = prov;
                }
            }
            catch (Exception)
            {
                key.Dispose();
                throw;
            }
            RsaCipher rsaCipher = new RsaCipher();
            rsaCipher._key = key;
            return(rsaCipher);
        }
예제 #2
0
        public RsaCipher Clone()
        {
            EnsureObjectNotDisposed();

            RsaCipher cipher = new RsaCipher();

#if NETSTANDARD2_0
            cipher.RsaEncryptionPadding = RsaEncryptionPadding;
#endif
            cipher._key = _key;
            cipher._key.AddRef();

            return(cipher);
        }
예제 #3
0
        public static RsaCipher LoadFromX509Store(string friendlyName)
        {
            System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(StoreName.My);
            try
            {
                store.Open(OpenFlags.ReadOnly);
                foreach (var x509 in store.Certificates)
                {
                    var cn = x509.FriendlyName;
                    if (cn == friendlyName)
                    {
                        var key = new RsaCipherKey();
                        try
                        {
                            #if NETSTANDARD2_0
                            key.Public  = x509.GetRSAPublicKey();
                            key.Private = x509.GetRSAPrivateKey();
                            #endif

                            #if NETFX
                            key.Public  = (RSACryptoServiceProvider)x509.PublicKey.Key;
                            key.Private = (RSACryptoServiceProvider)x509.PrivateKey;
                            #endif
                        }
                        catch (Exception)
                        {
                            key.Dispose();
                            throw;
                        }

                        RsaCipher rsaCipher = new RsaCipher();
                        rsaCipher._key = key;
                        return(rsaCipher);
                    }
                }
            }
            finally
            {
                #if NETSTANDARD2_0
                store.Dispose();
                #endif
            }
            throw new InternalErrorException("Certificate not found: " + friendlyName);
        }