public void ToKeyFileStringDecryptFailsWithNoPrivateKeyTest()
        {
            // Load Certificate
            X509Certificate2 cert = new X509Certificate2("4096.pfx", string.Empty, X509KeyStorageFlags.Exportable);

            // Make a provider
            AsymmetricBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(cert, true);

            string keyString = asymmetricProvider.ToKeyFileString(true);

            // Clone a new provider from exported keyfile
            IBlobCryptoProvider clonedProvider = ProviderFactory.CreateProviderFromKeyFileString(keyString);

            // Run an encryption loop using the cloned provider
            // which should not have a private key (And thus fail).
            var encryptedStream = clonedProvider.EncryptedStream(streamSample);
            var decryptedStream = clonedProvider.DecryptedStream(encryptedStream);

            byte[] result = new byte[sampleStreamSize];
            decryptedStream.Read(result, 0, result.Length);

            Assert.IsTrue(
                result.SequenceEqual(streamSample.ToArray()),
                "Decrypted data does not match original data");
        }
Esempio n. 2
0
        public static IBlobCryptoProvider CreateProviderFromKeyFileString(string keyFileData)
        {
            if (string.IsNullOrWhiteSpace(keyFileData))
            {
                throw new ArgumentNullException("keyFileData", "You can not provide an empty key file!");
            }

            KeyFileStorage keyStorage;

            try
            {
                keyStorage = JsonConvert.DeserializeObject <KeyFileStorage>(keyFileData);
            }
            catch (JsonReaderException je)
            {
                throw new InvalidKeyFileException("Could not deserialize the provided key file into valid provider metadata. \n" + je);
            }

            IBlobCryptoProvider provider =
                (IBlobCryptoProvider)Activator.CreateInstance(Type.GetType(keyStorage.ProviderType));

            provider.InitializeFromKeyBytes(keyStorage.KeyMaterial);

            return(provider);
        }
        public void ToKeyFileStringPublicOnlyCertificateTest()
        {
            // Load Certificate
            X509Certificate2 cert = new X509Certificate2("4096.pfx", string.Empty, X509KeyStorageFlags.Exportable);

            // Make a provider
            AsymmetricBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(cert, true);

            string keyString = asymmetricProvider.ToKeyFileString(true);

            // Clone a new provider from exported keyfile
            IBlobCryptoProvider clonedProvider = ProviderFactory.CreateProviderFromKeyFileString(keyString);

            // Run an encryption loop using the two providers
            // Should be able to encrypt with the public only clone, and decrypt with the original
            var encryptedStream = clonedProvider.EncryptedStream(streamSample);
            var decryptedStream = asymmetricProvider.DecryptedStream(encryptedStream);

            byte[] result = new byte[sampleStreamSize];
            decryptedStream.Read(result, 0, result.Length);

            Assert.IsTrue(
                result.SequenceEqual(streamSample.ToArray()),
                "Decrypted data does not match original data");
        }
     public async static Task DownloadToFileEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, string path, FileMode mode,
 AccessCondition accessCondition = null, BlobRequestOptions options = null,
 OperationContext operationContext = null)
     {
         using (FileStream fileStream = new FileStream(path, mode))
         {
             await blob.DownloadToStreamEncryptedAsync(provider, fileStream, accessCondition, options, operationContext);
         }
     }
     public async static Task UploadFromStreamEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
 AccessCondition accessCondition = null, BlobRequestOptions options = null,
 OperationContext operationContext = null)
     {
         using (Stream encryptedStream = provider.EncryptedStream(stream))
         {
             await blob.UploadFromStreamAsync(encryptedStream, accessCondition, options, operationContext);
         }
     }
 public async static Task DownloadToFileEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, string path, FileMode mode,
                                                       AccessCondition accessCondition   = null, BlobRequestOptions options = null,
                                                       OperationContext operationContext = null)
 {
     using (FileStream fileStream = new FileStream(path, mode))
     {
         await blob.DownloadToStreamEncryptedAsync(provider, fileStream, accessCondition, options, operationContext);
     }
 }
 public static void UploadFromStreamEncrypted(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
                                              AccessCondition accessCondition   = null, BlobRequestOptions options = null,
                                              OperationContext operationContext = null)
 {
     using (Stream encryptedStream = provider.EncryptedStream(stream))
     {
         blob.UploadFromStream(encryptedStream, accessCondition, options, operationContext);
     }
 }
 public static void DownloadToStreamEncrypted(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
     AccessCondition accessCondition = null, BlobRequestOptions options = null,
     OperationContext operationContext = null)
 {
     using (Stream blobStream = blob.OpenRead(accessCondition, options, operationContext))
     using (Stream decryptedStream = provider.DecryptedStream(blobStream))
     {
         decryptedStream.CopyTo(stream);
     }
 }
     public async static Task UploadFromFileEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, string path, FileMode mode,
 AccessCondition accessCondition = null, BlobRequestOptions options = null,
 OperationContext operationContext = null)
     {
         using (FileStream fileStream = new FileStream(path, mode))
         using (Stream encryptedStream = provider.EncryptedStream(fileStream))
         {
             await blob.UploadFromStreamAsync(encryptedStream, accessCondition, options, operationContext);
         }
     }
 public static void DownloadToStreamEncrypted(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
                                              AccessCondition accessCondition   = null, BlobRequestOptions options = null,
                                              OperationContext operationContext = null)
 {
     using (Stream blobStream = blob.OpenRead(accessCondition, options, operationContext))
         using (Stream decryptedStream = provider.DecryptedStream(blobStream))
         {
             decryptedStream.CopyTo(stream);
         }
 }
 public async static Task UploadFromFileEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, string path, FileMode mode,
                                                       AccessCondition accessCondition   = null, BlobRequestOptions options = null,
                                                       OperationContext operationContext = null)
 {
     using (FileStream fileStream = new FileStream(path, mode))
         using (Stream encryptedStream = provider.EncryptedStream(fileStream))
         {
             await blob.UploadFromStreamAsync(encryptedStream, accessCondition, options, operationContext);
         }
 }
        public void ToKeyFileAndBackTest()
        {
            IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider();

            asymmetricProvider.WriteKeyFile("keyfile.txt");

            IBlobCryptoProvider clonedProvider = ProviderFactory.CreateProviderFromKeyFile("keyfile.txt");

            var encryptedStream = asymmetricProvider.EncryptedStream(streamSample);
            var decryptedStream = clonedProvider.DecryptedStream(encryptedStream);

            byte[] result = new byte[sampleStreamSize];
            decryptedStream.Read(result, 0, result.Length);

            Assert.IsTrue(
                result.SequenceEqual(streamSample.ToArray()),
                "Decrypted data does not match original data");
        }
 public async static Task DownloadToStreamEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
 AccessCondition accessCondition = null, BlobRequestOptions options = null,
 OperationContext operationContext = null)
 {
     using (Stream blobStream = await blob.OpenReadAsync(accessCondition, options, operationContext))
     using (Stream decryptedStream = provider.DecryptedStream(blobStream))
     {
         await decryptedStream.CopyToAsync(stream);
     }
 }
 public async static Task DownloadToStreamEncryptedAsync(this ICloudBlob blob, IBlobCryptoProvider provider, Stream stream,
                                                         AccessCondition accessCondition   = null, BlobRequestOptions options = null,
                                                         OperationContext operationContext = null)
 {
     using (Stream blobStream = await blob.OpenReadAsync(accessCondition, options, operationContext))
         using (Stream decryptedStream = provider.DecryptedStream(blobStream))
         {
             await decryptedStream.CopyToAsync(stream);
         }
 }