示例#1
0
        /// <summary>
        /// Imports the <see cref="ManagedAuthenticatedEncryptorDescriptor"/> from serialized XML.
        /// </summary>
        public IAuthenticatedEncryptorDescriptor ImportFromXml(XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            // <descriptor>
            //   <!-- managed implementations -->
            //   <encryption algorithm="..." keyLength="..." />
            //   <masterKey>...</masterKey>
            // </descriptor>

            var configuration = new RsaEncryptorConfiguration();

            var encryptionElement = element.Element("encryption");

            configuration.EncryptionAlgorithmType    = FriendlyNameToType((string)encryptionElement.Attribute("algorithm"));
            configuration.EncryptionAlgorithmKeySize = (int)encryptionElement.Attribute("keyLength");

            var masterKey = ((string)element.Element("key")).ToSecret();

            byte[] unprotectedSecretRawBytes = new byte[masterKey.Length];
            var    segment = new ArraySegment <byte>(unprotectedSecretRawBytes);

            masterKey.WriteSecretIntoBuffer(segment);
            var parameters = JsonConvert.DeserializeObject <RSAParameters>(Encoding.UTF8.GetString(segment.Array));
            var key        = new RsaSecurityKey(parameters)
            {
                KeyId = (string)element.Element("keyId")
            };

            return(new RsaEncryptorDescriptor(configuration, key));
        }
 internal CacheableKeyRing(CancellationToken expirationToken,
                           DateTimeOffset expirationTime,
                           IKey defaultKey,
                           IEnumerable <IKey> allKeys,
                           RsaEncryptorConfiguration configuration) // constructor change to add a RsaEncryptorConfiguration instance
     : this(expirationToken, expirationTime, keyRing : new KeyRing(defaultKey, allKeys, configuration))
 {
 }
 private static Func <RSA> GetAsymmetricBlockCipherAlgorithmFactory(RsaEncryptorConfiguration configuration)
 {
     // basic argument checking
     if (configuration.EncryptionAlgorithmType == typeof(RSA))
     {
         return(RSA.Create);
     }
     else
     {
         return(AlgorithmActivator.CreateFactory <RSA>(configuration.EncryptionAlgorithmType));
     }
 }
示例#4
0
        internal RsaEncryptor CreateAuthenticatedEncryptorInstance(
            RsaSecurityKey secret,
            RsaEncryptorConfiguration configuration)
        {
            if (configuration == null)
            {
                return(null);
            }

            _logger.LogDebug($"Create new {nameof(RsaEncryptor)}");
            return(new RsaEncryptor(secret));
        }
示例#5
0
        public KeyRing(IKey defaultKey, IEnumerable <IKey> allKeys, RsaEncryptorConfiguration configuration) // add RsaEncryptorConfiguration intance from orignal
        {
            _keyIdToKeyHolderMap = new Dictionary <Guid, KeyHolder>();
            foreach (IKey key in allKeys)
            {
                _keyIdToKeyHolderMap.Add(key.KeyId, new KeyHolder(key));
            }

            // It's possible under some circumstances that the default key won't be part of 'allKeys',
            // such as if the key manager is forced to use the key it just generated even if such key
            // wasn't in the underlying repository. In this case, we just add it now.
            if (!_keyIdToKeyHolderMap.ContainsKey(defaultKey.KeyId))
            {
                _keyIdToKeyHolderMap.Add(defaultKey.KeyId, new KeyHolder(defaultKey));
            }

            DefaultKeyId      = defaultKey.KeyId;
            _defaultKeyHolder = _keyIdToKeyHolderMap[DefaultKeyId];
            _configuration    = configuration; // add RsaEncryptorConfiguration instance from orignal
        }
        public RsaEncryptorDescriptor(RsaEncryptorConfiguration configuration)
        {
            Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));

            RsaSecurityKey = GenerateNewKey();
        }
 public RsaEncryptorDescriptor(RsaEncryptorConfiguration configuration, RsaSecurityKey masterKey)
 {
     Configuration  = configuration ?? throw new ArgumentNullException(nameof(configuration));
     RsaSecurityKey = masterKey ?? throw new ArgumentNullException(nameof(masterKey));
 }