public Stream DecryptedStream(Stream streamToDecrypt)
        {
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                using (ICspProxy csp = CspFactory.GetProvider())
                {
                    // Get the AES key from the stream
                    // The length will be the size of the RSA key which was used to encrypt
                    // the 256 bit AES key (assuming the RSA key is always larger than 256 bit).
                    byte[] encryptedKey = new byte[AsymmetricKeySize / 8];
                    byte[] decryptedKey;
                    streamToDecrypt.Read(encryptedKey, 0, encryptedKey.Length);
                    decryptedKey = csp.Decrypt(encryptedKey);

                    // Attempt to read IV from Stream
                    byte[] ivBytes = new byte[aesAlg.BlockSize / 8];
                    streamToDecrypt.Read(ivBytes, 0, ivBytes.Length);

                    // Set key and initialization vector
                    aesAlg.Key = decryptedKey;
                    aesAlg.IV  = ivBytes;

                    // Create a decryptor to perform the stream transform.
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor();

                    CryptoStream cryptoStream = new CryptoStream(streamToDecrypt, decryptor, CryptoStreamMode.Read);

                    return(cryptoStream);
                }
        }
        public string ToKeyFileString(bool publicOnly)
        {
            KeyFileStorage keyStorage;

            byte[] temporaryCspBlob;

            using (ICspProxy csp = CspFactory.GetProvider())
            {
                if (publicOnly)
                {
                    temporaryCspBlob = csp.PublicKeyBlob;
                }
                else
                {
                    temporaryCspBlob = csp.KeyBlob;
                }

                keyStorage = new KeyFileStorage
                {
                    KeyMaterial        = temporaryCspBlob,
                    ProviderType       = GetType().ToString(),
                    ContainsPrivateKey = !publicOnly && !csp.IsPublicOnly
                };
            }

            return(JsonConvert.SerializeObject(keyStorage));
        }
        public string ToKeyFileString(bool publicOnly)
        {
            KeyFileStorage keyStorage;

            byte[] temporaryCspBlob;

            using (ICspProxy csp = CspFactory.GetProvider())
            {
                if (publicOnly)
                {
                    temporaryCspBlob = csp.PublicKeyBlob;
                }
                else
                {
                    temporaryCspBlob = csp.KeyBlob;
                }

                keyStorage = new KeyFileStorage
                {
                    KeyMaterial        = temporaryCspBlob,
                    ProviderType       = GetType().ToString(),
                    ContainsPrivateKey = !publicOnly && !csp.IsPublicOnly
                };
            }

            /*
             * using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
             * {
             *  rsa.ImportCspBlob(CspBlob);
             *
             *  if (publicOnly && !rsa.PublicOnly)
             *  {
             *      temporaryCspBlob = rsa.ExportCspBlob(false);
             *  }
             *  else
             *  {
             *      temporaryCspBlob = CspBlob;
             *  }
             *
             *  keyStorage = new KeyFileStorage
             *  {
             *      KeyMaterial = temporaryCspBlob,
             *      ProviderType = GetType().ToString(),
             *      ContainsPrivateKey = !publicOnly && !rsa.PublicOnly
             *  };
             * }
             */

            return(JsonConvert.SerializeObject(keyStorage));
        }
        public Stream EncryptedStream(Stream streamToEncrypt)
        {
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                using (ICspProxy csp = CspFactory.GetProvider())
                {
                    // Set new key but retain randomized IV created during provider instantiation.
                    aesAlg.Key = GenerateRandomKey();
                    byte[] encryptedKey = csp.Encrypt(aesAlg.Key);

                    // Create an encryptor to perform the stream transform.
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                    MemoryStream keyStream    = new MemoryStream(encryptedKey);
                    MemoryStream ivStream     = new MemoryStream(aesAlg.IV);
                    CryptoStream cryptoStream = new CryptoStream(streamToEncrypt, encryptor, CryptoStreamMode.Read);

                    return(new ConcatenatedStream(keyStream, ivStream, cryptoStream));
                }
        }