Home > Net >  Daniel, can you help me translated into c #!
Daniel, can you help me translated into c #!

Time:09-27

/* Add BC */
Security. AddProvider (new BouncyCastleProvider ());

String pkcs12Path="XXX. PFX";
String keyalias="XXXX";
String keyPwd="* * * * * *";
String smtpServer=xxx.xxx.com ";
String emailAddress="xxx.xxx.com";
String emailPwd="XXXXX";

ArrayList ToAdd=new ArrayList<> (a);
String path="[email protected]";
FileInputStream fi=new FileInputStream (path);
CertificateFactory cf=CertificateFactory. GetInstance (" X. 509 ", "BC");
X509Certificate cert=(X509Certificate) cf. GenerateCertificate (fi);
ToAdd. Add (new AddressInfo (" [email protected] ", cert));

Try {
MailcapCommandMap mailcap=(MailcapCommandMap) CommandMap. GetDefaultCommandMap ();

Mailcap. AddMailcap (" application/pkcs7 - signature;; X - Java - content - handler=org. Bouncycastle. Mail. Smime. Handlers. Pkcs7_signature ");
Mailcap. AddMailcap (" application/pkcs7 - mime;; X - Java - content - handler=org. Bouncycastle. Mail. Smime. Handlers. Pkcs7_mime ");
Mailcap. AddMailcap (" application/x - pkcs7 - signature;; X - Java - content - handler=org. Bouncycastle. Mail. Smime. Handlers. X_pkcs7_signature ");
Mailcap. AddMailcap (" application/x - pkcs7 - mime;; X - Java - content - handler=org. Bouncycastle. Mail. Smime. Handlers. X_pkcs7_mime ");
Mailcap. AddMailcap (" multipart/signed;; X - Java - content - handler=org. Bouncycastle. Mail. Smime. Handlers. Multipart_signed ");

CommandMap. SetDefaultCommandMap (mailcap);

/* Open the keystore */
KeyStore KeyStore=KeyStore. GetInstance (" as PKCS12 ", "BC");
Keystore. Load (new FileInputStream (pkcs12Path), "". ToCharArray ());
Certificate chain []=keystore. GetCertificateChain (keyalias);

/* Get the private key to sign the message with */
PrivateKey PrivateKey=(PrivateKey) keystore. GetKey (keyalias keyPwd. ToCharArray ());
If (privateKey==null) {
Throw new Exception (" always find a private key for alias: "+ keyalias);
}

/* Create the message to sign and encrypt */
The Properties of props=System. The getProperties ();
Props. The put (mail. The SMTP. "the host", smtpServer);
Props. The put (" mail. The SMTP auth ", "true");

The Session Session=Session. GetDefaultInstance (props, new MyAuthenticator (emailAddress, emailPwd));

The MimeMessage body=new MimeMessage (session);
Body. SetFrom (new InternetAddress (emailAddress));
If (toAdd!=null) {
Body. AddRecipients (Message) RecipientType) TO the parseEmailAdd (toAdd));
}
If (ccAdd!=null) {
Body. AddRecipients (Message. RecipientType. CC, parseEmailAdd (ccAdd));
}
If (bccAdd!=null) {
Body. AddRecipients (Message. RecipientType. BCC, parseEmailAdd (bccAdd));
}
Body. SetSubject (subject);
Body. SetContent (textContent, "text/plain");
Body. The saveChanges ();

/* Create the SMIMESignedGenerator */
SMIMECapabilityVector "capabilities=new SMIMECapabilityVector ();
"Capabilities. AddCapability (SMIMECapability. DES_EDE3_CBC);
"Capabilities. AddCapability (SMIMECapability rC2_CBC, 128);
"Capabilities. AddCapability (SMIMECapability. DES_CBC);

ASN1EncodableVector attributes=new ASN1EncodableVector ();
Attributes. The add (new SMIMEEncryptionKeyPreferenceAttribute (new IssuerAndSerialNumber (new X500Name (((X509Certificate) chain [0]). GetIssuerDN (). The getName ()),
((X509Certificate) chain [0]) getSerialNumber ())));
The attributes. The add (new SMIMECapabilitiesAttribute (" capabilities));

SMIMESignedGenerator signer=new SMIMESignedGenerator ();
Signer. AddSignerInfoGenerator (new JcaSimpleSignerInfoGeneratorBuilder (.) setProvider (" BC "). SetSignedAttributeGenerator (new AttributeTable (attributes))
. Build (" DSA. "equals (privateKey. GetAlgorithm ())? "SHA1withDSA" : "MD5withRSA", privateKey, (X509Certificate) chain [0]));

/* Add to the list of certs to the generator */
The List certList=new ArrayList ();
CertList. Add (chain [0]);
Store certs=new JcaCertStore (certList);
Signer. AddCertificates (certs);

/* Sign the message */
MimeMultipart mm=signer. The generate (body);
The MimeMessage signedMessage=new MimeMessage (session);

/* the Set all the original MIME headers in the signed message */
Enumeration headers=body. GetAllHeaderLines ();
While (headers. HasMoreElements ()) {
SignedMessage. AddHeaderLine ((String) headers. NextElement ());
}

/* Set the content of the signed message */
SignedMessage. SetContent (mm);
SignedMessage. SaveChanges ();

/* Create the encrypter */
SMIMEEnvelopedGenerator encrypter=new SMIMEEnvelopedGenerator ();
AddRecipientInfoGenerator (encrypter toAdd);//
AddRecipientInfoGenerator (encrypter ccAdd);
AddRecipientInfoGenerator (encrypter bccAdd);

/* Encrypt the message */
MimeBodyPart encryptedPart=encrypter. Generate (signedMessage, new JceCMSContentEncryptorBuilder (CMSAlgorithm. RC2_CBC). SetProvider (" BC "). The build ());

/*
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#