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.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;
import javax.crypto.Cipher;


public class RSALibrary {

  // String to hold name of the encryption algorithm.
  public final String ALGORITHM = "RSA";

  //String to hold the name of the private key file.
  public final String PRIVATE_KEY_FILE = "./private.key";

  // String to hold name of the public key file.
  public final String PUBLIC_KEY_FILE = "./public.key";

  /***********************************************************************************/
   /* Generates an RSA key pair (a public and a private key) of 1024 bits length */
   /* Stores the keys in the files defined by PUBLIC_KEY_FILE and PRIVATE_KEY_FILE */
   /* Throws IOException */
  /***********************************************************************************/
  public void generateKeys() throws IOException {

    try {

      final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
      keyGen.initialize(1024);

	  // TO-DO: Use KeyGen to generate a public and a private key
      // ...

	  // TO-DO: store the public key in the file PUBLIC_KEY_FILE
	  // ...

	  // TO-DO: store the private key in the file PRIVATE_KEY_FILE
	  // ...

	} catch (NoSuchAlgorithmException e) {
		System.out.println("Exception: " + e.getMessage());
		System.exit(-1);
	}
  }


  /***********************************************************************************/
  /* Encrypts a plaintext using an RSA public key. */
  /* Arguments: the plaintext and the RSA public key */
  /* Returns a byte array with the ciphertext */
  /***********************************************************************************/
  public byte[] encrypt(byte[] plaintext, PublicKey key) {

    byte[] ciphertext = null;

    try {

      // Gets an RSA cipher object
      final Cipher cipher = Cipher.getInstance(ALGORITHM);

      // TO-DO: initialize the cipher object and use it to encrypt the plaintext
	  // ...

    } catch (Exception e) {
      e.printStackTrace();
    }
    return ciphertext;
  }


  /***********************************************************************************/
  /* Decrypts a ciphertext using an RSA private key. */
  /* Arguments: the ciphertext and the RSA private key */
  /* Returns a byte array with the plaintext */
  /***********************************************************************************/
  public byte[] decrypt(byte[] ciphertext, PrivateKey key) {

    byte[] plaintext = null;

    try {
      // Gets an RSA cipher object
      final Cipher cipher = Cipher.getInstance(ALGORITHM);

      // TO-DO: initialize the cipher object and use it to decrypt the ciphertext
	  // ...

    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return plaintext;
  }

  /***********************************************************************************/
  /* Signs a plaintext using an RSA private key. */
  /* Arguments: the plaintext and the RSA private key */
  /* Returns a byte array with the signature */
  /***********************************************************************************/
  public byte[] sign(byte[] plaintext, PrivateKey key) {
		
    byte[] signedInfo = null;

    try {

	  // Gets a Signature object
      Signature signature = Signature.getInstance("SHA1withRSA");

	  // TO-DO: initialize the signature oject with the private key
	  // ...
	
	  // TO-DO: set plaintext as the bytes to be signed
	  // ...
	
	  // TO-DO: sign the plaintext and obtain the signature (signedInfo)
	  // ...

    } catch (Exception ex) {
      ex.printStackTrace();
    }

	return signedInfo;
  }
	
  /***********************************************************************************/
  /* Verifies a signature over a plaintext */
  /* Arguments: the plaintext, the signature to be verified (signed) 
  /* and the RSA public key */
  /* Returns TRUE if the signature was verified, false if not */
  /***********************************************************************************/
  public boolean verify(byte[] plaintext, byte[] signed, PublicKey key) {

	boolean result = false;

    try {

 	 // Gets a Signature object
     Signature signature = Signature.getInstance("SHA1withRSA");

	  // TO-DO: initialize the signature oject with the public key
	  // ...

	  // TO-DO: set plaintext as the bytes to be veryfied
	  // ...

	  // TO-DO: verify the signature (signed). Store the outcome in the boolean result
	  // ...
	
    } catch (Exception ex) {
      ex.printStackTrace();
    }

	return result;
  }
	
}



@@ -206,32 +206,58 @@
 ctOutputStream;%0A
+import java.security.Key;%0A
 import java.secu
@@ -385,24 +385,79 @@
 PrivateKey;%0A
+import java.io.PrintWriter;%0Aimport java.io.FileWriter;%0A
 import java.
@@ -1490,16 +1490,858 @@
 e(1024);
+%0A      KeyPair keyPair = keyGen.generateKeyPair();%0A%0A      File privateKey = new File(%22pruebapriv.key%22);%0A      File publicKey = new File(%22pruebapub.key%22);%0A%0A      if(privateKey.getParentFile() != null)%7B%0A        privateKey.getParentFile().mkdirs();%0A      %7D%0A      privateKey.createNewFile();%0A%0A      if(publicKey.getParentFile() != null)%7B%0A        publicKey.getParentFile().mkdirs();%0A      %7D%0A      publicKey.createNewFile();%0A%0A      ObjectOutputStream privateKeyOS = new ObjectOutputStream(%0A              new FileOutputStream(privateKey));%0A      privateKeyOS.writeObject(keyPair.getPrivate());%0A      privateKeyOS.close();%0A%0A      ObjectOutputStream publicKeyOS = new ObjectOutputStream(%0A              new FileOutputStream(publicKey));%0A      publicKeyOS.writeObject(keyPair.getPublic());%0A      publicKeyOS.close();%0A%0A      // TODO: solve encoding issue
 %0A%0A%09  // 
@@ -2657,29 +2657,267 @@
 ));%0A
-%09%09System.exit(-1);%0A%09%7D
+        e.printStackTrace();%0A%09%09System.exit(-1);%0A%09%7D%0A  %7D%0A%0A  public static void main (String%5B%5D args) %7B%0A      RSALibrary rsa = new RSALibrary();%0A      try %7B%0A          rsa.generateKeys();%0A      %7D catch(IOException e) %7B%0A        System.err.println(%22error%22);%0A      %7D%0A
 %0A  %7D

Save private and public key in a file