Rsa,java private key encryption, C # public key decryption

Source: Internet
Author: User
Tags base64 asymmetric encryption java format

Do this thing in the pit climbed for 3 days before crawling out, record for the Garden Friends reference. C # Programmer, a project needs to do data interaction with Java, the other side dumped Chimi and a CER certificate to me, and then I want to decrypt its ciphertext. RSA Asymmetric encryption, the other side with the private key encryption, I use the public key decryption. A note about certificates: The certificate type has two. pfx and. CER, where the. PFX certificate contains both the public key and the private key, and the. CER certificate contains only the public key.

C # default RSA only supports public key cryptography and private key decryption. Now the demand is the opposite, so the need to directly use C # built-in encryption class is certainly not feasible. and RSA encryption in C # and Java is not interoperable. Through the multi-data search, the third-party class library Bouncycastle is used to realize the current demand. Specifically, here's the code, which is where the main code snippet is posted:

1. Read the public key from the CER certificate. The public key format in C # is an XML-formatted string that is not the same as the public key format in Java.

/// <summary>///get the public key from the certificate/// </summary>/// <param name= "Cerpath" ></param>/// <returns></returns>Private stringGetpublickeyfromcer (stringCerpath) {X509Certificate2 PUBCRT=NewX509Certificate2 (Cerpath); RSACryptoServiceProvider PubKey=(RSACryptoServiceProvider) pubcrt.     Publickey.key; returnPubKey. Toxmlstring (false);}

2. Convert C # Format public key to Java-formatted public key

/// <summary>///Convert C # format public key to Java format Public key/// </summary>/// <param name= "PublicKey" ></param>/// <returns></returns> Public StaticRsakeyparameters Rsapublickeydotnet2java (stringPublicKey) {XmlDocument doc=NewXmlDocument (); Doc.     LOADXML (PublicKey); BigInteger m=NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("modulus")[0].     InnerText)); BigInteger P=NewBigInteger (1, Convert.frombase64string (Doc. Documentelement.getelementsbytagname ("Exponent")[0].     InnerText)); Rsakeyparameters Pub=NewRsakeyparameters (false, M, p); returnPub;}

3. Public key decryption. Because the other party to the ciphertext is Base64 encoded, so we must first decode. and the encryption fill mode to be set to the same as Java, I set here is "rsa/ecb/pkcs1padding".

/// <summary>///Public Key Decryption/// </summary>/// <param name= "xmlpublickey" >c# format public key </param>/// <param name= "strencryptstring" > Redaction </param>/// <returns></returns> Public Static stringRsadecryptbypublickey (stringXmlpublickey,stringstrencryptstring) {      //get the public keyRsakeyparameters Keyparams =Rsapublickeydotnet2java (Xmlpublickey); //parameters are consistent with the parameters in Java for decryptionIbufferedcipher C = Cipherutilities.getcipher ("rsa/ecb/pkcs1padding"); //the first parameter true-encryption, false-decryption, and the second parameter represents the keyC.init (false, Keyparams); //base64 decoding of ciphertext      byte[] Datafromencrypt =convert.frombase64string (strencryptstring); //decryption      byte[] Outbytes =c.dofinal (Datafromencrypt); //plaintext      stringCleartext =Encoding.Default.GetString (outbytes); returncleartext;}

The above code relies on Bouncycastle to add a reference before using it. Why did such a problem stay in the pit for 3 days? The reason is that Java has returned to my ciphertext format is wrong, so I can not solve it. At that time, the urgent thought that Java and C # can not realize interoperability and decryption it! I finally found the problem, and I lost it to them.

That's why I added two more days to my class .... When you encounter the Unknown block type error when decrypting, it is very possible that the encoding problem is that the ciphertext is not in the correct format.

Rsa,java private key encryption, C # public key decryption

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.