2

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.");
            }
harsh
  • 43
  • 1
  • 6
  • OpenRead method returns a FileStream object so if it is working you should convert the inputstream to a FileStream object after DownloadToStream call and try – Nabawoka Oct 04 '18 at 14:11
  • To convert you can use inputstream.CopyTo(objectofFileStream) after setting the position of stream to 0 https://stackoverflow.com/questions/18766055/copy-memorystream-to-filestream-and-save-the-file – Nabawoka Oct 04 '18 at 14:17
  • Thanks @Nabawoka, setting inputStream.Position = 0; worked for me. – harsh Oct 05 '18 at 02:30

1 Answers1

0

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.");
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
ravi teja
  • 26
  • 3
  • Can you please explain what the key differences in your code is to the original in the question, and what the underlying issue that it solves is? That would make for a better answer. – Dijkgraaf Oct 04 '18 at 21:18
  • @Dijkgraaf, the code worked for me .. the key line is inputStream.Position = 0; – harsh Oct 05 '18 at 02:31
  • @harsh Good that it worked for you. I was just giving a hint as to how ravi could improve his answer, as code only answers without explanation are considered low quality. If his answer solved your problem, then use the check mark next to his question to mark it as the accepted answer. Later when you have enough points, you can also upvote answer that helped you. – Dijkgraaf Oct 05 '18 at 03:08
  • @Dijkgraaf, When we try to write a byte array to a memory stream, if we don't reset the stream position to zero (inputStream.Position = 0;) 0 byte blob is being written in to a memory stream. – ravi teja Oct 05 '18 at 18:33
  • @raviteja Yes, I know all about having to rewind streams from programming C# pipeline components for BizTalk. But you need to add that sort of information into the answer, rather than in the comments. – Dijkgraaf Oct 06 '18 at 00:11