JAVA公鑰私鑰

package com.gavin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class KeyRSA {
 
 private KeyPairGenerator kpg=null;
 private KeyPair kp=null;
 private PublicKey publicKey=null;
 private PrivateKey privateKey=null;
 private FileOutputStream publicFileOut=null;
 private ObjectOutputStream publicObjectOut=null;
 private FileOutputStream privateFileOut=null;
 private ObjectOutputStream privateObjectOut=null;
 
 public KeyRSA(int in,String path)throws NoSuchAlgorithmException,FileNotFoundException,IOException{
  kpg=KeyPairGenerator.getInstance("RSA");
  kpg.initialize(in);
  kp=kpg.generateKeyPair();
  publicKey=kp.getPublic();
  privateKey=kp.getPrivate();
  //save public key
  publicFileOut=new FileOutputStream(path+File.separator+"publicKey.dat");
  publicObjectOut=new ObjectOutputStream(publicFileOut);
  publicObjectOut.writeObject(publicKey);
  //save private key
  privateFileOut=new FileOutputStream(path+File.separator+"privateKey.dat");
  privateObjectOut=new ObjectOutputStream(privateFileOut);
  privateObjectOut.writeObject(privateKey);
  
 }
 
 public static String encrypt(String path,String source) throws Exception{
  Key publicKey=null;
  ObjectInputStream ois=null;
  try {
   ois=new ObjectInputStream(new FileInputStream(path+File.separator+"publicKey.dat"));
   publicKey=(Key)ois.readObject();
  } catch (Exception e) {
   // TODO: handle exception
  } finally {
   ois.close();
  }
  Cipher cipher=Cipher.getInstance("RSA");
  cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  byte[] b=source.getBytes();
  
  byte[] b1=cipher.doFinal(b);
  BASE64Encoder encoder=new BASE64Encoder();
  return encoder.encode(b1);
 }
 
 
 public static String decrypt(String path,String source) throws Exception{
  Key privateKey=null;
  ObjectInputStream ois=null;
  try {
   ois=new ObjectInputStream(new FileInputStream(path+File.separator+"privateKey.dat"));
   privateKey=(Key)ois.readObject();
  } catch (Exception e) {
   // TODO: handle exception
  } finally {
   ois.close();
  }
  Cipher cipher=Cipher.getInstance("RSA");
  cipher.init(Cipher.DECRYPT_MODE, privateKey);
  
  BASE64Decoder decoder=new BASE64Decoder();
  byte[] b1=decoder.decodeBuffer(source);
  
  byte[] b=cipher.doFinal(b1);
  return new String(b);
 }
 public String getCertificate() throws Exception{
  CertificateFactory cf=CertificateFactory.getInstance("X.509");
  FileInputStream in=new FileInputStream("I:\\key\\publicKey.dat");
  Certificate c=cf.generateCertificate(in);
  return c.toString();
 }
 public KeyRSA(){}
 public static void main(String[] args) throws Exception{
  String source="hello i love you gavin kit peng peng";
  String path="I:\\key";
  String enStr=KeyRSA.encrypt(path, source);
  
  System.out.println(enStr);
  
  System.out.println("xxxx:"+KeyRSA.decrypt(path, enStr));
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章