⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createxml.java

📁 对于要加密的文档,www有一套标准,约定用于加密的文档应当组成一定的格式.对于符合这个格式的xml文档,本程序能从中提前rsa加密密钥和明文进行加密.本程序还提供rsa密钥生成接口和加密解密接口.
💻 JAVA
字号:
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;

import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreateXml
{
	public static void main(String[] args)
	{
		 Rsa rsa =new Rsa();
		 KeyPair keypair=rsa.RsaKeyGen(512);
		 RSAPublicKey pbkey=(RSAPublicKey)keypair.getPublic();
		 RSAPrivateKey prkey=(RSAPrivateKey)keypair.getPrivate();
		 BigInteger Exponent =prkey.getPrivateExponent();
		 BigInteger Modulus=prkey.getModulus();
		 String ExponentString=Exponent.toString();
		 String ModulusString=Modulus.toString();

		 /************************************************************************************************/
		 //以下的公钥部分为测试用的
		 BigInteger PublicExponent=pbkey.getPublicExponent();
		 		 System.out.println(PublicExponent);
		 		 //System.out.println(Exponent);

		 String PublicExponentString=PublicExponent.toString();
		 System.out.println(PublicExponentString);


		 DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();
		 DocumentBuilder builder;
		 Document doc=null;
		 File f;
		 Transformer t=null;
		 String HeadAddedDigestString="";
		 String HeadAddedCText="";


		       try
		      {
		 	        builder = factory.newDocumentBuilder();
		 	        doc=builder.newDocument();
		 	  }
		 	  catch(javax.xml.parsers.ParserConfigurationException e)
		 	  {
		 	  		  e.printStackTrace();
		 	  }

		 	  //if(builder==null) System.out.println("null builder");

//		 	  if (doc==null) System.out.println("null doc");
		 	  Element SignatureElement=doc.createElement("Signature");
		 	  doc.appendChild(SignatureElement);
		 	  doc.createProcessingInstruction("encoding","UNICODE");

		 	  Element SignedInfoElement=doc.createElement("SignedInfo");
		 	  SignatureElement.appendChild(SignedInfoElement);
		            Element CanonicalizationMethodElement=doc.createElement("CanonicalizationMethod");
		                    CanonicalizationMethodElement.setAttribute("Algorithm","http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
		            SignedInfoElement.appendChild(CanonicalizationMethodElement);

		            Element SignatureMethodElement=doc.createElement("SignatureMethod");
		                    SignatureMethodElement.setAttribute("Algorithm","http://www.w3.org/2000/09/xmldsig#rsa-sha1");
		            SignedInfoElement.appendChild(SignatureMethodElement);


		            Element ReferenceElement=doc.createElement("Reference");
		                    ReferenceElement.setAttribute("URI","http://www.w3.org/TR/xml-stylesheet");
		                  ReferenceElement.setAttribute("URI","d");
		            SignedInfoElement.appendChild(ReferenceElement);


		                 Element DigestMethodElement=doc.createElement("DigestMethod");
		                         DigestMethodElement.setAttribute("Algorithm","http://www.w3.org/2000/09/xmldsig#sha1");
		                 ReferenceElement.appendChild(DigestMethodElement);

		                 Element DigestValueElement=doc.createElement("DigestValue");
		                         Text DigestValue=doc.createTextNode(HeadAddedDigestString);
		                         DigestValueElement.appendChild(DigestValue);
		                 ReferenceElement.appendChild(DigestValueElement);//该节点不必要,not needed。










		       Element SignatureValueElement=doc.createElement("SignatureValue");
		               Text SignatureValue=doc.createTextNode(HeadAddedCText);
		               SignatureValueElement.appendChild(SignatureValue);
		       SignatureElement.appendChild(SignatureValueElement);


		       //本节点都不必要,界面从数据库中取出对应的密钥信息,以保证安全,
		       //不然都从xml文档里取密钥,非常容易冒充。
		       Element KeyInfoElement=doc.createElement("KeyInfo");

		          Element KeyValueElement=doc.createElement("KeyValue");
		               Element RSAKeyValueElement=doc.createElement("RSAKeyValue");
		                    Element ModulusElement=doc.createElement("Modulus");
		                            Text ModulusValue=doc.createTextNode(ModulusString);//空节点。不必要
		                            ModulusElement.appendChild(ModulusValue);
		                    RSAKeyValueElement.appendChild(ModulusElement);
		                    Element ExponentElement=doc.createElement("Exponent");
		                            Text ExponentValue=doc.createTextNode(ExponentString);//空节点,不必要
		                            ExponentElement.appendChild(ExponentValue);
		                    RSAKeyValueElement.appendChild(ExponentElement);

		                    Element PublicExponentElement=doc.createElement("PublicExponent");
			                        Text PublicExponentValue=doc.createTextNode(PublicExponentString);//空节点,不必要
						            PublicExponentElement.appendChild(PublicExponentValue);
		                    RSAKeyValueElement.appendChild(PublicExponentElement);

		               KeyValueElement.appendChild(RSAKeyValueElement);
		          KeyInfoElement.appendChild(KeyValueElement);

		          Element X509DataElement=doc.createElement("X509Data");
		               Element X509SubjectNameElement=doc.createElement("X509SubjectName");
		                       Text X509SubjectNameValue=doc.createTextNode("");
		                       X509SubjectNameElement.appendChild(X509SubjectNameValue);
		               X509DataElement.appendChild(X509SubjectNameElement);
		               Element X509IssuerSerialElement=doc.createElement("X509IssuerSerial");
		                    Element X509IssuerNameElement=doc.createElement("X509IssuerName");
		                            Text X509IssuerNameValue=doc.createTextNode("");
		                            X509IssuerNameElement.appendChild(X509IssuerNameValue);
		                    X509IssuerSerialElement.appendChild(X509IssuerNameElement);
		                    Element X509SerialNumberElement=doc.createElement("X509SerialNumber");
		                            Text X509SerialNumberValue=doc.createTextNode("");
		                            X509SerialNumberElement.appendChild(X509SerialNumberValue);
		                    X509IssuerSerialElement.appendChild(X509SerialNumberElement);
		               X509DataElement.appendChild(X509IssuerSerialElement);
		               Element X509CertificateElement=doc.createElement("X509Certificate");
		               X509DataElement.appendChild(X509CertificateElement);
		          KeyInfoElement.appendChild(X509DataElement);//该节点都不必要
		       SignatureElement.appendChild(KeyInfoElement);

		       f=new File("EcryptedToLiyuxin.xml");
		       try
		       {
		 	     t =TransformerFactory.newInstance().newTransformer();
		 	     t.setOutputProperty("doctype-public","http://www.w3.org/TR/2000/CR-SVG-20000802/DTD/svg-20000802.dtd");
			     t.setOutputProperty("doctype-public","-//W3C//DTD SVG 20000802//EN");
		         t.setOutputProperty("indent","yes");
		 	  }
		 	  catch(TransformerConfigurationException e)
		 	  {
		 		  e.printStackTrace();
		 	  }







		    try
		       {
		 		  if (doc==null) System.out.println("null doc");
		 		  //t.transform(new DOMSource(doc),new StreamResult(new FileOutputStream(f)));
		 		  DOMSource domsource=new DOMSource(doc);
		 		 StreamResult streamresult=new StreamResult(new FileOutputStream(f));
		 		  if (domsource==null) System.out.println("null domsource");
		 		  if (streamresult==null)System.out.println("null streamresult");
		 		  t.transform(domsource,streamresult);
		 	  }
		 	  catch(TransformerConfigurationException e)
		 	  {
		 		  e.printStackTrace();
		 	  }
		       catch(FileNotFoundException e)
		      {
		 			e.printStackTrace();
		 	  }
		 	  catch(TransformerException e)
		 	  {
		 		  e.printStackTrace();
	  }


		 /***********************************/
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -