package backend;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

/*
 * To serialize and deserialize the key
 */
public class Serialization {
	public Serialization(){
		
	}
	
	/**
	 * return string of "1234|5678" 
	 * where 1234 is publicKeyModulus and 5678 is publicKeyExponent
	 * @param kp keypair
	 * @return serialized keystring
	 */
	public String SerializePublicKey(RSAPublicKey publicKey){
		return publicKey.getModulus().toString() + "|" +
		    publicKey.getPublicExponent().toString();
	}
		
	
	public String SerializeByteKey(byte[] bytePubKey){
		X509EncodedKeySpec pubspec = new X509EncodedKeySpec(bytePubKey);
		KeyFactory factory;
		RSAPublicKey publicKey = null;
		try {
			factory = KeyFactory.getInstance("RSA");
			publicKey = (RSAPublicKey) factory.generatePublic(pubspec);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return publicKey.getModulus().toString() + "|" +
		    publicKey.getPublicExponent().toString();
	}
	
	/**
	 * Serialize the private key into string
	 * @param privateKey
	 * @return the string of "modulus|exponent" of private key
	 */
	public String SerializePrivateKey(RSAPrivateKey privateKey){
		return privateKey.getModulus().toString() + "|" +
		privateKey.getPrivateExponent().toString();
	}
	
	/**
	 * Deserialize keystring to RSAPublicKey 
	 * @param keyString
	 * @return RSAPublicKey
	 */
	public RSAPublicKey DeserializePublicKey(String pubKeyString){
		String []Parts = pubKeyString.split("\\|");
		RSAPublicKeySpec Spec = new RSAPublicKeySpec(
		        new BigInteger(Parts[0]),
		        new BigInteger(Parts[1]));
		RSAPublicKey pubkey = null;
		
		try {
			pubkey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(Spec);
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pubkey;
	}
	

}

@@ -446,16 +446,63 @@
 tion %7B%0D%0A
+%09ConstantValues CV = new ConstantValues();%0D%0A%09%0D%0A
 %09public 
@@ -2510,13 +2510,453 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09//get the expiration date from the certificate%0D%0A%09public byte%5B%5D getExpFromCert(byte%5B%5D cert)%7B%0D%0A%09%09byte%5B%5D exp = new byte%5BCV.EXP_LENGTH%5D;%0D%0A%09%09System.arraycopy(cert, 164, exp, 0, CV.EXP_LENGTH);%0D%0A%09%09return exp;%0D%0A%09%7D%0D%0A%0D%0A%09//get public key from certificate%0D%0A%09public byte%5B%5D getPublicKeyFromCert(byte%5B%5D cert)%7B%0D%0A%09%09byte%5B%5D pubKey = new byte%5BCV.RSAPUBLICKEYLENGTH%5D;%0D%0A%09%09System.arraycopy(cert, 1, pubKey, 0, CV.RSAPUBLICKEYLENGTH);%0D%0A%09%09return pubKey;%09%09%0D%0A%09%7D%0D%0A%0D%0A
 %0D%0A%7D%0D%0A

add the extract expiration and public key from certificate