`

java DSA签名实现

    博客分类:
  • java
阅读更多
通过以下工具类可以生成DSA公钥和私钥文件
/**
 * 
 */
package com.yeatssearch.security;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 生成DSA密钥对的工具类
 * 使用方法:java DSAKeyPairGenerator -genkey public.key private.key
 * public.key--生成的公钥文件名
 * private.key--生成的私钥文件名
 * @author Buffon
 *
 */
public class DSAKeyPairGenerator {
    private static final int KEYSIZE=512;
    /**
     * 生成DSA密钥对的工具类
     * 使用方法:java DSAKeyPairGenerator -genkey public.key private.key
     * public.key--生成的公钥文件名
     * private.key--生成的私钥文件名
     * @param args
     */
    public static void main(String[] args) {
        if(args[0].equals("-genkey")){
            try {
                KeyPairGenerator pairgen=KeyPairGenerator.getInstance("DSA");
                SecureRandom random=new SecureRandom();
                pairgen.initialize(KEYSIZE, random);
                KeyPair keyPair=pairgen.generateKeyPair();
                
                ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(args[1]));
                out.writeObject(keyPair.getPublic());
                out.close();
                
                out=new ObjectOutputStream(new FileOutputStream(args[2]));
                out.writeObject(keyPair.getPrivate());
                out.close();
            } catch (NoSuchAlgorithmException e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成 catch 块
                e.printStackTrace();
            }
        }
    }

}
/**
 * 
 */
package com.yeatssearch.security;

/**
 * @author Buffon
 *
 */
public interface DSA {
    /**
     * 进行签名的方法
     * @param content 需要签名的内容
     * @return
     * @throws Exception
     */
    public String sign(String content) throws Exception;
    /**
     * 验证签名的方法
     * @param signature 签名
     * @param contect 明文
     * @return
     * @throws Exception
     */
    public boolean verify(String signature,String contect) throws Exception;
}
/**
 * 
 */
package com.yeatssearch.security;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import com.yeatssearch.util.common.StringUtil;

/**
 * 在Spring中使用的DSA工具
 * 
 * @author Buffon
 * 
 */
public class DSAService implements DSA {
    private static Log log = LogFactory.getLog(DSAService.class);

    private Resource privateKeyResource;

    private Resource publicKeyResource;

    private PublicKey publicKey;

    private PrivateKey privateKey;

    public String sign(String content) throws Exception {
        try {
            Signature signalg = Signature.getInstance("DSA");
            signalg.initSign(privateKey);
            signalg.update(content.getBytes());
            byte[] signature = signalg.sign();
            return StringUtil.bytesToHexString(signature);
        } catch (Exception e) {
            log.error(e);
            throw e;
        }
    }

    public boolean verify(String signature, String contecnt) throws Exception {
        try {
            Signature verifyalg = Signature.getInstance("DSA");
            verifyalg.initVerify(publicKey);

            verifyalg.update(contecnt.getBytes());

            return verifyalg.verify(StringUtil.hexToBytes(signature));
        } catch (Exception e) {
            log.error(e);
            throw e;
        }
    }

    public void setPrivateKey(Resource privateKeyResource) throws Exception {
        try {
            this.privateKeyResource = privateKeyResource;
            ObjectInputStream keyIn = new ObjectInputStream(
                    new FileInputStream(this.privateKeyResource.getFile()));
            privateKey = (PrivateKey) keyIn.readObject();
            keyIn.close();
        } catch (Exception e) {
            log.error(e);
            throw e;
        }
    }

    public void setPublicKey(Resource publicKeyResource) throws Exception {
        try {
            this.publicKeyResource = publicKeyResource;
            ObjectInputStream keyIn = new ObjectInputStream(
                    new FileInputStream(this.publicKeyResource.getFile()));
            publicKey = (PublicKey) keyIn.readObject();
            keyIn.close();
        } catch (Exception e) {
            log.error(e);
            throw e;
        }
    }

}

 

Spring中的配置

<bean id="dsaService" class="com.yeatssearch.security.DSAService">
  <property name="publicKey" value="classpath:yeatssearch.publickey"/>
 </bean>

 

分享到:
评论
3 楼 SeanHe 2010-06-25  
sunney2010 写道
能不能提供这个类的包??我的邮件:chunming8302@126.com
建议你把这个StringUtil代码也放出来。谢谢

这个类写的有点年头了,我自己也没找到原来的代码,你如果需要使用就直接拷贝我的源代码吧,另外StringUtil这个类在这里用的功能就是将字节和16进制字符串的互转,你可以直接只用Apache的Commons包,具体可以参考这个文档http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html
2 楼 sunney2010 2010-06-25  
能不能提供这个类的包??我的邮件:chunming8302@126.com
建议你把这个StringUtil代码也放出来。谢谢
1 楼 lamisy 2008-11-20  
com.yeatssearch.util.common.StringUtil;
能不能提供这个类的包??我的邮件:langdy@live.cn

相关推荐

Global site tag (gtag.js) - Google Analytics