C# - 使用PGP加密和解密音频文件(错误:InputStream必须寻求)

发布于 2025-02-03 03:04:03 字数 1682 浏览 3 评论 0原文

上下文:在Azure存储中加密和解密音频文件(.WAV)。 问题:InputStream必须在加密时(加密时)。

我不是C#开发人员:)

谢谢您的帮助,

以下是我正在使用的代码:

 static async Task Main()
    {
        
        string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
        ////Create a unique name for the container
        string containerName = "audioinput";
        string filename = "abc.wav";

        //// Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient sourcecontainer = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = sourcecontainer.GetBlobClient(filename);

        if (sourcecontainer.Exists())
        {

            var sourceStream = new MemoryStream();
            
                //Download blob to MemoryStream
                await blobClient.DownloadToAsync(sourceStream);
                sourceStream.Position = 0;
                
                //OutputStream
                await using var outputStream = new MemoryStream();
                
                //Get encryptionkeys
                EncryptionKeys encryptionKeys;
                using (Stream publicKeyStream = new FileStream(@"...\public.asc", FileMode.Open))
                    encryptionKeys = new EncryptionKeys(publicKeyStream);

                PGP pgp = new PGP(encryptionKeys);

            await pgp.EncryptStreamAsync(sourceStream, outputStream);
            

        }
        else
        {
            Console.WriteLine("container doesn't exist");
        }

    }

Context: Encrypt and Decrypt an audio file (.wav) in Azure Storage.
Issue: inputStream must be seek-able (when encrypting) "await pgp.EncryptStreamAsync(sourceStream, outputStream);"

I'm not a C# Developer :)

Thank you for your help,

Here is the code i'm using:

 static async Task Main()
    {
        
        string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
        ////Create a unique name for the container
        string containerName = "audioinput";
        string filename = "abc.wav";

        //// Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient sourcecontainer = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = sourcecontainer.GetBlobClient(filename);

        if (sourcecontainer.Exists())
        {

            var sourceStream = new MemoryStream();
            
                //Download blob to MemoryStream
                await blobClient.DownloadToAsync(sourceStream);
                sourceStream.Position = 0;
                
                //OutputStream
                await using var outputStream = new MemoryStream();
                
                //Get encryptionkeys
                EncryptionKeys encryptionKeys;
                using (Stream publicKeyStream = new FileStream(@"...\public.asc", FileMode.Open))
                    encryptionKeys = new EncryptionKeys(publicKeyStream);

                PGP pgp = new PGP(encryptionKeys);

            await pgp.EncryptStreamAsync(sourceStream, outputStream);
            

        }
        else
        {
            Console.WriteLine("container doesn't exist");
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文