package com.mainet.security.mercury.target;
import java.security.Signature;
import java.security.SignatureException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.Provider;
import java.io.*;
import sun.misc.*;
/**
* 仅用于测试,不属于工程的一部分
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class TestSign {
public static void main(String[] args)throws Exception{
/**
* get the RSA KeyPair;1024;
*/
//------------------------generate key pairs-------------------------
// System.out.println("Generating RSA key pair.");
// KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// kpg.initialize(1024);
// KeyPair keyPair = kpg.genKeyPair();
// System.out.println("Done generator");
System.out.println("Now generating RSA key pair ....");
try{
Provider provider = (Provider)Class.forName("au.net.aba.crypto.provider.ABAProvider").newInstance();
Security.addProvider(provider);
}
catch(ClassNotFoundException classnotfoundexception){
}
catch(InstantiationException instantiationexception){
}
catch(IllegalAccessException illegalaccessexception){
}
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// SecureRandom secureRandom = SecureRandom.getInstance("SHA1withRSA");
// OOP-Research 中规定
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();
System.out.println("Nowdo
ne generating key pair...");
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
//-------------------------写入硬盘, 写入public key------------------
ObjectOutputStream objectoutputstream = new ObjectOutputStream(new FileOutputStream("d:/public.keystore"));
objectoutputstream.writeObject(publicKey);
objectoutputstream.flush();
objectoutputstream.close();
//再次写入硬盘
// this.private_key.put(s,privateKey);
// this.private_key_date.put(s,Calendar.getInstance().getTime());
// this.public_key.put(s,publicKey);
// this.public_key_date.put(s,Calendar.getInstance().getTime());
//------------------------generate key pairs--------------------------
File file = new File("D:/cc.txt");
//输入文件流
FileInputStream fileInputStream = new FileInputStream(file);
//由于存放的是二进制的文件流,所以可以使用ByteArrayOutputStream来转换,转换为
//二进制的文件流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//使用i来标志读入的字符是否为回车符号
//由于需要写入字符到bytearrayoutputstream中,故,每次读入1个byte
for(int i = 1;
i != -1
{
byte newByte[] = new byte[1];
i = fileInputStream.read(newByte, 0, 1);
if(i != -1)
byteArrayOutputStream.write(newByte);
}
//关闭流
fileInputStream.close();
//取得二进制的文件内容,使用byteArrayOutputStream输入
byte [] data = byteArrayOutputStream.toByteArray();
//关闭流
byteArrayOutputStream.close();
// byte[] data = textToTest.toString().getBytes();
//get the
//------------------------------OOP-Research
// Signature signature = Signature.getInstance(getSignAlg(), key_prov);
// signature.initSign(privatekey);
//00--research
Signature sig = Signature.getInstance("MD5WithRSA","ABA");
TestSignature ss = new TestSignature();
ss.setSignature(data);
objectoutputstream = new ObjectOutputStream(new FileOutputStream("d:/signature.signature"));
objectoutputstream.writeObject(ss);
objectoutputstream.flush();
objectoutputstream.close();
sig.initSign(privateKey);
sig.update(data);
byte[] signatureBytes = sig.sign();
// System.out.println("/nSignature:/n"+new BASE64Encoder().encode(signatureBytes));
//读出硬盘,读出public key
FileInputStream fileinputstream = new FileInputStream("d:/public.keystore");
ObjectInputStream objectinputstream = new ObjectInputStream(fileinputstream);
publicKey = (PublicKey)objectinputstream.readObject();
objectinputstream.close();
//读出硬盘
fileinputstream = new FileInputStream("d:/signature.signature");
objectinputstream = new ObjectInputStream(fileinputstream);
ss = (TestSignature)objectinputstream.readObject();
objectinputstream.close();
data = ss.getSignature();
sig = Signature.getInstance("MD5WithRSA","ABA");
sig.initVerify(publicKey);
sig.update(data);
boolean verified = false;
try{
verified = sig.verify(signatureBytes);
}catch(SignatureException se){
verified =false;
}
if(verified){
System.out.println("/nSignature verified.");
}else
{
System.out.println("/n Signature did not verified");
}
}
}