簡體   English   中英

如何使用 p7b 證書加密 zip 文件並使用 p12 對其進行簽名

[英]how to encrypt zip file with p7b certificate and sign it with p12

我有一個包含公鑰的 p7b 文件,目標是使用此公鑰加密 zip 文件並將文件更改為 .zip.encrypted,然后使用 .p12 文件對內容進行簽名

問題

現在我正試圖弄清楚這是如何工作的。 這只是 pkcs#7 加密還是我稱之為 CMS Evelope? 由於在 p7b 文件中我只有一個公共 rsa 密鑰,我如何才能用它加密整個 zip 文件? 據我所知,您只能使用 RSA 加密來加密小字節。 是否有某種字節流可用於自行加密 zip 文件的每個字節? 或者我是否以某種方式從 p7b 文件的公共 RSA 密鑰中生成 AES 密鑰? 我是否需要將 p7b 證書添加到我的 Windows 帳戶才能訪問里面的數據?

我試過的

我嘗試了 .net 資源,但不知道如何獲得我想要的東西,然后我用谷歌搜索了很多,找到了許多人們如何加密短字符串並嘗試適應的例子,但它從來沒有對我有用。

現在我在 c# 中使用 Bouncy Castle,我想當我嘗試加密一個普通的短字符串時,我能夠從我的 p7b 文件中讀取公鑰,它似乎有效。 但是當我使用我的 zip 文件時,我得到一個異常,我的字節數組太大了。 我用於本次測試的壓縮文件只有 3KB,后面的真實文件將達到~200MB

所以我想我使用 p7b 文件以及充氣城堡解決我的問題的方式是錯誤的。

我的代碼

 var p7bFilePath = @"key\Test.p7b";
 var text = @"zipfile";
 var bytesToEncrypt = File.ReadAllBytes(@"zip\test.zip");
 var certi = ReadCertificate(p7bFilePath);
 var encodedPublicKey = certi.GetPublicKey();
 var encryptEngine = new Pkcs1Encoding(new RsaEngine());
 encryptEngine.Init(true, encodedPublicKey);
 var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));

 Console.WriteLine(encrypted);

好的,因為我之前問過這個問題,只是將其刪除並稍微重寫了一下,希望有人可以幫助我。 我想我是在一位同事的幫助下得出的解決方案:

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;
using System.IO;

namespace pkchwencrypting
{
    class Program
    {
        static void Main(string[] args)
        {
            var certificateData = File.ReadAllBytes("YOUR_p7b_FILE");
            var cert = new X509CertificateParser().ReadCertificate(certificateData);
            //I just wanted to know if I can see the publicKey somehow            
            //var publicKey = cert.GetPublicKey();

            var store = new Pkcs12Store(File.OpenRead("YOUR_p12_File"), "test".ToCharArray());
            var privateKey = store.GetKey("THE_NAME_OF_KEY_YOU_WANT_TO_GET").Key;

            var signedDataGen = new CmsSignedDataGenerator();
            signedDataGen.AddSigner(privateKey, cert, CmsSignedDataGenerator.EncryptionRsa, CmsSignedDataGenerator.DigestSha512);

            var zipContent = new CmsProcessableFile(new FileInfo("YOUR_DATA_FILE"));
            //For me a zip
            var signedData = signedDataGen.Generate(zipContent, true);

            var envDataGen = new CmsEnvelopedDataGenerator();
            envDataGen.AddKeyTransRecipient(cert);

            var sData = new CmsProcessableByteArray(signedData.GetEncoded());
            var enveloped = envDataGen.Generate(sData, CmsEnvelopedDataGenerator.DesEde3Cbc);

            var dos = new DerOutputStream(File.OpenWrite("YOUR_DATA_FILE.zip.encrypted.sig)"));
            var bytesToWrite = enveloped.GetEncoded();
            dos.Write(bytesToWrite, 0, bytesToWrite.Length);
            dos.Flush();
            dos.Close();
        }
    }
}

這可能對某人有所幫助,也許某人可以看看它,如果這確實有意義,但它似乎做了它應該做的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM