Cryptography 使用PKCS#7进行加密解密
我想用PKCS#7加密解密数据。 在探索的过程中,我发现了一本书《用Java开始加密》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.
在本书的第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对象?