SoFunction
Updated on 2025-03-08

What are the differences and connections between C# and Java

Since the company uses both .NET and JAVA, and each service set uses interfaces to communicate, some systems with higher security such as clearing systems and cashiers will use RSA for encryption. Therefore, the conversion of the secret key will be involved. So I roughly looked at the difference between the C# secret key and the JAVA secret key.

RSA has no difference to the program itself, and its formats are the same. The syntax used by the storage (wrapped class) will vary for different programs.

There are many RSA syntax and grammar standards, and the large types are roughly divided into ASN.1, PKCS, and X.509.

Introduction to RSA syntax

ASN.1, PKCS is the original and most important syntax standard for RSA public and private keys, and is maintained by RSA Lab.

ASN.1 and 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 now are: PKCS#8's private key, X.509's public key.

PKCS syntax is mainly used for private keys, and there are currently 10 internal standards. Currently, JAVA is commonly used in PKCS#8, which is used as the private key format.

X.509 syntax is mainly used in public keys and is widely used in web browsers and SLLs.

The public and private keys of three syntax standards can be converted into each other, and their core is the integer value (modulus, PublicExponent, privateExponent) in the ASN1 syntax.

.NET uses the standard RSA format, and then encodes the number base64 and generates XML for storage.

Java uses the public and private key syntax of PKCS#8, X.509, and stores base64 strings automatically generated by the corresponding JAVA class.

Due to the differences in storage formats, when converting and reading each other, you need to understand RSA-related knowledge in order to correctly use classes for conversion.

C# to JAVA

The public and private keys in C# are stored using XML strings, and the string is read directly when reading.

Since C# uses the standard RSA format, the core parameters (modulus, PublicExponent, privateExponent) of JAVA's RSAPublicKeySpec and RSAPrivateKeySpec configuration classes can be obtained from the corresponding node values ​​(Modulus-modulus, Exponent-PublicExponent, D-privateExponent) base64 decoding. Then pass it into the JAVA configuration class, and then generate the corresponding RSA public and private key according to the configuration class.

JAVA to C#

The public and private keys in JAVA are stored using base64. After decoding into a byte array, they need to form the corresponding configuration object (PKCS#8, X.509) and generate the RSA public and private 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("3RgqP5YOYUXft8YOlDphyaCoof27MSfTD2eVCFVXB5hatrls1fSUcmUuWuGV970sS6KQZZtyWHQ5970sCzKFlq82He8Uoe0JM3axBvd6PbSGjulUJr62qNW5hgkIEfxSRYl8AQsbbusFtks4obfepsfE02cLmmZepnZAdIOWifE=");
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 = ("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
RSAPublicKey pubKey = (RSAPublicKey) (keySpec);
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(b11, b21);
RSAPrivateKey priKey = (RSAPrivateKey) (priKeySpec);

Private key

C# uses the standard RSA format, and the PKCS#1 syntax contains all integer values ​​in the standard RSA format private key. To configure the object, you need to generate an RSA object (RSAPrivateCrtKey) with PKCS#1 syntax, get the object properties, and construct the private key XML by yourself.

private static String getRSAPrivateKeyAsNetFormat(byte[] encodedPrivateKey) { 
try { 
StringBuffer buff = new StringBuffer(1024); 
PKCS8EncodedKeySpec pvkKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); 
KeyFactory keyFactory = ("RSA"); 
RSAPrivateCrtKey pvkKey = (RSAPrivateCrtKey) (pvkKeySpec); 
("<RSAKeyValue>"); 
("<Modulus>" + encodeBase64(removeMSZero(().toByteArray())) + "</Modulus>");
("<Exponent>" + encodeBase64(removeMSZero(()toByteArray())) + "</Exponent>");
("<P>" + encodeBase64(removeMSZero(().toByteArray())) + "</P>");
("<Q>" + encodeBase64(removeMSZero(().toByteArray())) + "</Q>");
("<DP>" + encodeBase64(removeMSZero(().toByteArray())) + "</DP>"); 
("<DQ>" + encodeBase64(removeMSZero(().toByteArray())) + "</DQ>");
("<InverseQ>" + encodeBase64(removeMSZero(().toByteArray())) + "</InverseQ>");
("<D>" + encodeBase64(removeMSZero(().toByteArray())) + "</D>"); 
("</RSAKeyValue>"); 
return (); 
} catch (Exception e) { 
(e); 
return null; 
} 
}

Public Key

The steps for generating public and private keys are the same as those for generating public keys. The standard RSA object (RSAPublicKey) is configured to generate.

private static String getRSAPublicKeyAsNetFormat(byte[] encodedPublicKey) { 
try { 
StringBuffer buff = new StringBuffer(1024);
//Only RSAPublicKeySpec and X509EncodedKeySpec supported for RSA public keys 
KeyFactory keyFactory = ("RSA"); 
RSAPublicKey pukKey = (RSAPublicKey) (new X509EncodedKeySpec(encodedPublicKey)); 
("<RSAKeyValue>"); 
("<Modulus>" + encodeBase64(removeMSZero(().toByteArray())) + "</Modulus>"); 
("<Exponent>" + encodeBase64(removeMSZero(().toByteArray())) + "</Exponent>"); 
("</RSAKeyValue>"); 
return (); 
} catch (Exception e) { 
(e); 
return null; 
} 
}

The above is the difference and connection between C# and Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!