Search code examples
c#azurepgp

PGP Encryption without files


I 'm doing PGP Encryption for a csv file, below is the code where I 'm stuck, basically below code works if the public key is in local text file however when I 'm having the same file in Azure blob storage, I download the contents in Memory stream and then passing it as parameter it's not working, in short File.OpenRead works but not memory stream, please help

 public static PgpPublicKey ReadPublicKey12()
            {
                var containerName = "pgpkeys";
                string storageConnection = CloudConfigurationManager.GetSetting("StorageConnnection");
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName);
                CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("keyPublic.txt");
                Stream inputStream = new MemoryStream();           
                blockBlob.DownloadToStream(inputStream);
               //  inputStream = File.OpenRead(@"C:\PGPTest\keyPublic1234.txt"); 
                inputStream = PgpUtilities.GetDecoderStream(inputStream);
                PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);

                foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
                {
                    foreach (PgpPublicKey k in kRing.GetPublicKeys())
                    {
                        if (k.IsEncryptionKey)
                            return k;
                    }
                }

                throw new ArgumentException("Can't find encryption key in key ring.");
            }

Solution

  • If we don't reset the stream position to zero (inputStream.Position = 0;) 0 byte blob is being written in to a memory stream, so you need to add that as below.

    var containerName = "pgpkeys";
                string storageConnection = CloudConfigurationManager.GetSetting("StorageConnnection");
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
                CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName);
                CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("keyPublic.txt");
                Stream inputStream = new MemoryStream();           
                blockBlob.DownloadToStream(inputStream);
                inputStream.Position = 0;
    
            inputStream = PgpUtilities.GetDecoderStream(inputStream);
            PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(inputStream);
    
            foreach (PgpPublicKeyRing kRing in pgpPub.GetKeyRings())
            {
                foreach (PgpPublicKey k in kRing.GetPublicKeys())
                {
                    Console.WriteLine("Obtained key from BLOB");
                    if (k.IsEncryptionKey)
                        return k;
                    Console.WriteLine("Obtained key from BLOB");
                }
            }
            throw new ArgumentException("Can't find encryption key in key ring.");