package backend;

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

/*
 * 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(KeyPair kp){
		RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();
		return publicKey.getModulus().toString() + "|" +
		    publicKey.getPublicExponent().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 | NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pubkey;
	}
	

}

@@ -102,63 +102,80 @@
 ity.
-KeyPair;%0D%0Aimport java.security.NoSuchAlgorithmException
+NoSuchAlgorithmException;%0D%0Aimport java.security.interfaces.RSAPrivateKey
 ;%0D%0Ai
@@ -316,16 +316,63 @@
 eySpec;%0D
+%0Aimport java.security.spec.X509EncodedKeySpec;%0D
 %0A%0D%0A/*%0D%0A 
@@ -691,157 +691,1044 @@
 Key(
-KeyPair kp)%7B%0D%0A%09%09RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();%0D%0A%09%09return publicKey.getModulus().toString() + %22%7C%22 +%0D%0A%09%09    publicKey.getPublic
+RSAPublicKey publicKey)%7B%0D%0A%09%09return publicKey.getModulus().toString() + %22%7C%22 +%0D%0A%09%09    publicKey.getPublicExponent().toString();%0D%0A%09%7D%0D%0A%09%09%0D%0A%09%0D%0A%09public String SerializeByteKey(byte%5B%5D bytePubKey)%7B%0D%0A%09%09X509EncodedKeySpec pubspec = new X509EncodedKeySpec(bytePubKey);%0D%0A%09%09KeyFactory factory;%0D%0A%09%09RSAPublicKey publicKey = null;%0D%0A%09%09try %7B%0D%0A%09%09%09factory = KeyFactory.getInstance(%22RSA%22);%0D%0A%09%09%09publicKey = (RSAPublicKey) factory.generatePublic(pubspec);%0D%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0D%0A%09%09%09// TODO Auto-generated catch block%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D catch (InvalidKeySpecException e) %7B%0D%0A%09%09%09// TODO Auto-generated catch block%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09return publicKey.getModulus().toString() + %22%7C%22 +%0D%0A%09%09    publicKey.getPublicExponent().toString();%0D%0A%09%7D%0D%0A%09%0D%0A%09/**%0D%0A%09 * Serialize the private key into string%0D%0A%09 * @param privateKey%0D%0A%09 * @return the string of %22modulus%7Cexponent%22 of private key%0D%0A%09 */%0D%0A%09public String SerializePrivateKey(RSAPrivateKey privateKey)%7B%0D%0A%09%09return privateKey.getModulus().toString() + %22%7C%22 +%0D%0A%09%09privateKey.getPrivate
 Expo
@@ -2117,32 +2117,36 @@
 pubkey = null;%0D%0A
+%09%09%0D%0A
 %09%09try %7B%0D%0A%09%09%09pubk
@@ -2257,10 +2257,89 @@
 ion 
-%7C 
+e) %7B%0D%0A%09%09%09// TODO Auto-generated catch block%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D catch (
 NoSu
@@ -2426,32 +2426,36 @@
 kTrace();%0D%0A%09%09%7D%0D%0A
+%09%09%0D%0A
 %09%09return pubkey;

add serialization byte