RSA 암호 화 자바 버 전 데모

import java.io.FileInputStream;
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.util.Scanner;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 
 * @author leon
 *
 */
class RSAUtils {
  //              
	private static String PUBLIC_KEY_FILE = "PublicKey";
	//              
	private static String PRIVATE_KEY_FILE = "PrivateKey";

	//               
	public static void setKeyPath(String path) {
		if (PUBLIC_KEY_FILE.equals("PublicKey")) {
			PUBLIC_KEY_FILE = path + (path.endsWith("//")?"PublicKey":"/PublicKey");
			PRIVATE_KEY_FILE = path + (path.endsWith("//")?"PrivateKey":"/PrivateKey");
		}
	}

	//      
	private static void createKeyPair() throws Exception {
		//  RSA            keyPairGenerator
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		//       1024
		keyPairGenerator.initialize(1024);
		//     
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		//    
		Key publicKey = keyPair.getPublic();
		//    
		Key privateKey = keyPair.getPrivate();
		//                 
		ObjectOutputStream oos1 = null;
		ObjectOutputStream oos2 = null;
		try {
			oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
			oos2 = new ObjectOutputStream(
					new FileOutputStream(PRIVATE_KEY_FILE));
			oos1.writeObject(publicKey);
			oos2.writeObject(privateKey);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			oos1.close();
			oos2.close();
		}
	}
    
	//RSA  
	public static String encryptWithRSA(String str) throws Exception {
		createKeyPair();
		Key publicKey = null;
		//          
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
			publicKey = (Key) ois.readObject();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			ois.close();
		}

		//         RSA       cipher。
		Cipher cipher = Cipher.getInstance("RSA");
		//       ,     cipher。
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		//    
		byte[] secret = cipher.doFinal(str.getBytes());
		//  Base64  
		return new BASE64Encoder().encode(secret);
	}

	//RSA  
	public static String decryptWithRSA(String secret) throws Exception {
		Key privateKey;
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
			privateKey = (Key) ois.readObject();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			ois.close();
		}
		Cipher cipher = Cipher.getInstance("RSA");
		//    ,       。
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		//      Base64      ,        
		byte[] b = cipher.doFinal(new BASE64Decoder().decodeBuffer(secret));
		//      
		return new String(b);

	}
}
public class RSA_Demo {
  public static void main(String[] args) throws Exception {
		//          ,  ,       
		//RSAUtils.setKeyPath(str);
		System.out.println("     :");
		Scanner sca = new Scanner(System.in);
		String str =sca.nextLine();
		System.out.println("============================");
		String secret = RSAUtils.encryptWithRSA(str);
		System.out.println("  RSA       :");
		System.out.println(secret);
		System.out.println("============================");
		String original = RSAUtils.decryptWithRSA(secret);
		System.out.println("  RSA       :");
		System.out.println(original);
	}
}

좋은 웹페이지 즐겨찾기