📄 keyrsa.java
字号:
package com.zzvcom.license.keytools;
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.PrivateKey;
import java.security.PublicKey;
import java.sql.Connection;
import java.sql.Statement;
import javax.crypto.Cipher;
public class KeyRSA
{
public void KeyRSA(int in, String address) throws Exception
{
KeyPairGenerator kpg = null;
KeyPair kp = null;
PublicKey public_key = null;
PrivateKey private_key = null;
FileOutputStream public_file_out = null;
ObjectOutputStream public_object_out = null;
FileOutputStream private_file_out = null;
ObjectOutputStream private_object_out = null;
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙
// 保存公匙
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
// 保存私匙
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(private_key);
}
public static void main(String[] args) throws Exception
{
System.out.println("私匙和公匙保存到C盘下的文件中.");
KeyRSA key = new KeyRSA();
// key.KeyRSA(512, "c:/");
String src = "111111111111";
String t1 = key.Encrypt(src);
System.out.println("密文----->"+t1);
//解密
String t2= key.Decoder(t1);
System.out.println("明文----->"+t2);
}
public String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public byte[] hexStr2ByteArr(String strIn) throws Exception
{
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2)
{
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
// System.out.println(arrOut[i/2]);
}
return arrOut;
}
public String Encrypt(String cardOrPwd) throws Exception
{
if (null == cardOrPwd || cardOrPwd.equals(""))
{
throw new Exception("加密参数不能为空!");
}
String url = getClass().getClassLoader().getResource("public_key.dat")
.getPath();
FileInputStream fis = new FileInputStream(url);
ObjectInputStream oos = new ObjectInputStream(fis);
PublicKey pulickey = (PublicKey) oos.readObject();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pulickey);
byte[] t = cipher.doFinal(cardOrPwd.getBytes("UTF-8"));
return byte2hex(t);
}
public String Decoder(String pwdStr) throws Exception
{
if (null == pwdStr || pwdStr.equals(""))
{
throw new Exception("解密参数不能为空!");
}
String url = getClass().getClassLoader().getResource("private_key.dat")
.getPath();
FileInputStream private_file = new FileInputStream(url);
ObjectInputStream private_oos = new ObjectInputStream(private_file);
PrivateKey private_key = (PrivateKey) private_oos.readObject();
byte[] bs = hexStr2ByteArr(pwdStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, private_key);
byte[] t2 = cipher.doFinal(bs);
return new String(t2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -