java rsa签名_Java实现RSA数字签名算法

本文档介绍了如何使用Java实现RSA数字签名算法,包括生成密钥对、进行签名和验证签名的步骤。通过示例代码展示了如何使用KeyPairGenerator、Signature等类进行操作,并提供了读取和保存密钥文件的方法。
摘要由CSDN通过智能技术生成

package com.runsun.bfc.protocol;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.InputStream;

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 org.apache.commons.codec.binary.Base64;

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

import org.apache.commons.io.IOUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**类名:RSACoder 

* 功能:RSA签名和验签的工具类

* 详细: 

* 作者: Tliu 

* 日期:2015年12月3日 

*/

public class RSACoder {

/*密钥长度*/

public final static int KEY_SIZE = 1024;

/*签名字符集*/

public final static String CHARSET = "UTF-8";

/*数字签名密钥算法*/

public static final String KEY_ALGORITHM = "RSA";

/*公钥文件名*/

public static final String PUBLIC_KEY = "bfcPub.key";

/*私钥文件名*/

public static final String PRIVATE_KEY = "bfcPri.key";

/*数字签名验证算法*/

public static final String SIGN_ALGORITHM = "SHA256withRSA";

/*日志输出*/

public static Log log = LogFactory.getLog(RSACoder.class);

/**

* @详细 生成签名内容

* @param data 等签名的消息字符串

* @param privateKey 私钥字符串

* @return

*/

public static String sign(String data, String privateKey){

byte[] dataByte = null;

String signMsg = null;

try {

dataByte = data.getBytes(CHARSET);

byte[] priKey = Base64.decodeBase64(privateKey);

signMsg = sign(dataByte, priKey);

}  catch (Exception ex){

ex.printStackTrace();

log.error(ex);

}

return signMsg;

}

/**

* @详细 验证签名

* @param data 待验签的字符串

* @param publicKey 公钥字符串

* @param sign 签名内容

* @return

*/

public static boolean verify(String data, String publicKey, String sign){

boolean rs = false;

try {

byte[] dataByte = data.getBytes(CHARSET);

byte[] pubKey = Base64.decodeBase64(publicKey);

byte[] signByte = Hex.decodeHex(sign.toCharArray());

rs = verify(dataByte, pubKey, signByte);

} catch (Exception ex){

ex.printStackTrace();

log.error(ex);

}

return rs;

}

private static boolean verify(byte[] data, byte[] publicKey, byte[] sign)throws Exception{

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);

KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

PublicKey pubKey = keyFactory.generatePublic(keySpec);

Signature signature = Signature.getInstance(SIGN_ALGORITHM);

signature.initVerify(pubKey);

signature.update(data);

return signature.verify(sign);

}

private static String sign(byte[] data, byte[] privateKey) throws Exception{

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);

KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

PrivateKey priKey = keyFactory.generatePrivate(keySpec);

Signature signature = Signature.getInstance(SIGN_ALGORITHM);

signature.initSign(priKey);

signature.update(data);

// 生成签名的字节数组

byte[] sign = signature.sign();

// 转换成16进制的字符串输出

return Hex.encodeHexString(sign).toUpperCase();

}

/**

* @详细 从文件中读取密钥字符串

* @param filePath 文件路径

* @return

* @throws Exception

*/

public static String loadKey(InputStream in) {

try {

return IOUtils.toString(in, CHARSET);

} catch (Exception ex){

log.error("读取RSA密钥文件出错。");

}

return "";

}

/**

* @详细 生成密钥对

* @param filePath 密钥文件保存的路径

* @throws Exception

*/

public static void initKey(String filePath) throws Exception{

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

keyPairGen.initialize(KEY_SIZE);

KeyPair keyPair = keyPairGen.generateKeyPair();

RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();

RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();

// 得到公钥字符串

String publicKeyString = Base64.encodeBase64String(publicKey.getEncoded());

// 得到私钥字符串

String privateKeyString = Base64.encodeBase64String(privateKey.getEncoded());

// 将密钥对写入到文件

BufferedWriter pubbw = new BufferedWriter(new FileWriter(filePath + "/" + PUBLIC_KEY));

BufferedWriter pribw = new BufferedWriter(new FileWriter(filePath + "/" + PRIVATE_KEY));

log.info(PUBLIC_KEY +"\n" + publicKeyString);

log.info(PRIVATE_KEY +"\n" + privateKeyString);

pubbw.write(publicKeyString);

pribw.write(privateKeyString);

pubbw.flush();

pubbw.close();

pribw.flush();

pribw.close();

}

public static void main(String[] args) throws Exception {

initKey("c:\\tmp");

String data = "这个一段RSA签名的内容。";

String privateKey = loadKey(new FileInputStream("c:\\tmp\\" + PRIVATE_KEY));

String sign = sign(data, privateKey);

log.info("签名内容:\n" + sign);

String publicKey = loadKey(new FileInputStream("c:\\tmp\\" + PUBLIC_KEY));

boolean rs = verify(data, publicKey, sign);

log.info("验签状态:" + rs);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、数字签名原理 用RSA算法数字签名,总的来说,就是签名者用私钥参数d加密,也就是签名;验证者用签字者的公钥参数e解密来完成认证。 下面简要描述数字签名和认证的过程。 (1)、生成密钥 为用户随机生成一对密钥:公钥(e,n)和私钥(d,n). (2)、签名过程 a) 计算消息的散列H(M). b) 用私钥(d,n)加密散列:s=(H(M)) mod n,签名结果就是s. c) 发送消息和签名(M,s). (3)、认证过程 a) 取得发送方的公钥(e,n). b) 解密签名s:h=s mod n. c) 计算消息的散列H(M). d) 比较,如果h=H(M),表示签名有效;否则,签名无效。 根据上面的过程,我们可以得到RSA数字签名的框图如图2-1: 图 2-1 RSA数字签名框图 2、 假设Alice想和Bob通信,以本地两个文件夹Alice和Bob模拟两个用户,实现消息M和签名的模拟分发 (1)、Alice通过RSA算法生成一对密钥:公钥(e,n)和私钥(d,n),将公私钥分别存入pubKey.txt和priKey.txt中。 pubKey.txt中公钥如下: priKey.txt中私钥如下: (2)、将Alice中的pubKey.txt拷到Bob中,模拟公玥的分发。 (3)、将Alice中的消息info.txt做散列,将散列后的存入hashInfo.txt中。 (4)、将Alice中的消息hashInfo.txt和签名sign.txt拷到Bob中,实现M密文状态下的签名与模拟分发、消息传递。 (5)Bob取得公钥pubKey.txt,用公钥解密签名,计算消息的散列H(M).比较,如果h=H(M),表示签名有效;否则,签名无效。 后台运行结果如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值