Esempio n. 1
0
 public SymmetricKeyFactory([NotNull] ISymmetricAlgorithmTypeReader symmetricAlgorithmTypeReader,
     [NotNull] ISymmetricAlgorithmFactory symmetricAlgorithmFactory)
 {
     if (symmetricAlgorithmTypeReader == null) throw new ArgumentNullException(nameof(symmetricAlgorithmTypeReader));
     if (symmetricAlgorithmFactory == null) throw new ArgumentNullException(nameof(symmetricAlgorithmFactory));
     _symmetricAlgorithmTypeReader = symmetricAlgorithmTypeReader;
     _symmetricAlgorithmFactory = symmetricAlgorithmFactory;
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedNewKeyCipher" /> class.
 /// </summary>
 /// <param name="certificate">
 /// The certificate containing the public and optionally the private encryption keys. Cannot be <see langword="null"/>.
 /// </param>
 /// <param name="symmetricAlgorithmName">
 /// The name of the symmetric algorithm implementation. You can use any of the constants from <see cref="Algorithms.Symmetric" /> or even
 /// <see langword="null" />, empty or whitespace characters only - these will default to <see cref="Algorithms.Symmetric.Default" />.
 /// </param>
 /// <param name="symmetricAlgorithmFactory">
 /// The symmetric algorithm factory. If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.SymmetricAlgorithmFactory" />,
 /// which uses the <see cref="SymmetricAlgorithm.Create(string)" /> method from the .NET library.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown when the <paramref name="certificate"/> is <see langword="null"/>.
 /// </exception>
 public EncryptedNewKeyCipher(
     X509Certificate2 certificate,
     string symmetricAlgorithmName = Algorithms.Symmetric.Default,
     ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
     : base(certificate, symmetricAlgorithmName, symmetricAlgorithmFactory)
 {
     // we do not need symmetric key storage - the key is stored in the crypto-package.
     // InitializeKeyStorage(symmetricKeyLocation, symmetricKeyLocationStrategy, keyStorage);
 }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProtectedKeyCipher" /> class.
 /// </summary>
 /// <param name="symmetricKeyLocation">
 /// Seeding name of store location name of the encrypted symmetric key (e.g. relative or absolute path). Can be <see langword="null" />,
 /// empty or whitespace characters only. The parameter will be passed to the <paramref name="symmetricKeyLocationStrategy" /> to determine the final
 /// store location name path (e.g. relative or absolute path).
 /// </param>
 /// <param name="symmetricKeyLocationStrategy">
 /// Object which implements the strategy for determining the store location name (e.g. path and filename) of the encrypted symmetric key.
 /// If <see langword="null" /> it defaults to a new instance of the class <see cref="DefaultServices.KeyFileLocationStrategy" />.
 /// Alternatively an implementation type can be registered in a common service locator compatible DI container.</param>
 /// <param name="keyStorage">
 /// Object which implements the storing and retrieving of the the encrypted symmetric key to and from the store with the determined location name.
 /// If <see langword="null" /> it defaults to a new instance of the class <see cref="DefaultServices.KeyFileStorage" />.
 /// Alternatively an implementation type can be registered in a common service locator compatible DI container.
 /// </param>
 /// <param name="symmetricAlgorithmName">
 /// The name of the symmetric algorithm implementation. You can use any of the constants from <see cref="Algorithms.Symmetric" /> or even
 /// <see langword="null" />, empty or whitespace characters only - these will default to <see cref="Algorithms.Symmetric.Default" />.
 /// </param>
 /// <param name="symmetricAlgorithmFactory">
 /// The symmetric algorithm factory. If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.SymmetricAlgorithmFactory" />,
 /// which uses the <see cref="SymmetricAlgorithm.Create(string)" /> method from the .NET library.
 /// </param>
 public ProtectedKeyCipher(
     string symmetricKeyLocation = null,
     IKeyLocationStrategy symmetricKeyLocationStrategy = null,
     IKeyStorageTasks keyStorage   = null,
     string symmetricAlgorithmName = Algorithms.Symmetric.Default,
     ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
     : base(symmetricAlgorithmName, symmetricAlgorithmFactory)
 {
     ResolveKeyStorage(symmetricKeyLocation, symmetricKeyLocationStrategy, keyStorage);
 }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EncryptedNewKeyCipher" /> class.
 /// </summary>
 /// <param name="certificate">
 /// The certificate containing the public and optionally the private encryption keys. Cannot be <see langword="null" />.
 /// </param>
 /// <param name="symmetricAlgorithmName">
 /// The name of the symmetric algorithm implementation. You can use any of the constants from <see cref="Algorithms.Symmetric" /> or
 /// <see langword="null" />, empty or whitespace characters only - these will default to <see cref="Algorithms.Symmetric.Default" />.
 /// </param>
 /// <param name="hashAlgorithmName">
 /// The name of the hash algorithm. By default the cipher will pick the algorithm from the <paramref name="certificate"/> but the caller
 /// may choose to use lower length signature key, e.g. the certificate may be for SHA256 but the caller may override that to SHA1.
 /// </param>
 /// <param name="hashAlgorithmFactory">
 /// The hash algorithm factory.
 /// If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.HashAlgorithmFactory" />,
 /// which uses the <see cref="HashAlgorithm.Create(string)" /> method from the .NET library.
 /// </param>
 /// <param name="symmetricAlgorithmFactory">
 /// The symmetric algorithm factory.
 /// If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.SymmetricAlgorithmFactory" />,
 /// which uses the <see cref="SymmetricAlgorithm.Create(string)" /> method from the .NET library.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 /// Thrown when the <paramref name="certificate" /> is <see langword="null" />.
 /// </exception>
 public EncryptedNewKeyHashedCipher(
     X509Certificate2 certificate,
     string hashAlgorithmName      = Algorithms.Hash.Default,
     string symmetricAlgorithmName = Algorithms.Symmetric.Default,
     IHashAlgorithmFactory hashAlgorithmFactory           = null,
     ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
     : base(certificate, symmetricAlgorithmName, symmetricAlgorithmFactory)
 {
     InitializeHasher(hashAlgorithmName, hashAlgorithmFactory);
 }
Esempio n. 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SymmetricKeyCipherBase" /> class by instantiating a symmetric algorithm provider
 /// derived from the <paramref name="symmetricAlgorithmName" />.
 /// </summary>
 /// <param name="symmetricAlgorithmName">
 /// If <see langword="null" /> the algorithm will default to <see cref="Algorithms.Symmetric.Default"/> (AESManaged).
 /// Hint: use the constants in the <see cref="Algorithms.Symmetric" /> static class.
 /// </param>
 /// <param name="symmetricAlgorithmFactory">
 /// The symmetric algorithm factory.
 /// If <see langword="null"/> the constructor will create an instance of the default <see cref="DefaultServices.SymmetricAlgorithmFactory"/>,
 /// which uses the <see cref="SymmetricAlgorithm.Create(string)"/> method from the .NET library.
 /// </param>
 protected SymmetricKeyCipherBase(
     string symmetricAlgorithmName = Algorithms.Symmetric.Default,
     ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
 {
     Symmetric = DefaultServices
                 .Resolver
                 .GetInstanceOrDefault(symmetricAlgorithmFactory)
                 .Initialize(symmetricAlgorithmName)
                 .Create();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="EncryptedNewKeyCipher" /> class.
        /// </summary>
        /// <param name="exchangeCertificate">
        /// The certificate containing the public and optionally the private keys for encrypting the symmetric key. Cannot be <see langword="null"/>.
        /// </param>
        /// <param name="signCertificate">
        /// The certificate containing the public and optionally the private keys for encrypting the hash - signing. Cannot be <see langword="null"/>.
        /// </param>
        /// <param name="symmetricAlgorithmName">
        /// The name of the symmetric algorithm implementation. You can use any of the constants from <see cref="Algorithms.Symmetric" /> or
        /// <see langword="null" />, empty or whitespace characters only - these will default to <see cref="Algorithms.Symmetric.Default" />.
        /// </param>
        /// <param name="hashAlgorithmName">
        /// The name of the hash algorithm. By default the cipher will pick the algorithm from the <paramref name="signCertificate"/> but the caller
        /// may choose to use lower length signature key, e.g. the certificate may be for SHA256 but the caller may override that to SHA1.
        /// </param>
        /// <param name="hashAlgorithmFactory">
        /// The hash algorithm factory.
        /// If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.HashAlgorithmFactory" />,
        /// which uses the <see cref="HashAlgorithm.Create(string)" /> method from the .NET library.
        /// </param>
        /// <param name="symmetricAlgorithmFactory">
        /// The symmetric algorithm factory.
        /// If <see langword="null" /> the constructor will create an instance of the <see cref="DefaultServices.SymmetricAlgorithmFactory" />,
        /// which uses the <see cref="SymmetricAlgorithm.Create(string)" /> method from the .NET library.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when either the <paramref name="exchangeCertificate" /> or the <paramref name="signCertificate" /> is <see langword="null" />.
        /// </exception>
        /// <remarks>
        /// Note that for XML signing the cipher supports only SHA1 and SHA256.
        /// </remarks>
        public EncryptedNewKeySignedCipher(
            X509Certificate2 exchangeCertificate,
            X509Certificate2 signCertificate,
            string hashAlgorithmName      = Algorithms.Hash.Default,
            string symmetricAlgorithmName = Algorithms.Symmetric.Default,
            IHashAlgorithmFactory hashAlgorithmFactory           = null,
            ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
            : base(
                exchangeCertificate,
                !hashAlgorithmName.IsNullOrWhiteSpace() ? hashAlgorithmName : signCertificate.HashAlgorithm(),
                symmetricAlgorithmName,
                hashAlgorithmFactory,
                symmetricAlgorithmFactory)
        {
            if (signCertificate == null)
            {
                throw new ArgumentNullException(nameof(signCertificate));
            }

            _asymmetric = signCertificate.HasPrivateKey
                                    ? (RSACryptoServiceProvider)signCertificate.PrivateKey
                                    : (RSACryptoServiceProvider)signCertificate.PublicKey.Key;
        }
Esempio n. 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProtectedKeyCipher" /> class for initialization by the constructors of the inheriting classes.
 /// </summary>
 /// <param name="symmetricAlgorithmName">
 /// The name of the symmetric algorithm implementation. You can use any of the constants from <see cref="Algorithms.Symmetric" /> or
 /// <see langword="null" />, empty or whitespace characters only - these will default to <see cref="Algorithms.Symmetric.Default" />.
 /// </param>
 /// <param name="symmetricAlgorithmFactory">
 /// The symmetric algorithm factory. If <see langword="null" /> the constructor will create an instance of the default <see cref="DefaultServices.SymmetricAlgorithmFactory" />,
 /// which uses the <see cref="SymmetricAlgorithm.Create(string)" /> method from the .NET library.
 /// </param>
 protected ProtectedKeyCipher(
     string symmetricAlgorithmName,
     ISymmetricAlgorithmFactory symmetricAlgorithmFactory = null)
     : base(symmetricAlgorithmName, symmetricAlgorithmFactory)
 {
 }
 public SymmetricCryptoService([NotNull] ISymmetricAlgorithmFactory symmetricAlgorithmFactory)
 {
     if (symmetricAlgorithmFactory == null) throw new ArgumentNullException(nameof(symmetricAlgorithmFactory));
     _symmetricAlgorithmFactory = symmetricAlgorithmFactory;
 }
Esempio n. 9
0
 public EncryptionService(IMemoryStreamManager memoryStreamManager, ISymmetricAlgorithmFactory symmetricAlgorithmFactory)
 {
     this.memoryStreamManager       = memoryStreamManager;
     this.symmetricAlgorithmFactory = symmetricAlgorithmFactory;
 }