import java.io.*;
import java.security.*;
/**
* <p>Title: 数字签名工具类</p>
* <p>Description: 使用非对称密匙的私匙对输入信息加密</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not galaxyp
* @version 1.0
*/
public class DSAUtil {
public DSAUtil() {
}
/*
* 根据传入的密匙文件路径加密信息,并保存到指定的目录下
* @param pOrgiInfo 要加密的信息
* @param pPrikeyPath 加密的私匙路径和文件名
* @param pInfoPath 保存密文的路径
*/
private void encryptInfo(String pOrgiInfo,String pPrikeyPath,String pInfoPath)throws Exception{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(pPrikeyPath));
PrivateKey myprikey=(PrivateKey)in.readObject();
Signature signet=Signature.getInstance("DSA");
signet.initSign(myprikey);
signet.update(pOrgiInfo.getBytes());
byte[] signed=signet.sign();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(pInfoPath+"\\out.dat"));
out.writeObject(pOrgiInfo);
out.writeObject(signed);
out.close();
}
public static void main(String[] args) {
DSAUtil DSAUtil1 = new DSAUtil();
if(args == null || args.length != 3){
System.out.println("没有输入必要的信息");
System.out.println("\t参数一:要加密的信息");
System.out.println("\t参数二:私匙文件完整路径和文件名");
System.out.println("\t参数三:保存密文的路径,文件名为out.dat");
return;
}
String info = args[0];
String pPrikeyPath = args[1];
String pInfoPath = args[2];
try{
DSAUtil1.encryptInfo(info,pPrikeyPath,pInfoPath);
}catch(Exception e){
e.printStackTrace();
System.out.println("签名失败");
}
}
}