getkey.java

来自「用纯java语言实现的数字证书制作工具。」· Java 代码 · 共 144 行

JAVA
144
字号
package data;

import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import java.io.*;
import data.MyWindowListener;
import java.sql.*;
import data.Connect;
import data.BASE64Encoder;

public class GetKey
{
	RSAPublicKey pbkey;
	RSAPrivateKey prkey;
	Base64 DE=new Base64();
	BASE64Encoder BE=new BASE64Encoder();
	byte[] b_pbk,b_prk;
	String s_pbk,s_prk,query;
	
	public GetKey(String table,String Item,String value) throws Exception
	{
		
		try{
			
			Connect conn=new Connect();
			Statement stmt=conn.con.createStatement();
			query="select * from "+table+" where "+Item+"="+value;
			ResultSet rs=stmt.executeQuery(query);
			int result=0;
			while(rs.next())
			{
				result++;
				try{
				s_pbk=rs.getString(3);
				byte[] b_pbk =DE.decode(s_pbk.getBytes());

				FileOutputStream f_pbk = new FileOutputStream("temp_pbk.dat");
     			f_pbk.write(b_pbk);
     			f_pbk.close();
     			System.out.println(b_pbk.toString());
     			
				s_prk=rs.getString(4);
				byte[] b_prk =DE.decode(s_prk.getBytes());
				System.out.println(b_prk.toString());
			
     			FileOutputStream f_prk = new FileOutputStream("temp_prk.dat");
     			f_prk.write(b_prk);
     			f_prk.close();
     			
     			}catch(IOException i){
     				System.out.println("临时文件创建错误!");
     			}
				
			}
			if(result==0)/*****测试临时文件读入,跳过数据库,==->!=******/
			{
				System.out.println("还没有密钥对进行签名,请先生成自己密钥对!");
				return;
			}
			else 
			{
				//将读出的公钥字符串转换成RSAPublicKey结构
			try{
				FileInputStream f_pbkey=new FileInputStream("temp_pbk.dat");
     			ObjectInputStream o_pbkey=
     					new ObjectInputStream(f_pbkey);
     			
     			try{
     				pbkey=(RSAPublicKey)o_pbkey.readObject();
     				System.out.println(pbkey+"\n");
     				}catch(ClassNotFoundException cnfe){
     				System.out.println("ClassNotFoundException!");
     			}
     			
     			f_pbkey.close();
     			o_pbkey.close();
     			
     			FileInputStream f_prkey=new FileInputStream("temp_prk.dat");
     			ObjectInputStream o_prkey= new ObjectInputStream(f_prkey);
     			try{prkey=(RSAPrivateKey)o_prkey.readObject();
     				System.out.println(prkey+"\n");
     				}catch(ClassNotFoundException c2){
     					System.out.println("ClassNotFoundException!");
     			}
     			
     			f_prkey.close();
     			o_prkey.close();
     			//删除临时文件
     			
     			try { 
					File file1 = new File("temp_pbk.dat"); 
					File file2=new File("temp_prk.dat");
					if(file2.exists()&&file2.exists()) 
						{ 
							file1.delete(); 
							file2.delete();
							System.out.println("文件存在,已删除!"); 
							 
						} 
					else 
						{ 
							System.out.println("文件不存在!"); 
						} 
					}catch(Exception g){
						System.out.println("删除临时文件错误!");
					}
     			
     			
     			
     			
				}
				catch(IOException o){
					System.out.println("临时文件读取错误!");
				}
				stmt.close();
				conn.con.close();
				return;
			}

		}catch(Exception m)
		{
			System.out.println("错误!");
		}
	}
	
	
	public RSAPublicKey getRSAPublicKey()
	{
		return pbkey;
	}
	
	public RSAPrivateKey getRSAPrivateKey()
	{
		return prkey;
	}
	
/*	public static void main(String args[]) throws Exception
	{
		
	}
*/
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?