没有文件的 PGP 加密
PGP Encryption without files
我正在为一个 csv 文件做 PGP 加密,下面是我卡住的代码,如果 public 密钥在本地文本文件中,下面的代码基本上可以工作但是当我有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.");
我正在为一个 csv 文件做 PGP 加密,下面是我卡住的代码,如果 public 密钥在本地文本文件中,下面的代码基本上可以工作但是当我有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.");