Return to Snippet

Revision: 16791
at August 16, 2009 08:56 by geekiuscaesar


Initial Code
package net.java.edem;

import java.io.*;
import java.security.*;
import java.security.spec.*;

public class Adam {

	public static void main(String args[]) {
		Adam adam = new Adam();
		try {
			String path = "C:\\Documents and Settings\\george\\My Documents\\workspaces\\gsoc09\\playground\\tmp";

			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");

			keyGen.initialize(1024);
			KeyPair generatedKeyPair = keyGen.genKeyPair();

			System.out.println("Generated Key Pair");
			adam.dumpKeyPair(generatedKeyPair);
			adam.SaveKeyPair(path, generatedKeyPair);

			KeyPair loadedKeyPair = adam.LoadKeyPair(path, "DSA");
			System.out.println("Loaded Key Pair");
			adam.dumpKeyPair(loadedKeyPair);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}

	private void dumpKeyPair(KeyPair keyPair) {
		PublicKey pub = keyPair.getPublic();
		System.out.println("Public Key: " + getHexString(pub.getEncoded()));

		PrivateKey priv = keyPair.getPrivate();
		System.out.println("Private Key: " + getHexString(priv.getEncoded()));
	}

	private String getHexString(byte[] b) {
		String result = "";
		for (int i = 0; i < b.length; i++) {
			result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
		}
		return result;
	}

	public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
		PrivateKey privateKey = keyPair.getPrivate();
		PublicKey publicKey = keyPair.getPublic();

		// Store Public Key.
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
				publicKey.getEncoded());
		FileOutputStream fos = new FileOutputStream(path + "/public.key");
		fos.write(x509EncodedKeySpec.getEncoded());
		fos.close();

		// Store Private Key.
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
				privateKey.getEncoded());
		fos = new FileOutputStream(path + "/private.key");
		fos.write(pkcs8EncodedKeySpec.getEncoded());
		fos.close();
	}

	public KeyPair LoadKeyPair(String path, String algorithm)
			throws IOException, NoSuchAlgorithmException,
			InvalidKeySpecException {
		// Read Public Key.
		File filePublicKey = new File(path + "/public.key");
		FileInputStream fis = new FileInputStream(path + "/public.key");
		byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
		fis.read(encodedPublicKey);
		fis.close();

		// Read Private Key.
		File filePrivateKey = new File(path + "/private.key");
		fis = new FileInputStream(path + "/private.key");
		byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
		fis.read(encodedPrivateKey);
		fis.close();

		// Generate KeyPair.
		KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
		X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
				encodedPublicKey);
		PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

		PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
				encodedPrivateKey);
		PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

		return new KeyPair(publicKey, privateKey);
	}
}

Initial URL
http://java.sun.com/docs/books/tutorial/security/apisign/vstep2.html

Initial Description
Save/Load or.. Write/Read or.. Store/Retrieve Private Key/Public Key to/from disk/file :D

Initial Title
Save/Load  Private and Public Key to/from a file

Initial Tags
java

Initial Language
Java