SoFunction
Updated on 2025-03-02

RSA key--Difference and connection between JAVA and C#

First of all, thanks:/Articles/25487/Cryptographic-Interoperability-KeysArticles.

Because the company uses it at the same time.NETandJAVA, and each service set uses interfaces to communicate, so some systems with higher security such as clearing systems and cashiers will use RSA for encryption. Therefore, the conversion of the key will be involved. So I roughly looked at the difference between the C# key and the JAVA key.
RSAThere is no difference between the program itself, and its formats are the same. The syntax used by the storage (wrapped class) will vary for different programs.
RSAThere are many grammar and grammar standards, and the large types are roughly divided intoASN.1PKCSX.509

1. Introduction to RSA grammar

ASN.1、PKCSIt is the original and most important syntax standard for RSA public and private keys,RSA LabMaintained.
ASN.1、PKCS#1Both define the types of public and private keys - serialized numbers.

For the next level of abstraction (appropriate wrapping), the combinations commonly used are now:PKCS#8private key,X.509public key .

PKCSSyntax is mainly used forPrivate key, there are currently 10 internal standards. at presentJAVAIt is commonly usedPKCS#8, used as the private key format.
X.509Syntax is mainly used forPublic Key, widely used inwebBrowser andSLL
The public and private keys of 3 syntax standards can be converted into each other, and their core is the integer value in ASN1 syntax (modulusPublicExponentprivateExponent ), the other values ​​can be calculated and obtained.

.NETStandard useRSAFormat, then place the numbers in itbase64After encoding, generateXMLTo store.
What is used in javaPKCS#8,、X.509The public and private key syntax stored by the corresponding JAVA class automatically generatesbase64String.
Due to the differences in storage formats, it is necessary to understand when converting and reading each other.RSAOnly by using relevant knowledge can you correctly use classes to convert.

1.1 C# to JAVA

C#The public and private keys in it are stored using XML strings, and you can directly read the string when reading.
Since C# uses the standard RSA format, JAVA's RSAPublicKeySpecRSAPrivateKeySpecThe core parameters of the configuration class (modulusPublicExponentprivateExponent ) can all be from the node value in the corresponding XML (Modulus-modulus Exponent-PublicExponent D-privateExponent base64Decoded and retrieved. Then pass it into the JAVA configuration class, and then generate the corresponding one according to the configuration class.RSAPublic and private key.

 View Code

1.2 JAVA to C#

JAVAUse of public and private keys inbase64After storing and decoding into a byte array, the corresponding configuration object needs to be generated (PKCS#8,、X.509 ), generate according to the configuration objectRSAPublic and private key.

Private key:

C#Standard useRSAFormat,PKCS#1The grammar contains the standardRSAAll integer values ​​in the format private key. Configuration objects need to be generatedPKCS#1SyntaxRSAObject (RSAPrivateCrtKey), obtain the object attributes, and construct the private key by yourselfXML

 View Code

Public Key:

The steps for generating public and private keys are the same as those for generating public keys. The configuration object is generated in standardRSAObject (RSAPublicKey)。

 View Code

This is the article about the difference and connection between RSA keys-JAVA and C#. For more related RSA keys, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!