What are the differences and connections between C and Java

  • 2021-10-27 08:29:59
  • OfStack

Because companies use both. NET and JAVA, and each service set uses interfaces to communicate, some more secure systems, such as clearing systems and cash registers, use RSA for encryption. Therefore, the conversion of the secret key will be involved. So I probably looked at the difference between the secret key of C # and the secret key of JAVA.

RSA makes no difference to the program itself, and its format is the same. The syntax (wrapped classes) used for storage will vary from program to program.

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

Introduction to RSA Syntax

ASN. 1 and PKCS are the first and most important syntax standards for public and private keys of RSA, which are maintained by RSA and Lab.

Both ASN. 1 and PKCS # 1 define the types of public and private keys--serialized numbers. For the next level of abstraction (proper wrapping), the combinations now used in general are the private key of PKCS # 8 and the public key of X. 509.

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

The X. 509 syntax is primarily used for public keys and is widely used in the web browser and SLL.

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

. NET uses the standard RSA format, and then the number base64 is encoded to generate 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.

Because of the difference in storage format, it is necessary to understand the knowledge related to RSA when converting and reading each other, so as to correctly use classes for conversion.

C # to JAVA

Public and private keys in C # are stored using XML strings, which can be read directly into the strings.

Because C # uses the standard RSA format, the core parameters (modulus, PublicExponent, privateExponent) of RSAPublicKeySpec and RSAPrivateKeySpec configuration classes of JAVA can be obtained from the node values (Modulus-modulus, Exponent-PublicExponent, D-privateExponent) in the corresponding XML after decoding base64. It is then passed into the JAVA configuration class, and the corresponding RSA public and private keys are generated according to the configuration class.

JAVA to C #

The public and private keys in JAVA are stored by base64. After decoding into byte arrays, they need to be created into corresponding configuration objects (PKCS # 8, X. 509), and the public and private keys of RSA are generated 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 = 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 key

C # uses the standard RSA format, and the PKCS # 1 syntax contains all the integer values in the standard RSA format private key. Configuration object needs to generate RSA object with PKCS # 1 syntax (RSAPrivateCrtKey), obtain object attributes, and construct private key XML by itself.


private static String getRSAPrivateKeyAsNetFormat(byte[] encodedPrivateKey) { 
try { 
StringBuffer buff = new StringBuffer(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 key

The public key and private key generation steps are the same, configuring the generation standard RSA object (RSAPublicKey).


private static String getRSAPublicKeyAsNetFormat(byte[] encodedPublicKey) { 
try { 
StringBuffer buff = new StringBuffer(1024);
//Only RSAPublicKeySpec and X509EncodedKeySpec supported for RSA public keys 
KeyFactory keyFactory = KeyFactory.getInstance("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; 
} 
}

Related articles: