c# - 无文件的 PGP 加密

标签 c# azure pgp

我正在对 csv 文件进行 PGP 加密,下面是我遇到问题的代码,基本上,如果公钥位于本地文本文件中,那么下面的代码就可以工作,但是当我在 Azure blob 存储中拥有相同的文件时,我下载内存流中的内容,然后将其作为参数传递,它不起作用,简而言之 File.OpenRead 可以工作,但不能内存流,请帮助

 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.");
            }

最佳答案

如果我们不将流位置重置为零 (inputStream.Position = 0;),则会将 0 字节 blob 写入内存流,因此您需要添加如下内容。

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.");

关于c# - 无文件的 PGP 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52648661/

相关文章:

android - 将 base64 图像从 Android 发布到 Azure 移动后端

用于容纳应用程序配置的 Azure 表存储

java - BouncycaSTLe 从 1.46 更新到 1.56 不起作用

go - openpgp 和 golang

c# - 如何在 asp.net 中显示 response.write

c# - 将 Youtube/Vimeo 视频嵌入电子邮件模板

c# - 如何动态声明一个类? C#

c# - 我如何测试 Relaycommand?

c++ - 在 C++ 中签署/认证文本文件的最简单方法?

json - 通过 Powershell 定义 Azure 流分析 iot-hub 输入源