private SessionKeyInfo EncryptKey()
        {
            var rsa = UidaiKey.GetRSAPublicKey();

            if (rsa == null)
            {
                throw new ArgumentNullException(nameof(UidaiKey), NoPublicKey);
            }

            // For normal session key, syncKey is always null.
            var key = Convert.ToBase64String(syncKey ?? rsa.Encrypt(aes.Key, RSAEncryptionPadding.Pkcs1));

            GenerateKey();

            return(new SessionKeyInfo
            {
                CertificateIdentifier = UidaiKey.NotAfter,
                Key = key,
                KeyIdentifier = KeyIdentifier
            });
        }
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <param name="disposing">true if the method call comes from a <see cref="Dispose(bool)"/>; otherwise false.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             aes.Dispose();
             random?.Dispose();
             if (!leaveOpen)
             {
                 UidaiKey.Dispose();
             }
             if (seedKey != null)
             {
                 Array.Clear(seedKey, 0, seedKey.Length);
             }
             if (syncKey != null)
             {
                 Array.Clear(syncKey, 0, syncKey.Length);
             }
         }
         disposedValue = true;
     }
 }