下面是一个在JAVA语言中,使用DSA签名算法来进行进行数字签名的一个示例。DSA(Digital Signature Algorithm)是Schnorr和ElGamal签名算法的变种,被美国NIST作为数字签名标准(DigitalSignature Standard)。参照JDK文档中关于Signature类的叙述:NIST标准的DSA算法应该本身就包含了散列算法SHA-1和加密算法DSA。Signature.getInstance("DSA")等同于Signature.getInstance("SHA1withDSA");如果使用DSA作为数字签名的加密算法,则只能使用SHA1作为消息散列(即消息摘要)算法。如果使用RSA作为数字签名加密算法,对消息摘要算法则会有多种选择,因此,可以将签名算法指定为 MD2withRSA、MD5withRSA 或SHA1withRSA。关于使用RSA实现数字签名请参照《用RSA实现数字签名》如果你对数字签名还不熟悉,请先阅读《数字签名简介》及《Java的数字签名和数字证书》DSA_Demo.java文件import java.security.InvalidKeyException;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.NoSuchAlgorithmException;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.SignatureException;public class DSA_Demo {DSA_Demo(){init();}private void init(){prepare();doSenderWork();doReceiverWork();}//share by sender and receiverSignature sign;//belong to sender,it visible to sender and receiverPublicKey publicKey;//belong to sender,it is only visible to senderPrivateKey privateKey;private void prepare(){KeyPairGenerator keyGen=null;try {//实例化一个DSA算法的公钥/私钥对生成器keyGen=KeyPairGenerator.getInstance("DSA");} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}int keysize=1024;//设置公钥/私钥对的长度keyGen.initialize(keysize);//生成一个RSA算法的公钥/私钥KeyPair keyPair=keyGen.generateKeyPair();privateKey=keyPair.getPrivate();publicKey=keyPair.getPublic();try {//实例化一个DSA签名算法的Signature.sign=Signature.getInstance("DSA");} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}}void doSenderWork(){String words="This is robin.How are you?This a DSA Signature Demo";Message msg=new Message(words.getBytes());try {//设置加密散列码用的私钥sign.initSign(privateKey);} catch (InvalidKeyException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {//设置散列算法的输入sign.update(msg.getBody());} catch (SignatureException e) {// TODO Auto-generated catch blocke.printStackTrace();}byte data[]=null;try {//进行散列,对产生的散列码进行加密并返回data=sign.sign();} catch (SignatureException e) {// TODO Auto-generated catch blocke.printStackTrace();}//把加密后散列(即签名)加到消息中msg.setSignature(data);//发送消息sendMsg(msg);}Message sendingMsg;void sendMsg(Message sendMsg){sendingMsg=sendMsg;System.out.println("sending Message Signed by DSA");}void doReceiverWork(){//收到消息Message msg=getReceivedMsg();try {//设置解密散列码用的公钥。sign.initVerify(publicKey);} catch (InvalidKeyException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {//设置散列算法的输入sign.update(msg.getBody());} catch (SignatureException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {/*进行散列计算,比较计算所得散列码是否和解密的散列码是否一致。 * 一致则验证成功,否则失败*/if(sign.verify(msg.getSignature())){System.out.println("数字签名验证成功!");}else{System.out.println("数字签名验证失败!");}} catch (SignatureException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Message getReceivedMsg(){System.out.println("receiving Message");return sendingMsg;}}class Message{private byte [] body;private byte [] signature;Message(byte data[]){body=data;}byte[] getBody(){return body;}byte[] getSignature(){return signature;}void setSignature(byte data[]){signature=data;}}