java安全6

package com.ngsn.security;


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

/**
* 数字签名(私钥签名公钥校验)
* 此 Signature 类用来为应用程序提供数字签名算法功能。
* 数字签名用于确保数字数据的验证和完整性。
* @author John.Yao
*/
public class SecretKeyTest5 {

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

}
/**
* 签名
* 根据私钥对数据进行签名,生成签名字节
*/
private static void signature() throws Exception{
//签名
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = generator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
//创建signature对象
Signature signature = Signature.getInstance("SHA1withRSA");
//私钥进行签名
signature.initSign(privateKey);
//更新要由字节签名或验证的数据。
signature.update("到底是怎么回事,为什么不行呢".getBytes()); //签名的数据
//返回所有已更新数据的签名字节。
byte[] result = signature.sign(); //得到签名的字节
saveKey(publicKey,"sign_public.key");
saveData(result,"mysign.data");
}
/**
* 校验
* 根据公钥和签名字节进行数据的校验
* @throws Exception
*/
private static void verify() throws Exception{
//校验
//创建signature对象
Signature signature = Signature.getInstance("SHA1withRSA");
//从文件中读取保存的公钥
PublicKey publicKey = (PublicKey)readKey("sign_public.key");
//公钥进行校验
signature.initVerify(publicKey);
//添加校验的数据
signature.update("到底是怎么回事,为什么不行呢".getBytes());
//从文件中读取保存的签名字节
byte[] signed = readData("mysign.data");
//根据签名字节进行校验
boolean b = signature.verify(signed);

if(b){
System.out.println("校验结果:"+b+",是自己的签名");
}else{
System.out.println("校验结果:"+b+",不是自己的签名");
}
}
/**
* 保存密钥到指定的文件中
* @param publicKey
* @param fileName
* @throws Exception
*/
public static void saveKey(PublicKey publicKey,String fileName) throws Exception{
FileOutputStream fosKey = new FileOutputStream(fileName);
ObjectOutputStream oosSecretKey = new ObjectOutputStream(fosKey);
//将密钥保存到zxx_secret.key文件中
oosSecretKey.writeObject(publicKey);
oosSecretKey.close();
fosKey.close();
}
/**
* 保存数据到指定的文件中
* @param data
* @param fileName
* @throws Exception
*/
public static void saveData(byte[] data,String fileName) throws Exception{
FileOutputStream fosData = new FileOutputStream(fileName);
fosData.write(data);
fosData.close();
}
/**
* 从指定的文件中读取密钥
* @param fileName
* @return
* @throws Exception
*/
public static Key readKey(String fileName) throws Exception{
FileInputStream fisKey = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fisKey);
Key key = (Key)ois.readObject();
ois.close();
fisKey.close();
return key;
}
/**
* 从指定的文件中读取数据
* @param fileName
* @return
* @throws Exception
*/
public static byte[] readData(String fileName) throws Exception{
FileInputStream fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = fis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
//从输出流中得到byte[]的加密数据
byte[] result = bos.toByteArray();
fis.close();
bos.close();
return result;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值