📄 test.java
字号:
package ly.util;
import java.sql.*;
import java.io.*;
import java.sql.*;
import java.security.*;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.awt.*;
public class test {
/*
public static void main(String[] args){
DSBLMainFrame frame=new DSBLMainFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
*/
/*
public static void main(String[] args){
String conURL="jdbc:odbc:driver={MicroSoft Access Driver (*.mdb)};DBQ=db\\MyRSAStore.mdb";
String password="123";
Connection con=null;
PreparedStatement pst=null;
try{
KeyPair keyPair = lyRSA.generateKeyPair(1024);
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey priKey = (RSAPrivateKey) keyPair.getPrivate();
byte[] pubModBytes = pubKey.getModulus().toByteArray();//取得公要模
byte[] pubPubExpBytes = pubKey.getPublicExponent().toByteArray();//取得公要指数
byte[] priModBytes = priKey.getModulus().toByteArray();//取得私要模
byte[] priPriExpBytes = priKey.getPrivateExponent().toByteArray();//取得私要指数
RSAPublicKey recoveryPubKey = lyRSA.generateRSAPublicKey(pubModBytes,
pubPubExpBytes);
RSAPrivateKey recoveryPriKey = lyRSA.generateRSAPrivateKey(priModBytes,
priPriExpBytes);
ByteArrayOutputStream bout1 = new ByteArrayOutputStream();
ObjectOutputStream oout1 = new ObjectOutputStream(bout1);
oout1.writeObject(recoveryPubKey);
oout1.flush();
oout1.close();
ByteArrayOutputStream bout2 = new ByteArrayOutputStream();
ObjectOutputStream oout2 = new ObjectOutputStream(bout2);
oout2.writeObject(priKey);
oout2.flush();
oout2.close();
ByteArrayInputStream pubin=new ByteArrayInputStream(bout1.toByteArray());
ByteArrayInputStream priin=new ByteArrayInputStream(bout2.toByteArray());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(conURL,password,password);
pst=con.prepareStatement("insert into MyRSAKeys(name,description,RSApublicKey,RSAprivateKey,create_data) values (?,?,?,?,?)");
pst.setString(1,"test");
pst.setString(2,"no");
pst.setBinaryStream(3,pubin,pubin.available());
pst.setBinaryStream(4,priin,priin.available());
pst.setString(5,new java.sql.Date(new java.util.Date().getTime()).toString());
pst.executeUpdate();
pst=con.prepareStatement("select * from MyRSAKeys");
ResultSet rs=pst.executeQuery();
RSAPublicKey r=null;
RSAPrivateKey p=null;
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
r=(RSAPublicKey)(new ObjectInputStream(new ByteArrayInputStream(rs.getBytes(4))).readObject());
p = (RSAPrivateKey)(new ObjectInputStream(new ByteArrayInputStream(rs.getBytes(5))).readObject());
System.out.println(r);
System.out.println(p);
System.out.println(rs.getString(6));
}
pst.close();
con.close();
File file = new File("d:\\test.txt");
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = in.read(tmpbuf)) != -1) {
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
in.close();
byte[] orgData = bout.toByteArray();
byte[] raw = lyRSA.encrypt(p, orgData);
file = new File("d:\\encrypt_result.txt");
OutputStream out = new FileOutputStream(file);
out.write(raw);
out.close();
byte[] data = lyRSA.decrypt(r, raw);
file = new File("d:\\decrypt_result.txt");
out = new FileOutputStream(file);
out.write(data);
out.flush();
out.close();
}catch(Exception e){
System.out.println(e);
}
try{
pst.close();
con.close();
}catch(Exception e){
System.out.println(e);
}
}
*
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -