本文整理汇总了Java中org.bouncycastle.mail.smime.SMIMEEnveloped类的典型用法代码示例。如果您正苦于以下问题:Java SMIMEEnveloped类的具体用法?Java SMIMEEnveloped怎么用?Java SMIMEEnveloped使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SMIMEEnveloped类属于org.bouncycastle.mail.smime包,在下文中一共展示了SMIMEEnveloped类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decrypt
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
public void decrypt(X509Certificate cert, PrivateKey privateKey) throws SFRMException {
try {
SMIMEEnveloped m = new SMIMEEnveloped(bodyPart);
RecipientId recId = new JceKeyTransRecipientId(cert);
RecipientInformationStore recipientsInfo = m.getRecipientInfos();
RecipientInformation recipientInfo = recipientsInfo.get(recId);
if (recipientInfo == null) {
throw new SFRMMessageException("Invalid encrypted content");
}
JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(privateKey);
recipient.setProvider(SECURITY_PROVIDER);
this.bodyPart = new MimeBodyPart(new ByteArrayInputStream(recipientInfo.getContent(recipient)));
this.setIsEncrypted(true);
} catch (org.bouncycastle.cms.CMSException ex) {
throw new SFRMException("Unable to decrypt body part", ex.getUnderlyingException());
} catch (Exception e) {
throw new SFRMException("Unable to decrypt body part", e);
}
}
示例2: decryptPart
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
private MimeBodyPart decryptPart(MimeBodyPart part) throws Exception {
SMIMEEnveloped smimeEnveloped = new SMIMEEnveloped(part);
RecipientInformationStore recipients = smimeEnveloped.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
if (null == recipient) {
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("This email wasn't encrypted with \"" + recId.toString() + "\".\n");
errorMessage.append("The encryption recId is: ");
for (Object rec : recipients.getRecipients()) {
if (rec instanceof RecipientInformation) {
RecipientId recipientId = ((RecipientInformation) rec).getRID();
errorMessage.append("\"" + recipientId.toString() + "\"\n");
}
}
throw new Exception(errorMessage.toString());
}
return toMimeBodyPart(recipient.getContent(ks.getKey(decryptionKeyAlias, null), BOUNCY_CASTLE_PROVIDER));
}
示例3: decryptMimeBodyPart
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
public static MimeBodyPart decryptMimeBodyPart(MimeBodyPart mbp,
KeyStore ks, char[] password)
throws Exception
{
SMIMEEnveloped enveloped = new SMIMEEnveloped(mbp);
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements())
{
String alias = aliases.nextElement();
if (ks.isKeyEntry(alias))
{
MimeBodyPart decrypted = decryptEnvelope(enveloped,
ks.getKey(alias, password), (X509Certificate) ks.getCertificate(alias));
if (decrypted != null)
return decrypted;
}
}
return null;
}
示例4: decryptEnvelope
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
/**
* Try to decrypt the provided envelope with the provided certificate
* and private key.
*/
public static MimeBodyPart decryptEnvelope(SMIMEEnveloped enveloped,
Key key, X509Certificate cert)
throws Exception
{
// look for our recipient identifier
RecipientId recId = new RecipientId();
recId.setSerialNumber(cert.getSerialNumber());
recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
RecipientInformationStore recipients = enveloped.getRecipientInfos();
RecipientInformation recipient = recipients.get(recId);
// decryption step
if (recipient != null)
return SMIMEUtil.toMimeBodyPart(recipient.getContent(key, "BC"));
else
return null;
}
示例5: verifyAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的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);
}
示例6: testBrokenEnvelope
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
public void testBrokenEnvelope()
throws Exception
{
Session session = Session.getDefaultInstance(System.getProperties(), null);
MimeMessage msg = new MimeMessage(session, getClass().getResourceAsStream("brokenEnv.message"));
try
{
new SMIMEEnveloped(msg);
}
catch (CMSException e)
{
if (!e.getMessage().equals("Malformed content."))
{
fail("wrong exception on bogus envelope");
}
}
}
示例7: verifyAlgorithm
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的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);
}
示例8: getEntity
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
public Object getEntity(Class t, Type gt, Annotation[] ann, PrivateKey pKey, X509Certificate cert) {
final MimeBodyPart decrypted;
try {
MimeBodyPart encryptedBodyPartBase64 = body;
SMIMEEnveloped m = new SMIMEEnveloped(encryptedBodyPartBase64);
JceKeyTransRecipientId recId = new JceKeyTransRecipientId(cert);
RecipientInformationStore recipients = m.getRecipientInfos();
RecipientInformation recipientInfo = recipients.get(recId);
Recipient recipient = new JceKeyTransEnvelopedRecipient(pKey).setProvider("BC");
decrypted = SMIMEUtil.toMimeBodyPart(recipientInfo.getContent(recipient));
} catch (Exception e1) {
throw new RuntimeException(e1);
}
return extractEntity(t, gt, ann, decrypted, providers);
}
示例9: testCapEncrypt
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的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);
}
示例10: testQuotePrintableSigPreservation
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的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();
}
示例11: testDotNetEncMailMatch
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
public void testDotNetEncMailMatch()
throws Exception
{
MimeMessage message = loadMessage("dotnet_encrypted_mail.eml");
SMIMEEnveloped env = new SMIMEEnveloped(message);
RecipientInformationStore store = env.getRecipientInfos();
assertNotNull(store.get(new JceKeyTransRecipientId(loadCert("dotnet_enc_cert.pem"))));
}
示例12: testCapEncrypt
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的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.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(dig.digest(), _reciCert.getPublicKey()).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());
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(new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));
verifyMessageBytes(msg, res);
}
示例13: decode2Mime
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
private MimeBodyPart decode2Mime(MimeBodyPart mp) throws MessagingException, CMSException, SMIMEException {
final Recipient recipient = new JceKeyTransEnvelopedRecipient(privateKey);
final RecipientId recipientId = new JceKeyTransRecipientId(cert);
final SMIMEEnveloped m = new SMIMEEnveloped(mp);
final RecipientInformationStore recipients = m.getRecipientInfos();
final RecipientInformation recipientInformation = recipients.get(recipientId);
return SMIMEUtil.toMimeBodyPart(recipientInformation.getContent(recipient));
}
示例14: decrypt
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
/**
* Decrypts the encapsulated MIME body part.
*
* @param privateKey the private key for decryption.
* @return an S/MIME message encapsulating the decrypted MIME body part.
* @throws SMimeException if unable to decrpyt the body part.
*/
public SMimeMessage decrypt(PrivateKey privateKey) throws SMimeException {
if (privateKey==null) {
throw new SMimeException("Private key not found");
}
try {
setDefaults();
SMIMEEnveloped m = new SMIMEEnveloped(bodyPart);
// RecipientId recId = new RecipientId(); // change to abstract class
//
// recId.setSerialNumber(cert.getSerialNumber());
// recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
RecipientId recId = new JceKeyTransRecipientId(cert);
// RecipientId recId = new RecipientId();
//
// recId.setSerialNumber(cert.getSerialNumber());
// recId.setIssuer(cert.getIssuerX500Principal().getEncoded());
// RecipientInformationStore recipients = m.getRecipientInfos();
// RecipientInformation recipient = recipientsInfo.get(recId);
RecipientInformationStore recipientsInfo = m.getRecipientInfos();
RecipientInformation recipientInfo = recipientsInfo.get(recId);
if (recipientInfo == null) {
throw new SMimeException("Invalid encrypted content");
}
JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(privateKey);
recipient.setProvider(SECURITY_PROVIDER);
// ByteArrayInputStream ins = new ByteArrayInputStream(recipient.getContent(privateKey, "BC")); // Deprecated
ByteArrayInputStream ins = new ByteArrayInputStream(recipientInfo.getContent(recipient));
MimeBodyPart decryptedPart = new MimeBodyPart(ins);
return new SMimeMessage(decryptedPart, this);
}
catch (Exception e) {
throw new SMimeException("Unable to decrypt body part", e);
}
}
示例15: testEncrytedAS2Message
import org.bouncycastle.mail.smime.SMIMEEnveloped; //导入依赖的package包/类
@Test
public void testEncrytedAS2Message() throws Exception{
InputStream ins = FIXTURE_LOADER.getResourceAsStream(MOCK_AS2_MSG);
ByteArrayInputStream bIns = new ByteArrayInputStream(IOHandler.readBytes(ins));
String mid = RANDOM.toString();
partnershipDVO.setIsOutboundEncryptRequired(true);
AS2Message as2Msg = TARGET.storeOutgoingMessage(
mid, //MessageID
"xml",
partnershipDVO,
new InputStreamDataSource(bIns, "xml", MOCK_AS2_MSG));
// Decrypt Message
SMIMEEnveloped crypted = new SMIMEEnveloped(as2Msg.getBodyPart());
// RecipientId recId = new RecipientId();
RecipientId recId = new JceKeyTransRecipientId(partnershipDVO.getEncryptX509Certificate());
RecipientInformationStore recipientsInfo = crypted.getRecipientInfos();
RecipientInformation recipientInfo = recipientsInfo.get(recId);
KeyStoreManager keyMan = (KeyStoreManager)TARGET.getSystemModule().getComponent("keystore-manager");
JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(keyMan.getPrivateKey());
recipient.setProvider(SECURITY_PROVIDER);
MimeBodyPart decrpted = SMIMEUtil.toMimeBodyPart(recipientInfo.getContent(recipient));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOHandler.pipe( decrpted.getDataHandler().getInputStream(), baos);
byte[] decrptedBA = baos.toByteArray();
byte[] originalBA = IOHandler.readBytes(FIXTURE_LOADER.getResourceAsStream(MOCK_AS2_MSG));
Assert.assertTrue(Arrays.equals(decrptedBA, originalBA));
//Assert the filename
String filenameHdr = decrpted.getHeader("Content-Disposition")[0];
Assert.assertEquals("Filename value lost in BodyPartHeader",
MOCK_AS2_MSG, getFileName(filenameHdr));
//Verify MIC
ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
decrpted.writeTo(contentStream);
byte[] content = (contentStream.toByteArray());
String mic = calculateMIC(content);
Assert.assertEquals( "MIC Value is not valid.", mic, getStoredMessage(mid).getMicValue());
}