如何使用java生成公钥和私钥程序源码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
public class SecurityUtil {
/**
* 使用MD5先对密码加密
*/
public static String string2MD5(String inStr){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance(“MD5”);
}catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
return “”;
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++){
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append(“0”);
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 生成公钥和私钥
*/
public static void createkey(String signaturePassword) throws Exception{
KeyPairGenerator keygen=KeyPairGenerator.getInstance(“DSA”);
//如果设定随机产生器就用如下代码初始化
SecureRandom secrand=new SecureRandom();
if(null != signaturePassword && !signaturePassword.equals(“”)){
//对签名密码进行MD5加密
String ressignaturePassword=string2MD5(signaturePassword);
System.out.println(“原密码 “+signaturePassword);
System.out.println(“MD5后的密码 “+ressignaturePassword);
secrand.setSeed(ressignaturePassword.getBytes()); // 初始化随机产生器
}
if(null != secrand){
keygen.initialize(512,secrand); // 初始化密钥生成器
}else if(null == secrand){
//否则
keygen.initialize(512);
}
//生成密钥公钥 pubkey 和私钥 prikey
KeyPair keys=keygen.generateKeyPair(); // 生成密钥组
PublicKey pubkey=keys.getPublic();
PrivateKey prikey=keys.getPrivate();
//分别保存在 myprikey.dat 和 mypubkey.dat 中 , 以便下次不在生成生成密钥对的时间比较长
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream(“F:/myprikey.key”));//私钥保存
out.writeObject(prikey);
out.close();
out=new ObjectOutputStream(
new FileOutputStream(“F:/mypubkey.key”));//公钥保存
out.writeObject(pubkey);
out.close();
}
/**
* 使用私钥对信息加密
*/
public static void privateEncode(String myinfo) throws Exception{
ObjectInputStream in=new ObjectInputStream(
new FileInputStream(“F:/myprikey.key”));
PrivateKey myprikey=(PrivateKey)in.readObject();
in.close();
//初始一个 Signature 对象 , 并用私钥对信息签名
Signature signet=Signature.getInstance(“DSA”);
signet.initSign(myprikey);
signet.update(myinfo.getBytes());
byte[] signed=signet.sign();
System.out.println(“私钥签名前的信息 “+myinfo);
System.out.println(“私钥签名后的信息 “+signed);
//把信息和签名保存在一个文件中 (myinfo.dat)
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream(“F:/myinfo.dat”));
out.writeObject(myinfo);
out.writeObject(signed);
out.close();
}
/**
* 公钥验证,私钥签名后的信息
*/
public static void validate() throws Exception{
//拿到公钥
ObjectInputStream in=new ObjectInputStream(new FileInputStream(“F:/mypubkey.key”));
PublicKey pubkey=(PublicKey)in.readObject();
in.close();
//读入签名和信息
in=new ObjectInputStream(new FileInputStream(“F:/myinfo.dat”));
String info=(String)in.readObject();
byte[] signed=(byte[])in.readObject();
in.close();
//初始一个 Signature 对象 , 并用公钥和签名进行验证
Signature signetcheck=Signature.getInstance(“DSA”);
signetcheck.initVerify(pubkey);
signetcheck.update(info.getBytes());
if (signetcheck.verify(signed)) {
System.out.println(“签名正常 √”);
}else{
System.out.println(“签名异常 ×”);
}
}
public static void main(String[] args) throws Exception {
createkey(“6666662xVVVVxxBxx”);//创建公钥和私钥
//privateEncode(“这是我的密码为8888888”);//私钥对信息进行加密
validate();
}
}
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14721.html