Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cryptography 使用PKCS#7进行加密解密_Cryptography_Bouncycastle_Public Key Encryption_Pkcs#7 - Fatal编程技术网

Cryptography 使用PKCS#7进行加密解密

Cryptography 使用PKCS#7进行加密解密,cryptography,bouncycastle,public-key-encryption,pkcs#7,Cryptography,Bouncycastle,Public Key Encryption,Pkcs#7,我想用PKCS#7加密解密数据。 在探索的过程中,我发现了一本书《用Java开始加密》 在本书的第9章中,有一个示例代码如下 import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.*; import java.util.Arrays; import org.bouncycastle.cms.CMSProcessable; import org.bouncycastle.

我想用PKCS#7加密解密数据。 在探索的过程中,我发现了一本书《用Java开始加密》
在本书的第9章中,有一个示例代码如下

import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.*;
import java.util.Arrays;

import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;

/**
 * Example of generating a detached signature.
 */
public class SignedDataExample
    extends SignedDataProcessor
{
    public static void main(String[] args) throws Exception
    {
        KeyStore         credentials = Utils.createCredentials();
        PrivateKey       key = (PrivateKey)credentials.getKey(
                                          Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD);
        Certificate[]    chain = credentials.getCertificateChain(
                                                           Utils.END_ENTITY_ALIAS);
        CertStore        certsAndCRLs = CertStore.getInstance(
                                "Collection", new CollectionCertStoreParameters(
                                                      Arrays.asList(chain)), "BC");
        X509Certificate  cert = (X509Certificate)chain[0];

        // set up the generator
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

        gen.addSigner(key, cert, CMSSignedDataGenerator.DIGEST_SHA224);
        gen.addCertificatesAndCRLs(certsAndCRLs);

        // create the signed-data object
        CMSProcessable data = new CMSProcessableByteArray(
                                                     "Hello World!".getBytes());

        CMSSignedData signed = gen.generate(data, "BC");

        // re-create
        signed = new CMSSignedData(data, signed.getEncoded());

        // verification step
        X509Certificate rootCert = (X509Certificate)credentials.getCertificate(
                                                                Utils.ROOT_ALIAS);
        if (isValid(signed, rootCert))

        {
           System.out.println("verification succeeded");
        }
        else
        {
           System.out.println("verification failed");
        }
     }
  }
我有几个问题

  • 加密数据在哪里?如何将其写入文件
  • 如何从加密数据恢复原始数据
  • 我是否需要向解密加密数据的人发送密钥存储
  • 我应该以什么格式发送加密数据

  • 非常感谢

    发布的代码不包含任何与加密相关的内容。它是关于为示例消息“Hello World!”生成pkcs7消息签名的。请注意,CMS容器既可用于数据加密,也可用于数据签名。请再看一遍;我很确定它应该在那里的某个地方。你能告诉我,为了使用CMS加密和解密,我需要使用哪个类吗?封装数据和加密数据是一样的吗?有没有一种方法,我可以将CMSSignedData作为字节写入文件,并从该字节再次生成CMSSignedData对象?