question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] CryptographicException: Padding is invalid and cannot be removed.

See original GitHub issue

Library name and version

Azure.Storage.Blobs 12.10.0

Describe the bug

An exception is thrown when doing multiple UploadAsync and DownloadAsync in parallel with ClientSideEncryption enabled.

Exception: System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed when doing

Background information: We are migrating from Microsoft.Azure.Storage.Blob to Azure.Storage.Blobs. While doing a stress test on our service, we started seeing these exceptions. I’ve isolated the code to a single Unit Test which should be helpful to reproduce the issue. I’ve also tried several variants resulting in the same exception:

  • New BlobClient per request
  • Using streams for uploading and downloading

Expected behavior

No exception.

Actual behavior

A System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed when doing is thrown.

Reproduction Steps

[TestMethod]
public async Task TestUploadAsyncScenarios2()
{
    var keyVaultUri = new Uri("https://***.vault.azure.net");
    var keyVaultSecretName = "***";
    var keyVaultTenantId = "***";
    var keyVaultClientId = "***";
    var certificateThumbprint = "***";

    var x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    x509Store.Open(OpenFlags.ReadOnly);
    var certificates = x509Store.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
    var certificate = certificates[0];
    x509Store.Close();

    var credential = new ClientCertificateCredential(keyVaultTenantId, keyVaultClientId, certificate);
    var secretClient = new SecretClient(keyVaultUri, credential);
    var secret = secretClient.GetSecret(keyVaultSecretName);

    var keyResolver = new KeyResolver(credential);
    var keyEncryptionKey = keyResolver.Resolve(secret.Value.Id);

    var clientSideEncryptionOptions = new ClientSideEncryptionOptions(ClientSideEncryptionVersion.V1_0)
    {
        KeyEncryptionKey = keyEncryptionKey,
        KeyResolver = keyResolver,
        KeyWrapAlgorithm = "A256KW"
    };

    var blobConnectionString = "***";
    var blobContainerName = "***";
    var blobName = "test";
    var blobContents = "test content";

    var blobContainerClient = new BlobContainerClient(blobConnectionString, blobContainerName);
    var blobClient = blobContainerClient.GetBlobClient(blobName);
    var encryptedBlobClient = blobClient.WithClientSideEncryptionOptions(clientSideEncryptionOptions);

    await UploadAsync();

    while (true)
    {
        try
        {
            await Task.WhenAll(
                UploadAsync(),
                DownloadAsync(),
                UploadAsync()
            );
        }
        catch (Exception)
        {
            throw;
        }
    }

    Task UploadAsync() => encryptedBlobClient.UploadAsync(new BinaryData(blobContents), new BlobUploadOptions { });
    Task DownloadAsync() => encryptedBlobClient.DownloadContentAsync();
}

Environment

Windows 10 .NET Standard 2.0 Visual Studio 16.10.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bsumtercommented, Dec 7, 2021

@bsumter , Sorry to get back and forth. I just wanted to repro this issue with the same code as yours. The X509StoreAdapter and X509Certificate2Retriever arent a part of the System.Security.Cryptography.X509Certificates namespace. I have checked it for .net standard 2.0 and latest .net core versions (.net 3.1,5,6)

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates?view=netstandard-2.0

Please point out if Im missing out something

My Bad. I’ve updated the initial post. Should be using System.Security.Cryptography.X509Certificates now.

1reaction
SaurabhSharma-MSFTcommented, Dec 3, 2021

@bsumter We are looking into this and get back to you in case need any information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Padding is invalid and cannot be removed?
The padding needs to be the same on both sides. You should explicitly set the padding twice, once for encryption and once for...
Read more >
Cryptographic error "Padding is invalid and cannot be ...
NET 4.7.2 gives the following error in .NET 6 when decrypting existing data: System.Security.Cryptography.CryptographicException: Padding is invalid and cannot ...
Read more >
Padding is invalid and cannot be removed Exception while ...
I am getting exception while I try to decrypt the encrypted string using following code.
Read more >
Padding Is Invalid and Cannot Be Removed: You Must ...
The padding is invalid and cannot be removed PowerShell error appears when your program fails to locate the expected default padding.
Read more >
Solution for "Padding is invalid and cannot be removed
This error relates to encryption. 1. You don't have all the encryted string, so when decrypting, it's not valid. When using CryptoStream, you ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found