X509 static rsacryptoserviceprovider high-concurrency encryption and decryption

Source: Internet
Author: User
/* Makecert.exe-n "cn = microshaoft X509 test-a"-sky exchange-pe-sv. PVK a.cerpvk2pfx.exe-pvk. PVK-spc. cer-pfx. pfx-F-po 123makecert.exe-n "cn = microshaoft X509 test-B"-sky exchange-pe-sv B. PVK B .cerpvk2pfx.exe-pvk B. PVK-spc B. cer-pfx B. pfx-F-po ABC */namespace consoleapplication {using system; using system. io; using system. text; using system. threading; using system. security. cryptography; using system. security. cryptography. x509certificates; public class class1 {static void main (string [] ARGs) {encryptorprivatekeypfxprovider = encryptorprivatekeypfx. privatekey as rsacryptoserviceprovider; encryptorpublickeycerprovider = encryptorpublickeycer. publickey. key as rsacryptoserviceprovider; decryptorpublickeycerprovider = decryptorpublickeycer. publickey. key as rsacryptoserviceprovider; decryptorprivatekeypfxprovider = decryptorprivatekeypfx. privatekey as rsacryptoserviceprovider; For (INT I = 0; I <2000; I ++) {threadstart Ts = new threadstart (run); thread t = new thread (TS); T. name = _ threadid. tostring (); _ threadid ++; T. start (); // run () ;}} Private Static volatile int _ threadid = 0; Private Static object _ synclockobject = new object (); private Static x509certificate2 encryptorprivatekeypfx = new x509certificate2 (@ ". pfx "," 123 "); Private Static x509certificate2 encryptorpublickeycer = new x509certificate2 (@". CER "); Private Static x509certificate2 decryptorpublickeycer = new x509certificate2 (@" B. CER "); Private Static x509certificate2 decryptorprivatekeypfx = new x509certificate2 (@" B. pfx "," ABC "); Private Static rsacryptoserviceprovider identifier = NULL; Private Static rsacryptoserviceprovider encryptorpublickeycerprovider = NULL; Private Static rsacryptoserviceprovider identifier = NULL; static void run () {// todo: addCode To start the application Program // String S = "original plaintext"; byte [] DATA = encoding. utf8.getbytes (s); byte [] signature; bool dooaepadding = false; bool verifyed = false; // specify Signature = NULL; // x509certificate2 encryptorpublickeycer = NULL; /// x509certificate2 decryptorpublickeycer = NULL; // x509certificate2 decryptorprivatekeypfx = NULL; try {// encryptorprivatekeypfx = new x509cer Tificate2 (@ ". pfx "," 123 "); // encryptorpublickeycer = new x509certificate2 (@". CER "); // decryptorpublickeycer = new x509certificate2 (@" B. CER "); // decryptorprivatekeypfx = new x509certificate2 (@" B. pfx "," ABC "); // using (rsacryptoserviceprovider decryptorpublickeycerprovider = decryptorpublickeycer. publickey. key as rsacryptoserviceprovider) {// encrypted data = decryptorpublickeycerprovider. encrypt (Data, Dooaepadding);} byte [] hash = new sha1cryptoserviceprovider (). computehash (data); // using (rsacryptoserviceprovider encryptorprivatekeypfxprovider = encryptorprivatekeypfx. privatekey as rsacryptoserviceprovider) {// signature Signature = encryptorprivatekeypfxprovider. signhash (hash, "sha1");} // using (rsacryptoserviceprovider encryptorpublickeycerprovider = encryptorpublickeycer. publickey. key as rsacr Yptoserviceprovider) {// verifyed = encryptorpublickeycerprovider. verifyhash (hash, "sha1", signature);} // using (rsacryptoserviceprovider decryptorprivatekeypfxprovider = decryptorprivatekeypfx. privatekey as rsacryptoserviceprovider) {// decrypt data = decryptorprivatekeypfxprovider. decrypt (data, dooaepadding);} s = encoding. utf8.getstring (data); console. writeline ("{0}, {1}, {2}", thread. currentthre Ad. Name, verifyed, S);} catch (exception e) {console. writeline (E. tostring ();} finally {// If (encryptorprivatekeypfx! = NULL) // {// encryptorprivatekeypfx. Reset (); //} // If (encryptorpublickeycer! = NULL) // {// encryptorpublickeycer. Reset (); //} // If (decryptorpublickeycer! = NULL) // {// decryptorpublickeycer. Reset ();///}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.