First of all, thanks:/Articles/25487/Cryptographic-Interoperability-KeysArticles.
Because the company uses it at the same time.NET
andJAVA
, 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.RSA
There 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.RSA
There are many grammar and grammar standards, and the large types are roughly divided intoASN.1
、PKCS
、X.509
。
1. Introduction to RSA grammar
ASN.1、PKCS
It is the original and most important syntax standard for RSA public and private keys,RSA Lab
Maintained.ASN.1、PKCS#1
Both 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#8
private key,X.509
public key .
PKCS
Syntax is mainly used forPrivate key, there are currently 10 internal standards. at presentJAVA
It is commonly usedPKCS#8
, used as the private key format.X.509
Syntax is mainly used forPublic Key, widely used inweb
Browser 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 (modulus
,PublicExponent
,privateExponent
), the other values can be calculated and obtained.
.NET
Standard useRSA
Format, then place the numbers in itbase64
After encoding, generateXML
To store.
What is used in javaPKCS#8
,、X.509
The public and private key syntax stored by the corresponding JAVA class automatically generatesbase64
String.
Due to the differences in storage formats, it is necessary to understand when converting and reading each other.RSA
Only 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 RSAPublicKeySpec
、RSAPrivateKeySpec
The core parameters of the configuration class (modulus
,PublicExponent
,privateExponent
) can all be from the node value in the corresponding XML (Modulus-modulus
、Exponent-PublicExponent
、D-privateExponent
)base64
Decoded and retrieved. Then pass it into the JAVA configuration class, and then generate the corresponding one according to the configuration class.RSA
Public and private key.
View Code
1.2 JAVA to C#
JAVA
Use of public and private keys inbase64
After storing and decoding into a byte array, the corresponding configuration object needs to be generated (PKCS#8
,、X.509
), generate according to the configuration objectRSA
Public and private key.
Private key:
C#
Standard useRSA
Format,PKCS#1
The grammar contains the standardRSA
All integer values in the format private key. Configuration objects need to be generatedPKCS#1
SyntaxRSA
Object (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 standardRSA
Object (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!