Java实现rsa数字签名代码和验签
时间: 2023-11-14 17:07:05 浏览: 92
使用RSA生成公私钥对、加解密、及签名验签 JAVA源码.rar
RSA数字签名是非常重要的加密算法之一,以下是Java实现RSA数字签名的代码示例:
## 数字签名
```java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAUtil {
private static final String ALGORITHM = "RSA";
private static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";
/**
* 从文件中加载私钥
*
* @param privateKeyFile 私钥文件
* @return 私钥
* @throws Exception
*/
public static PrivateKey loadPrivateKeyFromFile(File privateKeyFile) throws Exception {
FileInputStream fis = new FileInputStream(privateKeyFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
fis.close();
bos.close();
byte[] privateKeyBytes = bos.toByteArray();
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return keyFactory.generatePrivate(pkcs8KeySpec);
}
/**
* 从文件中加载公钥
*
* @param publicKeyFile 公钥文件
* @return 公钥
* @throws Exception
*/
public static PublicKey loadPublicKeyFromFile(File publicKeyFile) throws Exception {
FileInputStream fis = new FileInputStream(publicKeyFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
fis.close();
bos.close();
byte[] publicKeyBytes = bos.toByteArray();
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return keyFactory.generatePublic(x509KeySpec);
}
/**
* 对数据进行数字签名
*
* @param data 数据
* @param privateKey 私钥
* @return 数字签名
* @throws Exception
*/
public static byte[] sign(byte[] data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* 验证数字签名
*
* @param data 数据
* @param publicKey 公钥
* @param signedData 数字签名
* @return 是否验证通过
* @throws Exception
*/
public static boolean verify(byte[] data, PublicKey publicKey, byte[] signedData) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(signedData);
}
}
```
## 使用示例
```java
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.PublicKey;
public class Main {
public static void main(String[] args) throws Exception {
// 加载私钥
File privateKeyFile = new File("private_key.txt");
PrivateKey privateKey = RSAUtil.loadPrivateKeyFromFile(privateKeyFile);
// 加载公钥
File publicKeyFile = new File("public_key.txt");
PublicKey publicKey = RSAUtil.loadPublicKeyFromFile(publicKeyFile);
// 待签名数据
String data = "Hello, World!";
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
// 数字签名
byte[] signedData = RSAUtil.sign(dataBytes, privateKey);
// 验证数字签名
boolean verified = RSAUtil.verify(dataBytes, publicKey, signedData);
System.out.println("Verified: " + verified);
}
}
```
以上代码示例中,我们使用了`RSAUtil`类中的`loadPrivateKeyFromFile`方法和`loadPublicKeyFromFile`方法分别从私钥文件和公钥文件中加载私钥和公钥。然后我们使用`sign`方法对待签名数据进行数字签名,使用`verify`方法对签名结果进行验证。
切记,要使用自己的密钥对,不要使用他人的密钥对。
阅读全文