What is the difference between C # and Java and contact _c# Tutorial

Source: Internet
Author: User
Tags base64 modulus wrapper stringbuffer

Because the company is in the same use. NET and Java, and each service set uses an interface for communication, so a more secure system, such as a clearing system or a cashier, uses RSA for encryption. So it will involve the conversion of the secret key. So I probably saw the secret key of C # and Java secret key difference.

RSA is no different from the program itself, and its format is the same. For different programs, the syntax that the store uses (the wrapper class) will vary.

There are many RSA grammatical and grammatical standards, and the large types are probably divided into ASN.1, PKCS and X.509.

Introduction to RSA Syntax

ASN.1, a PKCS is the first and most important grammatical standard for RSA public and private keys, maintained by the RSA Lab.

ASN.1, pkcs#1 both define the type of public and private key--serialized digits. For the next level of abstraction (the appropriate wrapper), the combination now commonly used is: Pkcs#8 's private key, X.509 's public key.

The PKCS syntax is mainly used for private keys, which have 10 internal standards. Currently, Java is commonly used pkcs#8, as the private key format.

The X.509 syntax is primarily used for public keys, widely used in Web browsers and SLL.

The common private keys of 3 grammatical standards can be transformed into each other, the core of which is the integer value (modulus,publicexponent,privateexponent) in the ASN1 syntax.

. NET uses the standard RSA format, and then encodes the digital base64 to generate XML for storage.

Java uses the pkcs#8, X.509 public key syntax, stored in the corresponding Java class automatically generated Base64 strings.

Because of the difference in storage format, it is necessary to understand the RSA-related knowledge when converting and reading each other in order to correctly use the class to convert.

C # turn Java

The public key in C # is stored using an XML string, which is read directly into the string.

Because C # uses the standard RSA format, the core parameters of the Java Rsapublickeyspec, Rsaprivatekeyspec configuration class (Modulus,publicexponent,privateexponent Can be obtained from the node values in the corresponding XML (Modulus-modulus, Exponent-publicexponent, d-privateexponent) Base64 decoded. It is then passed into the Java configuration class and the corresponding RSA public key is generated based on the configuration class.

Java to C #

The public private key in Java is stored using Base64, and after decoding into byte array, it needs to be the corresponding configuration object (Pkcs#8, X.509) to generate the RSA public key according to the configuration.

byte[] m = base64.decodebase64 ("mx/9zl8rflh5plap5p1qd/9wxwnbsx7opllydngr7wd0njidfpsukgf9of5ncvzwl24qdj1slmrgutnr+
Yexbnznkaan1xxkishdlhvbw5g8njcjw6cuahmkvw3y7kwaiiludv09vxfjj0aoabttjbtf1kqetzbq6fk3en6sy5u= ");
Byte[] e = base64.decodebase64 ("Aqab");
BigInteger B1 = new BigInteger (1, m);
BigInteger b2 = new BigInteger (1, e); Byte[] M1 = Base64.decodebase64 (" 3rgqp5yoyuxft8yoldphyacoof27msftd2evcfvxb5hatrls1fsucmuuwugv970ss6kqzztywhq5970sczkflq82he8uoe0jm3axbvd6pbsgjulujr62qnw5h
Gkiefxsryl8aqsbbusftks4obfepsfe02clmmzepnzadiowife= "); byte[] e1 = base64.decodebase64 ("qcszdlbhakolxx4gajpnunmwsbdrisss7o0qeqmh02gpwoegdfkmw20bv+8q9fpypeekyqu/
M25ffafq453qvlegyyi8ovwn+dvgchqrdeb22d+s6xyggn9drcpfre48inde8fbhf/lzvgtov75h1h7g+jb4hlmleuiuhsb43/0= ");
BigInteger B11 = new BigInteger (1, M1);
BigInteger B21 = new BigInteger (1, E1);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
Rsapublickeyspec Keyspec = new Rsapublickeyspec (B1, B2); Rsapublickey PubKey = (rsapublickey) keyfactory.generatepublic (keYSPEC);
Rsaprivatekeyspec Prikeyspec = new Rsaprivatekeyspec (B11, B21); Rsaprivatekey Prikey = (rsaprivatekey) keyfactory.generateprivate (PRIKEYSPEC);

Private

C # uses the standard RSA format, and the PKCS#1 syntax contains all the integer values in the standard RSA format private key. The configuration object needs to generate the RSA object (Rsaprivatecrtkey) of the pkcs#1 syntax, get the object properties, and construct the private key XML itself.

private static String Getrsaprivatekeyasnetformat (byte[] encodedprivatekey) {try {stringbuffer buff = new Stringbuffe 
R (1024); 
Pkcs8encodedkeyspec Pvkkeyspec = new Pkcs8encodedkeyspec (Encodedprivatekey); 
Keyfactory keyfactory = keyfactory.getinstance ("RSA"); 
Rsaprivatecrtkey Pvkkey = (rsaprivatecrtkey) keyfactory.generateprivate (PVKKEYSPEC); 
Buff.append ("<RSAKeyValue>"); Buff.append ("<Modulus>" + encodeBase64 (Removemszero (Pvkkey.getmodulus (). Tobytearray ())) + "</Modulus>"
); Buff.append ("<Exponent>" + encodeBase64 (Removemszero (Pvkkey.getpublicexponent () Tobytearray ()) + "</
Exponent> ");
Buff.append ("<P>" + encodeBase64 (Removemszero (PVKKEY.GETPRIMEP (). Tobytearray ())) + "</P>");
Buff.append ("<Q>" + encodeBase64 (Removemszero (Pvkkey.getprimeq (). Tobytearray ())) + "</Q>"); 
Buff.append ("<DP>" + encodeBase64 (Removemszero (PVKKEY.GETPRIMEEXPONENTP (). Tobytearray ())) + "</DP>"); Buff.append ("<DQ>" + encodeBase64) (removeMszero (PVKKEY.GETPRIMEEXPONENTQ (). Tobytearray ()) + "</DQ>"); Buff.append ("<InverseQ>" + encodeBase64 (Removemszero (Pvkkey.getcrtcoefficient (). Tobytearray ())) + "</
Inverseq> "); 
Buff.append ("<D>" + encodeBase64 (Removemszero (Pvkkey.getprivateexponent (). Tobytearray ())) + "</D>"); 
Buff.append ("</RSAKeyValue>"); 
return buff.tostring (); 
catch (Exception e) {System.err.println (e); 
return null; } 
}

Public

The public key is the same as the private key generation step, and the generated standard RSA object (Rsapublickey) is configured.

private static String Getrsapublickeyasnetformat (byte[] encodedpublickey) { 
try { 
StringBuffer buff = new StringBuffer (1024);
Only Rsapublickeyspec and X509encodedkeyspec supported for RSA public keys 
keyfactory keyfactory = Keyfactory.getin Stance ("RSA"); 
Rsapublickey Pukkey = (rsapublickey) keyfactory.generatepublic (new X509encodedkeyspec (Encodedpublickey)); 
Buff.append ("<RSAKeyValue>"); 
Buff.append ("<Modulus>" + encodeBase64 (Removemszero (Pukkey.getmodulus (). Tobytearray ())) + "</Modulus>" ); 
Buff.append ("<Exponent>" + encodeBase64 (Removemszero (Pukkey.getpublicexponent (). Tobytearray ())) + "</ Exponent> "); 
Buff.append ("</RSAKeyValue>"); 
return buff.tostring (); 
} catch (Exception e) { 
System.err.println (e); 
return null; 
} 
}

The above is a small series to introduce you to the C # and Java What is the difference and contact, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.