import java.io.*;

public class WTest {
	public static void main( String args[] ) {

		WTest w = new WTest();

		try {

			w.guardaClaus("public","private");
  			w.carregaClaus("public","private","");

		}
		catch (Exception e) {} ;

	} 

  public void guardaClaus(String arxiuPublica, String arxiuPrivada) throws Exception{    
//	  PrivateKey pk = _keyPair.getPrivate() ;
//	  PublicKey puk = _keyPair.getPublic();
//
//	  PKCS8EncodedKeySpec eks = new PKCS8EncodedKeySpec(pk.getEncoded());
//	  X509EncodedKeySpec eks2 = new X509EncodedKeySpec(puk.getEncoded());
//
//	  KeyFactory kf = KeyFactory.getInstance(pk.getAlgorithm());
//
//	  PrivateKey pk2 = kf.generatePrivate(eks);
//	  PublicKey puk2 = kf.generatePublic(eks2);
//
//	  this._keyPair = new KeyPair(puk2,pk2);

	 String algo = "RSA";

	 System.out.println(algo);

	 byte[] b = (algo+"\n").getBytes();
	 byte[] t =  { 1 , 2 , 4 , 4 , 77, 66, 33 ,45 } ;

	 //for (int i=0; i<b.length; i++) System.out.print(b[i]) ;

	 FileOutputStream fos = new FileOutputStream(new File(arxiuPublica)) ;
	 fos.write(b);
	 fos.write(t);
	 fos.close();

  }

  public void carregaClaus(String arxiuPublica, String arxiuPrivada, String alg) throws Exception{
	  // implementation distinto. Typo de algorithmos tambien guardado en fitchero
	  // por eso el tercero argumento sera ignorado
	  
	  FileInputStream fis = new FileInputStream(new File(arxiuPublica));

	  java.util.Vector v = new java.util.Vector();


	  int c = fis.read();

	  while ( c != -1 && (byte)c != ("\n".getBytes())[0] ) {
		  v.add(new Byte( (byte)c ) );
		  c=fis.read();
	  }

	  fis.close();

	  byte[] b = new byte[v.size()];
	  for (int i=0; i<v.size(); i++) b[i] = ((Byte)v.elementAt(i)).byteValue() ;

	  String algo  =  new String(b);

	  System.out.println(algo);


  }
}

