本文整理汇总了Java中org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator.generate方法的典型用法代码示例。如果您正苦于以下问题:Java SMIMEEnvelopedGenerator.generate方法的具体用法?Java SMIMEEnvelopedGenerator.generate怎么用?Java SMIMEEnvelopedGenerator.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator
的用法示例。
在下文中一共展示了SMIMEEnvelopedGenerator.generate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: cryptMessage
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private static MimeMultipart cryptMessage(MimeBodyPart mbp)
throws Exception
{
// Open the key store
char[] pwd = "password".toCharArray();
KeyStore ks = MailsterKeyStoreFactory.loadKeyStore("PKCS12", "clients.p12", pwd);
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient((X509Certificate) ks.getCertificate(MailsterKeyStoreFactory.DEFAULT_CERT_ALIAS));
// generate the enveloped message
MimeBodyPart envPart = gen.generate(mbp, SMIMEEnvelopedGenerator.AES256_CBC, "BC");
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(envPart);
return multipart;
}
示例2: testHeaders
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void testHeaders()
throws Exception
{
MimeBodyPart _msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient(_reciCert);
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(_msg, SMIMEEnvelopedGenerator.DES_EDE3_CBC, "BC");
assertEquals("application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data", mp.getHeader("Content-Type")[0]);
assertEquals("attachment; filename=\"smime.p7m\"", mp.getHeader("Content-Disposition")[0]);
assertEquals("S/MIME Encrypted Message", mp.getHeader("Content-Description")[0]);
}
示例3: verifyAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private void verifyAlgorithm(
String algorithmOid,
MimeBodyPart msg)
throws Exception
{
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient(_reciCert);
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(msg, algorithmOid, "BC");
SMIMEEnveloped m = new SMIMEEnveloped(mp);
RecipientId recId = getRecipientId(_reciCert);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(_reciKP.getPrivate(), "BC"));
verifyMessageBytes(msg, res);
}
示例4: verifyParserAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private void verifyParserAlgorithm(
String algorithmOid,
MimeBodyPart msg)
throws Exception
{
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient(_reciCert);
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(msg, algorithmOid, "BC");
SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(mp);
RecipientId recId = getRecipientId(_reciCert);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(_reciKP.getPrivate(), "BC"));
verifyMessageBytes(msg, res);
}
示例5: testHeaders
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void testHeaders()
throws Exception
{
MimeBodyPart msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(BC).build());
assertEquals("application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data", mp.getHeader("Content-Type")[0]);
assertEquals("attachment; filename=\"smime.p7m\"", mp.getHeader("Content-Disposition")[0]);
assertEquals("S/MIME Encrypted Message", mp.getHeader("Content-Description")[0]);
}
示例6: verifyAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private void verifyAlgorithm(
String algorithmOid,
MimeBodyPart msg)
throws Exception
{
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(algorithmOid)).setProvider(BC).build());
SMIMEEnveloped m = new SMIMEEnveloped(mp);
RecipientId recId = getRecipientId(_reciCert);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));
verifyMessageBytes(msg, res);
}
示例7: verifyParserAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private void verifyParserAlgorithm(
String algorithmOid,
MimeBodyPart msg)
throws Exception
{
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(algorithmOid)).setProvider(BC).build());
SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(mp);
RecipientId recId = getRecipientId(_reciCert);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));
verifyMessageBytes(msg, res);
}
示例8: testHeaders
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
@Test
public void testHeaders()
throws Exception {
InternetHeaders ih = new InternetHeaders();
ih.addHeader("Content-Type", "application/xml");
MimeBodyPart _msg = new MimeBodyPart(ih, "<customer name=\"bill\"/>".getBytes());
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC)
.setProvider("BC")
.build();
RecipientInfoGenerator generator = new JceKeyTransRecipientInfoGenerator(cert);
gen.addRecipientInfoGenerator(generator);
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(_msg, encryptor);
output(mp);
}
示例9: encrypt
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
/**
* Encrypts the encapsulated MIME body part.
*
* @param cert the certificate for encryption.
* @return an S/MIME message encapsulating the encrypted MIME body part.
* @throws SMimeException if unable to encrpyt the body part.
*/
public SMimeMessage encrypt(X509Certificate cert) throws SMimeException {
try {
try {
if (cert == null) {
throw new SMimeException("No certificate for encryption");
}
setDefaults();
/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.setContentTransferEncoding(getContentTransferEncoding());
// encrypter.addKeyTransRecipient(cert); // Deprecated
encrypter.addRecipientInfoGenerator(
// JceKeyTransRecipientInfoGenerator(X509Certificate,
// [org.bouncycastle.asn1.x509.AlgorithmIdentifier])
new JceKeyTransRecipientInfoGenerator(cert).setProvider(SECURITY_PROVIDER));
/* Encrypt the body part */
MimeBodyPart encryptedPart =
// encrypter.generate(bodyPart, getEncryptAlgorithm(), SECURITY_PROVIDER); // Deprecated
// encryptor.generate(MimeBodyPart, JceCMSContentEncryptorBuilder(
// org.bouncycastle.asn1.ANS1ObjectIdentifier, [int keySize])
encrypter.generate(bodyPart,
new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(getEncryptAlgorithm()))
.setProvider(SECURITY_PROVIDER).build());
return new SMimeMessage(encryptedPart, this);
}
catch (org.bouncycastle.mail.smime.SMIMEException ex) {
throw new SMimeException(ex.getMessage(), ex.getUnderlyingException());
}
}
catch (Exception e) {
throw new SMimeException("Unable to encrypt body part", e);
}
}
示例10: encrypt
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
private MimeBodyPart encrypt(MimeBodyPart bodyPart) throws Exception{
// Create Encrypter
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.setContentTransferEncoding("base64");
encrypter.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(partnershipDVO.getEncryptX509Certificate())
.setProvider(SECURITY_PROVIDER));
// Encrypt BodyPart
MimeBodyPart encryptedPart = encrypter.generate(bodyPart,
new JceCMSContentEncryptorBuilder(
new ASN1ObjectIdentifier(SMIMEEnvelopedGenerator.DES_EDE3_CBC))
.setProvider(SECURITY_PROVIDER).build());
return encryptedPart;
}
示例11: encrypt
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void encrypt(X509Certificate cert, String encryptAlg) throws SFRMException {
try {
/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
encrypter.setContentTransferEncoding("binary");
encrypter.addRecipientInfoGenerator(
new JceKeyTransRecipientInfoGenerator(cert).setProvider(SECURITY_PROVIDER));
/* Encrypt the body part */
if (encryptAlg.equalsIgnoreCase(ALG_ENCRYPT_RC2))
this.bodyPart = encrypter.generate(bodyPart,
new JceCMSContentEncryptorBuilder(
new ASN1ObjectIdentifier(SMIMEEnvelopedGenerator.RC2_CBC))
.setProvider(SECURITY_PROVIDER).build());
else if (encryptAlg.equalsIgnoreCase(ALG_ENCRYPT_3DES))
this.bodyPart = encrypter.generate(bodyPart,
new JceCMSContentEncryptorBuilder(
new ASN1ObjectIdentifier(SMIMEEnvelopedGenerator.DES_EDE3_CBC))
.setProvider(SECURITY_PROVIDER).build());
else
throw new SFRMException("Encryption algorithm error - " + encryptAlg);
this.setIsEncrypted(true);
} catch (org.bouncycastle.mail.smime.SMIMEException ex) {
throw new SFRMException("Unable to encrypt body part", ex.getUnderlyingException());
} catch (Exception e) {
throw new SFRMException("Unable to encrypt body part", e);
}
}
示例12: testCapEncrypt
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void testCapEncrypt()
throws Exception
{
MimeBodyPart _msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
//
// create a subject key id - this has to be done the same way as
// it is done in the certificate associated with the private key
//
MessageDigest dig = MessageDigest.getInstance("SHA1", "BC");
dig.update(_reciCert.getPublicKey().getEncoded());
gen.addKeyTransRecipient(_reciCert.getPublicKey(), dig.digest());
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(_msg, SMIMEEnvelopedGenerator.RC2_CBC, 40, "BC");
SMIMEEnveloped m = new SMIMEEnveloped(mp);
dig.update(_reciCert.getPublicKey().getEncoded());
RecipientId recId = new KeyTransRecipientId(dig.digest());
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
MimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContent(_reciKP.getPrivate(), "BC"));
verifyMessageBytes(_msg, res);
}
示例13: testQuotePrintableSigPreservation
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void testQuotePrintableSigPreservation()
throws Exception
{
MimeMessage msg = new MimeMessage((Session)null, getClass().getResourceAsStream("qp-soft-break.eml"));
SMIMEEnvelopedGenerator encGen = new SMIMEEnvelopedGenerator();
encGen.addKeyTransRecipient(origCert);
MimeBodyPart mp = encGen.generate(msg, SMIMEEnvelopedGenerator.AES128_CBC, "BC");
SMIMEEnveloped env = new SMIMEEnveloped(mp);
RecipientInformation ri = (RecipientInformation)env.getRecipientInfos().getRecipients().iterator().next();
MimeBodyPart mm = SMIMEUtil.toMimeBodyPart(ri.getContentStream(origKP.getPrivate(), "BC"));
SMIMESigned s = new SMIMESigned((MimeMultipart)mm.getContent());
Collection c = s.getSignerInfos().getSigners();
Iterator it = c.iterator();
CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
while (it.hasNext())
{
SignerInformation signer = (SignerInformation)it.next();
Collection certCollection = certs.getCertificates(selectorConverter.getCertSelector(signer.getSID()));
Iterator certIt = certCollection.iterator();
X509Certificate cert = (X509Certificate)certIt.next();
assertEquals(true, signer.verify(cert, "BC"));
}
((FileBackedMimeBodyPart)mm).dispose();
}
示例14: main
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public static void main(
String args[])
throws Exception
{
if (args.length != 3)
{
System.err.println("usage: CreateLargeEncryptedMail pkcs12Keystore password inputFile");
System.exit(0);
}
//
// Open the key store
//
KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
String keyAlias = ExampleUtils.findKeyAlias(ks, args[0], args[1].toCharArray());
Certificate[] chain = ks.getCertificateChain(keyAlias);
//
// create the generator for creating an smime/encrypted message
//
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addKeyTransRecipient((X509Certificate)chain[0]);
//
// create a subject key id - this has to be done the same way as
// it is done in the certificate associated with the private key
// version 3 only.
//
/*
MessageDigest dig = MessageDigest.getInstance("SHA1", "BC");
dig.update(cert.getPublicKey().getEncoded());
gen.addKeyTransRecipient(cert.getPublicKey(), dig.digest());
*/
//
// create the base for our message
//
MimeBodyPart msg = new MimeBodyPart();
msg.setDataHandler(new DataHandler(new FileDataSource(new File(args[2]))));
msg.setHeader("Content-Type", "application/octet-stream");
msg.setHeader("Content-Transfer-Encoding", "binary");
MimeBodyPart mp = gen.generate(msg, SMIMEEnvelopedGenerator.RC2_CBC, "BC");
//
// Get a Session object and create the mail message
//
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
Address fromUser = new InternetAddress("\"Eric H. Echidna\"<[email protected]>");
Address toUser = new InternetAddress("[email protected]");
MimeMessage body = new MimeMessage(session);
body.setFrom(fromUser);
body.setRecipient(Message.RecipientType.TO, toUser);
body.setSubject("example encrypted message");
body.setContent(mp.getContent(), mp.getContentType());
body.saveChanges();
body.writeTo(new FileOutputStream("encrypted.message"));
}
示例15: testTwoRecipients
import org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator; //导入方法依赖的package包/类
public void testTwoRecipients()
throws Exception
{
MimeBodyPart _msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");
SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert2).setProvider(BC));
//
// generate a MimeBodyPart object which encapsulates the content
// we want encrypted.
//
MimeBodyPart mp = gen.generate(_msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 40).setProvider(BC).build());
SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(mp);
RecipientId recId = getRecipientId(_reciCert2);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
FileBackedMimeBodyPart res = SMIMEUtil.toMimeBodyPart(recipient.getContentStream(new JceKeyTransEnvelopedRecipient(_reciKP2.getPrivate()).setProvider(BC)));
verifyMessageBytes(_msg, res);
m = new SMIMEEnvelopedParser(mp);
res.dispose();
recId = getRecipientId(_reciCert);
recipients = m.getRecipientInfos();
recipient = recipients.get(recId);
res = SMIMEUtil.toMimeBodyPart(recipient.getContentStream(new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));
verifyMessageBytes(_msg, res);
res.dispose();
}