1. Public Key của .Net sinh ra có dạng
Code:
<RSAKeyValue><Modulus>Modulus value</Modulus><Exponent>exponent value</Exponent></RSAKeyValue>
Convert sang java:
Code:
BigInteger modulus = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("Modulus value in .net")), 16);
BigInteger exponent = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("exponent value in .net")), 16);
RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(rsaPublicKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
2. Private key của .net có dạng:
Code:
<RSAKeyValue><Modulus>Modulus value </Modulus><Exponent> Exponent value</Exponent><P> P value</P><Q> Q value</Q><DP>dp value</DP><DQ>dq value</DQ><InverseQ>inverseq value</InverseQ><D>d value</D></RSAKeyValue>
Convert sang java private key:
Code:
BigInteger modules = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("modules value")), 16);
BigInteger exponent = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("exponent value")), 16);
BigInteger p = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("p value")), 16);
BigInteger q = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("q value")), 16);
BigInteger dp = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("dp value")), 16);
BigInteger dq = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("dq value")), 16);
BigInteger inverseQ = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("inverseq value")), 16);
BigInteger d = new BigInteger(ISOUtil.hexString(Base64.decodeBase64("d value")), 16);
RSAPrivateCrtKeySpec rsaPrivateKeySpec = new RSAPrivateCrtKeySpec(modulus, exponent, d, p, q, dp, dq, inverseQ);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);