📄 liyuxin.java
字号:
DocumentBuilder builder = factory.newDocumentBuilder();
doc=builder.parse(f);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
Document returndoc=this.RsaEncrypt(doc,prkey);
return returndoc;
}//RsaEncrypt(File f,PrivateKey prkey)
//以接口不再使用,编写代码时一步步扩展,后来把privatekey放在了doc对象中中
protected Document RsaEncrypt(Document doc,PrivateKey prkey)
{
NodeList MessageNodeList=doc.getElementsByTagName("MessageValue");
//System.out.println(MessageNodeList.getLength());
if(MessageNodeList==null)System.out.println("null nodelist");
Node MessageNode=MessageNodeList.item(0);
if(MessageNode==null)System.out.println("null MessageNode");
Node TextNode=MessageNode.getFirstChild();
String s=TextNode.getNodeValue();
//取key
Document returndoc=this.RsaEncrypt(s,prkey);
return returndoc;
// return null;
}//RsaEncrypt(Document doc,PrivateKey prkey)
*////////////////////////////////////////////////////////////2006.06.14///////////////////////////////////////////////
//对传递过来的文件f,提取其中的明文和密钥信息加密
protected String RsaEncrypt(File f)
{
Document doc=null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
doc=builder.parse(f);
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
String returnstring=this.RsaEncrypt(doc);
return returnstring;
}
//RsaEncrypt(Fiel f)也要调用该接口
protected String RsaEncrypt(Document MessageDoc)
{
NodeList ExponentNodeList=MessageDoc.getElementsByTagName("Exponent");
NodeList ModulusNodeList=MessageDoc.getElementsByTagName("Modulus");
NodeList MessageValueNodeList=MessageDoc.getElementsByTagName("MessageValue");
if(ExponentNodeList.getLength()!=1)//是数字"1"不是字母“l”
{
System.out.println("Invalid xml file,check exponent node ,please");
}
if(ExponentNodeList.getLength()!=1)//是数字"1"不是字母“l”
{
System.out.println("Invalid xml file,check exponent node ,please");
}
String ExponentString=ExponentNodeList.item(0).getFirstChild().getNodeValue();
String ModulusString = ModulusNodeList.item(0).getFirstChild().getNodeValue();
String OrignalString =MessageValueNodeList.item(0).getFirstChild().getNodeValue();
BigInteger Exponent=new BigInteger(ExponentString);
BigInteger Modulus =new BigInteger(ModulusString);
System.out.println("Orignal Message is: "+OrignalString);
//System.out.println("Exponent is: "+ExponentString);
//System.out.println("Modulus is: "+ModulusString);
//System.out.println("Modulus.length is: "+Modulus.bitLength());
//以上为测试用的输出,保留,以备差错。
try
{
OrignalByte=OrignalString.getBytes("UNICODE");
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
//以下为提取unicode编码的头两个字节,在加密完返回字符串的时候添加在密文的头部,这样才可将数组转换成字符串(unicode编码需要头部)
UnicodeStringHead=new byte[2];
System.arraycopy(OrignalByte,0,UnicodeStringHead,0,2);
//记得转换成MByte
CurrentDigest.reset();
CurrentDigest.update(OrignalByte);
MByte=CurrentDigest.digest();
// System.out.println("MByte is "+MByte);
byte[] HeadAddedDigestStringByte=new byte[MByte.length+2];
System.arraycopy(UnicodeStringHead,0,HeadAddedDigestStringByte,0,2);
System.arraycopy(MByte,0,HeadAddedDigestStringByte,2,MByte.length);
//System.out.println("HeadAddedDigestStringByte is "+HeadAddedDigestStringByte);
try
{
HeadAddedDigestString =new String(HeadAddedDigestStringByte,"UNICODE");//摘要以后的字符串
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
TempRandom =new SecureRandom();//安全系数很高的随机类
//用密钥初始化
try
{
RsaInstance.newengineInit(Cipher.ENCRYPT_MODE,Exponent,Modulus,(RSAKeyGenParameterSpec)null,TempRandom);
}
catch(InvalidKeyException e)
{
//System.out.println("第一个");
e.printStackTrace();
}
catch(InvalidAlgorithmParameterException e)
{
//System.out.println("第二个");
e.printStackTrace();
}
outsize=0;
offset=0;
BlockSize=RsaInstance.engineGetBlockSize();
/**************以下需要更正的*/
if(MByte.length>=BlockSize)
{
TempByte=new byte[(int)(MByte.length*(BlockSize+11)/BlockSize+1)];
}
else
{
TempByte=new byte[BlockSize+11];
}
// (s.length()*2+2)为该字符串的unicode编码的字节数,*(BlockSize+11)/BlockSize是因为padding格式需要留出至少十个空位,
//加上必须保证加密的块比模数小,所以再空出一位,所以需要
//System.out.println("in RsaEncrypt(doc) MByte.length"+MByte.length+"offest is "+offset);
for(;MByte.length-offset>=BlockSize;offset=offset+BlockSize)
{
byte[] TempOutByte=RsaInstance.engineUpdate(MByte,offset,BlockSize);
System.arraycopy(TempOutByte,0,TempByte,outsize,TempOutByte.length);
outsize=outsize+TempOutByte.length;
}//前几次满块加密
byte[] TempOutByte2=RsaInstance.engineUpdate(MByte,offset,MByte.length-offset);//最后一次非满块加密
if(TempOutByte2==null)System.out.println("null TempOutByte2");
if(TempByte==null)System.out.println("null TempByte");
System.arraycopy(TempOutByte2,0,TempByte,outsize,TempOutByte2.length);
outsize=outsize+TempOutByte2.length;
CByte=new byte[outsize];
System.arraycopy(TempByte,0,CByte,0,outsize);
try
{
finals=new String(CByte,"UNICODE");
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return finals;
/*/////////////////////////////////////////////////////2006.06.15////////////////////////////////////////////
byte[] HeadAddedByte=new byte[outsize+2];
System.arraycopy(UnicodeStringHead,0,HeadAddedByte,0,2);
System.arraycopy(CByte,0,HeadAddedByte,2,outsize);
try
{
HeadAddedCText =new String(HeadAddedByte,"UNICODE");
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
System.out.println("SignatureValue is: "+ HeadAddedCText);
return HeadAddedCText;
*/////////////////////////////////////////////////////2006.06.15////////////////////////////////////////////
// System.out.println("HeadAddedCText is "+HeadAddedCText);
/* try
{
System.out.println(new String(HeadAddedCText.getBytes("UNICODE"),"UNICODE"));
}
catch(Exception e)
{}
*/
/*
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("now decrpyt");
if(this.RsaDecrypt(OrignalString,pbkey,HeadAddedCText))
System.out.println("success!");
else
System.out.println("sorry");
*/
/* //////////////////////////////////////////////////////2006.06.14///////////////////////////////////////////
factory = DocumentBuilderFactory.newInstance();
try
{
builder = factory.newDocumentBuilder();
}
catch(javax.xml.parsers.ParserConfigurationException e)
{
e.printStackTrace();
}
//if(builder==null) System.out.println("null builder");
doc=builder.newDocument();
doc.createProcessingInstruction("encoding","UNICODE");
if (doc==null) System.out.println("null doc");
Element SignatureElement=doc.createElement("Signature");
doc.appendChild(SignatureElement);
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","http://www.w3.org/2000/09/xmldsig#sha1");
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。
*/
/**/ //////////////////////////////////////////////////////2006.06.14///////////////////////////////////////////
/* //////////////////////////////////////////////////////2006.06.14///////////////////////////////////////////
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("");//空节点。不必要
ModulusElement.appendChild(ModulusValue);
RSAKeyValueElement.appendChild(ModulusElement);
Element ExponentElement=doc.createElement("Exponent");
Text ExponentValue=doc.createTextNode("");//空节点,不必要
ExponentElement.appendChild(ExponentValue);
RSAKeyValueElement.appendChild(ExponentElement);
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();
}
catch(TransformerConfigurationException e)
{
e.printStackTrace();
}
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");
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();
}
//根结点的三个子结点
//System.out.println(finals);
return doc;
*/ //////////////////////////////////////////////////////2006.06.14///////////////////////////////////////////
}
protected boolean RsaDecrypt(String s,PublicKey pbkey,String c)
{
//先对明文进行摘要,以便等下进行比较
CurrentDigest.reset();
try
{
byte[] TempByteInDecrypt=s.getBytes("UNICODE");
CurrentDigest.update(TempByteInDecrypt);
DigestByte=CurrentDigest.digest();//用来比较的
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
//以下为对签名进行解密的部分
//去掉字符串的头部,即为了显示而加上去的unicode编码的头部。
CByte=new byte[c.length()*2];
try
{
System.arraycopy(c.getBytes("UNICODE"),0,CByte,0,CByte.length);
System.out.println((c.getBytes("UNICODE")).length);
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
try
{
RsaInstance.engineInit(Cipher.DECRYPT_MODE,pbkey,(RSAKeyGenParameterSpec)null,new SecureRandom());
}
catch(InvalidKeyException e)
{
e.printStackTrace();
}
catch(InvalidAlgorithmParameterException e)
{
e.printStackTrace();
}
BlockSize=RsaInstance.engineGetBlockSize();
outsize=0;
offset=0;
SecondTempByte=new byte[CByte.length];
if(CByte.length%BlockSize != 0)
{
System.out.println("the message to be decrypted is invalid");
System.out.println(CByte.length+" "+BlockSize+" "+c.length());
System.exit(0);
}
System.out.println("CByte.length is "+CByte.length);
for(;CByte.length-offset>=BlockSize;offset=offset+BlockSize)
{
byte[] SecondTempOutByte=RsaInstance.engineUpdate(CByte,offset,BlockSize);
System.arraycopy(SecondTempOutByte,0,SecondTempByte,outsize,SecondTempOutByte.length);
outsize=outsize+SecondTempOutByte.length;
}
SecondCByte=new byte[outsize];
System.arraycopy(SecondTempByte,0,SecondCByte,0,outsize);
//以下进行比较
try
{
String tempStringOne=new String(SecondTempByte,"UNICODE");
String tempStringTwo=new String(DigestByte,"UNICODE");
if(tempStringOne.equals(tempStringTwo))
return true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -