Java数字签名技术

package com.experience.tool;

import java.io.BufferedReader;
import java.io.FileReader;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

/**
 * 数字签名  RSA
 */
public class RSASignCoder {

    //数字签名 - 签名验证算法
    private static final String SIGNATRUE_ALGORITHM = "MD5withRSA";
    //数字签名 - RSA算法
    private static final String KEY_ALGORITHM = "RSA";
    //私钥
    private static final String PRIVATEKEY = "RSAPrivateKey";
    //公钥
    private static final String PUBLICKEY = "RSAPublicKey";

    /**
     * RSA长度
     * 默认是 1024
     * 必须是 64的倍数
     * 范围:512 - 65536
     */
    private static final int KeySize = 512; // 512 位 2进制,128 位 16进制

    /**
     * 初始化 **
     *
     * @return map
     * @throws Exception
     * @author wlk
     */
    public static Map<String, Object> initKey() throws Exception {
        //实例化**生成器
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        //初始化
        keyPairGenerator.initialize(KeySize);
        //获取**对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //获取私钥
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        //获取公钥
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(PRIVATEKEY, privateKey);
        map.put(PUBLICKEY, publicKey);
        return map;
    }

    /**
     * 取得公钥
     *
     * @return 公钥加密
     * @author wlk
     */
    public static byte[] getPublicKey(Map<String, Object> map) {
        PublicKey publicKey = (PublicKey) map.get(PUBLICKEY);
        return publicKey.getEncoded();
    }

    /**
     * 取得私钥
     * @return 私钥加密
     * @author wlk
     */
    public static byte[] getPrivateKey(Map<String, Object> map) {
        PrivateKey privateKey = (PrivateKey) map.get(PRIVATEKEY);
        return privateKey.getEncoded();
    }

    /***
     * 私钥加密
     * @author wlk
     * @param source 需要加密数据
     * @param key 私钥
     * @return
     */
    public static String sign(String source, byte[] key) throws Exception {
        byte[] data = source.getBytes("utf-8");

        // 取得私钥
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

        // 生成私钥
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

        //新增: 实例化 签名对象
        Signature signature = Signature.getInstance(SIGNATRUE_ALGORITHM);
        //初始化 私钥
        signature.initSign(privateKey);
        //更新签名
        signature.update(data);
        byte[] enSign = signature.sign();

        return Base64.encodeBase64String(enSign);
    }

    /**
     * 校验
     * @param key     公钥
     * @param signStr 已签名数据
     * @return 数字签名验证
     * @throws Exception
     * @author wlk
     */
    public static boolean verify(String source, byte[] key, String signStr) throws Exception {
        // 还原即将 解密的 数据源
        byte[] data = source.getBytes("utf-8");
        // 还原已签名数据
        byte[] signData = Base64.decodeBase64(signStr);

        // 取得公钥
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        // 生成公钥
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        //新增: 实例化 签名对象
        Signature signature = Signature.getInstance(SIGNATRUE_ALGORITHM);
        //初始化 校验公钥
        signature.initVerify(publicKey);
        //更新签名
        signature.update(data);
        //验证
        return signature.verify(signData);
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> keyMap = initKey();
        byte[] privateKey = getPrivateKey(keyMap);
        byte[] publicKey = getPublicKey(keyMap);
        System.out.println("获取的私钥:" + Base64.encodeBase64String(privateKey));
        System.out.println("获取的公钥:" + Base64.encodeBase64String(publicKey));

        String source = "";
        //需要签名文件的内容
        String fileName = "data.txt";
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
//                System.out.println("文件内容是: "+line);
                source = line;
            }
        }
        System.out.println("需要签名的文件内容是: " + source);

        String sign = sign(source, privateKey);
        System.out.println("签名后:" + sign);
        boolean flat = verify(source, publicKey, sign);
        System.out.println("校验结果:" + flat);

    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值