. NET docking Java uses modulus,exponent RSA encryption

Source: Internet
Author: User
Tags modulus

There is a recent job that requires data to be sent to Java in RSA

Although some of the algorithms for the initial standard public key net and Java RSA population are different, the pit is almost finished.

For details, refer to

Http://www.cnblogs.com/dudu/p/csharp-openssl-encrypt-decrypt.html

But someone gave me a straight string of 10 digits.

And then I checked some. NET has a RSAParameters class, I try to turn modulus into Base64 and then generate RSAParameters objects and then

Use Rsacryptoserviceprovider.importparameters Although can be encrypted successfully but encrypted ciphertext reached more than 400 (normal can be parsed by the server ciphertext is 172 bits).

Then I want to write a RSA encryption class out of the Internet to find a bit of encryption formula

New BigInteger ("65537");      New Ten );        New BigInteger (Encoding.Default.GetBytes ("123456"));        var str = M.MODPOW (e, N). ToString ();

This time, the length of the encryption is 172, but the server or decryption failed.

When I tried a lot of different ways, I began to doubt my programmer's life, because in addition to my other departments docking, the Android department docking was successful.

Be forced to helpless I went to find the Java code to see if the encryption is successful

Finally, it was encrypted in Java. Java uses a class

Cipher Ciphe

Then through Baidu to search. NET there is no such class. No related documents found

Finally tried Google. Sure enough, I found it.

Original address
Http://stackoverflow.com/questions/22825663/cipher-selection-for-sslstream-in-net-4-5
I'll post my revised code below.

                byte[] Publickeybyte =Encoding.Default.GetBytes (modulus); byte[] Exponentbyte =Encoding.Default.GetBytes (Exponent); UTF8Encoding Byteconverter=Newutf8encoding (); stringPublickeystring =System.Text.Encoding.Default.GetString (Publickeybyte); stringExponentstring =System.Text.Encoding.Default.GetString (Exponentbyte); BigInteger Publickeybi=Biginteger.parse (publickeystring); BigInteger Exponentbi=Biginteger.parse (exponentstring); byte[] PublicKeyByte2 =Publickeybi.tobytearray (); byte[] ExponentByte2 =Exponentbi.tobytearray (); if(Publickeybyte2.length = =129) Array.resize (refPublicKeyByte2, -); RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); RSAParameters Rsakeyinfo=NewRSAParameters (); Rsakeyinfo.modulus=Publickeybyte2.reverse ().                ToArray (); Rsakeyinfo.exponent=Exponentbyte2.reverse ().                ToArray (); Rsa.                ImportParameters (Rsakeyinfo); byte[] Passwordbyte =byteconverter.getbytes (encryptstring); varciphertext = convert.tobase64string (RSA. Encrypt (Passwordbyte,false));

Finish the sprinkle if this article helps you, I hope you can leave a message below to tell me thank you.

. NET docking Java uses modulus,exponent RSA encryption

Related Article

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.