XmlKeyBuilder.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.caimei.util;
  2. import org.w3c.dom.Document;
  3. import org.xml.sax.InputSource;
  4. import org.xml.sax.SAXException;
  5. import sun.misc.BASE64Decoder;
  6. import sun.misc.BASE64Encoder;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.IOException;
  12. import java.math.BigInteger;
  13. import java.security.KeyFactory;
  14. import java.security.PrivateKey;
  15. import java.security.PublicKey;
  16. import java.security.interfaces.RSAPrivateCrtKey;
  17. import java.security.interfaces.RSAPublicKey;
  18. import java.security.spec.RSAPrivateCrtKeySpec;
  19. import java.security.spec.RSAPublicKeySpec;
  20. /*
  21. Jeffrey Walton
  22. http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys
  23. */
  24. public class XmlKeyBuilder {
  25. public static String publicKeyToXML(PublicKey key) {
  26. if (!RSAPublicKey.class.isInstance(key)) {
  27. return null;
  28. }
  29. RSAPublicKey pubKey = (RSAPublicKey) key;
  30. StringBuilder sb = new StringBuilder();
  31. sb.append("<RSAKeyValue>");
  32. sb.append("<Modulus>")
  33. .append(new BASE64Encoder().encode(TrimLeadingZero(pubKey.getModulus()
  34. .toByteArray()))).append("</Modulus>");
  35. sb.append("<Exponent>")
  36. .append(new BASE64Encoder().encode(TrimLeadingZero(pubKey.getPublicExponent()
  37. .toByteArray()))).append("</Exponent>");
  38. sb.append("</RSAKeyValue>");
  39. return sb.toString();
  40. }
  41. public static String privateKeyToXML(PrivateKey key) {
  42. if (!RSAPrivateCrtKey.class.isInstance(key)) {
  43. return null;
  44. }
  45. RSAPrivateCrtKey priKey = (RSAPrivateCrtKey) key;
  46. StringBuilder sb = new StringBuilder();
  47. sb.append("<RSAKeyValue>");
  48. sb.append("<Modulus>")
  49. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getModulus()
  50. .toByteArray()))).append("</Modulus>");
  51. sb.append("<Exponent>")
  52. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPublicExponent()
  53. .toByteArray()))).append("</Exponent>");
  54. sb.append("<P>")
  55. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPrimeP()
  56. .toByteArray()))).append("</P>");
  57. sb.append("<Q>")
  58. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPrimeQ()
  59. .toByteArray()))).append("</Q>");
  60. sb.append("<DP>")
  61. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPrimeExponentP()
  62. .toByteArray()))).append("</DP>");
  63. sb.append("<DQ>")
  64. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPrimeExponentQ()
  65. .toByteArray()))).append("</DQ>");
  66. sb.append("<InverseQ>")
  67. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getCrtCoefficient()
  68. .toByteArray()))).append("</InverseQ>");
  69. sb.append("<D>")
  70. .append(new BASE64Encoder().encode(TrimLeadingZero(priKey.getPrivateExponent()
  71. .toByteArray()))).append("</D>");
  72. sb.append("</RSAKeyValue>");
  73. return sb.toString();
  74. }
  75. public static PublicKey xmlToPublicKey(String key)
  76. throws ParserConfigurationException, SAXException, IOException {
  77. key = key.replaceAll("\r", "").replaceAll("\n", "");
  78. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  79. DocumentBuilder builder = factory.newDocumentBuilder();
  80. Document doc = builder.parse(new InputSource(new ByteArrayInputStream(
  81. key.getBytes("utf-8"))));
  82. String n = doc.getDocumentElement().getElementsByTagName("Modulus")
  83. .item(0).getNodeValue();
  84. String e = doc.getDocumentElement().getElementsByTagName("Exponent")
  85. .item(0).getNodeValue();
  86. BigInteger modulus = new BigInteger(1,
  87. new BASE64Decoder().decodeBuffer(n));
  88. BigInteger publicExponent = new BigInteger(1,
  89. new BASE64Decoder().decodeBuffer(e));
  90. RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus,
  91. publicExponent);
  92. KeyFactory keyf;
  93. try {
  94. keyf = KeyFactory.getInstance("RSA");
  95. return keyf.generatePublic(rsaPubKey);
  96. } catch (Exception ex) {
  97. return null;
  98. }
  99. }
  100. public static PrivateKey xmlToPrivateKey(String key) throws IOException,
  101. SAXException, ParserConfigurationException {
  102. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  103. DocumentBuilder builder = factory.newDocumentBuilder();
  104. Document doc = builder.parse(new InputSource(new ByteArrayInputStream(
  105. key.getBytes("utf-8"))));
  106. String n = doc.getDocumentElement().getElementsByTagName("Modulus")
  107. .item(0).getNodeValue();
  108. String e = doc.getDocumentElement().getElementsByTagName("Exponent")
  109. .item(0).getNodeValue();
  110. String d = doc.getDocumentElement().getElementsByTagName("D").item(0)
  111. .getNodeValue();
  112. String p = doc.getDocumentElement().getElementsByTagName("P").item(0)
  113. .getNodeValue();
  114. String q = doc.getDocumentElement().getElementsByTagName("Q").item(0)
  115. .getNodeValue();
  116. String dp = doc.getDocumentElement().getElementsByTagName("DP").item(0)
  117. .getNodeValue();
  118. String dq = doc.getDocumentElement().getElementsByTagName("DQ").item(0)
  119. .getNodeValue();
  120. String inverseQ = doc.getDocumentElement()
  121. .getElementsByTagName("InverseQ").item(0).getNodeValue();
  122. key = key.replaceAll("\r", "").replaceAll("\n", "");
  123. BigInteger modulus = new BigInteger(1,
  124. new BASE64Decoder().decodeBuffer(n));
  125. BigInteger publicExponent = new BigInteger(1,
  126. new BASE64Decoder().decodeBuffer(e));
  127. BigInteger privateExponent = new BigInteger(1,
  128. new BASE64Decoder().decodeBuffer(d));
  129. BigInteger primeP = new BigInteger(1,
  130. new BASE64Decoder().decodeBuffer(p));
  131. BigInteger primeQ = new BigInteger(1,
  132. new BASE64Decoder().decodeBuffer(q));
  133. BigInteger primeExponentP = new BigInteger(1,
  134. new BASE64Decoder().decodeBuffer(dp));
  135. BigInteger primeExponentQ = new BigInteger(1,
  136. new BASE64Decoder().decodeBuffer(dq));
  137. BigInteger crtCoefficient = new BigInteger(1,
  138. new BASE64Decoder().decodeBuffer(inverseQ));
  139. RSAPrivateCrtKeySpec rsaPriKey = new RSAPrivateCrtKeySpec(modulus,
  140. publicExponent, privateExponent, primeP, primeQ,
  141. primeExponentP, primeExponentQ, crtCoefficient);
  142. KeyFactory keyf;
  143. try {
  144. keyf = KeyFactory.getInstance("RSA");
  145. return keyf.generatePrivate(rsaPriKey);
  146. } catch (Exception ex) {
  147. return null;
  148. }
  149. }
  150. static byte[] TrimLeadingZero(byte[] values) {
  151. if ((0x00 == values[0]) && (values.length > 1)) {
  152. byte[] r = null;
  153. r = new byte[values.length - 1];
  154. System.arraycopy(values, 1, r, 0, r.length);
  155. return r;
  156. }
  157. return values;
  158. }
  159. }