Example #1
0
        public byte[] Decrypt(byte[] value)
        {
            // Load the certificate if not already loaded
            if (_publicKey == null || _privateKey == null)
            {
                var cert = CertificateUtilities.GetCertificateFromSerial(_state.Serial);
                if (cert == null || !cert.HasPrivateKey)
                {
                    var pkcs = CertificateUtilities.GetPkcs11CertificateFromSerial(_state.Serial);
                    if (pkcs == null)
                    {
                        throw new ArgumentException("A certificate with this serial could not be found.", nameof(_state.Serial));
                    }

                    _publicKey  = pkcs.GetRSAPublicKey();
                    _privateKey = pkcs.GetRSAPrivateKey();
                }
                else
                {
                    _publicKey  = cert.PublicKey.Key;
                    _privateKey = cert.PrivateKey as RSACng;
                }
            }

            if (_rawKey == null)
            {
                _rawKey = new RsaCryptoProvider(_privateKey).Decrypt(_state.ProviderKey);
            }

            // Decrypt the value
            return(new AesCryptoProvider(new CryptoKey(new CryptoKeyProtector("None", _rawKey))).Decrypt(value));
        }