import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class mycipher 
{		
	public static void main(String[] args) throws Exception
	{	String inputString = "";
		int c;
		while( (c = System.in.read()) != -1)
		{	inputString += (char)c;	}
		byte[] inputByte = inputString.getBytes();
		String inputHex = byteToHex(inputByte);
		String password = args[1];
		byte[] key = password.getBytes();
		
		SecretKeySpec secretkeyspec = new SecretKeySpec(key, "HmacSHA512");
		Mac mac = Mac.getInstance(secretkeyspec.getAlgorithm());
		mac.init(secretkeyspec);
		DESedeKeySpec deskey = new DESedeKeySpec(setKey(key));
		SecretKeyFactory secretkeyfactory  = SecretKeyFactory.getInstance("DESede");
		SecretKey secretkey=  secretkeyfactory.generateSecret(deskey);

		if(args[0].equals("-E"))
		{	byte [] byteMac = mac.doFinal(inputByte);
			String hexByteMac = byteToHex(byteMac);
			byte[] bytePlain = new byte[byteMac.length+inputByte.length]; 
			for(int i=0;i < 64;i++)
			{	bytePlain[i] = byteMac[i];	}
			for(int i=0;i < inputByte.length;i++)
			{	bytePlain[i+64] = inputByte[i];	}
			SecureRandom sr = new SecureRandom();
			byte[] pseudoRandom = new byte[8];
			sr.nextBytes(pseudoRandom);		
			IvParameterSpec iv = new IvParameterSpec(pseudoRandom);

			Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE,secretkey,iv);
			byte[] cipherText = cipher.doFinal(bytePlain);
			String cipherString = byteToHex(cipherText);
			String ivHex = byteToHex(pseudoRandom);
			cipherString = ivHex.concat(cipherString);
			System.out.print(cipherString);			
		}
		if(args[0].equals("-D"))
		{	String cipherText = inputString;
			String ivHex = cipherText.substring(0, 16);
        	byte[] pseudoRandom = hexToByte(ivHex);
			    
        	IvParameterSpec iv = new IvParameterSpec(pseudoRandom);
        	cipherText = cipherText.substring(16, cipherText.length());
			byte[] cipherBytes = hexToByte(cipherText);
			Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE,secretkey,iv);
			byte[] plainByte= cipher.doFinal(cipherBytes);
			byte[] cipherMac = new byte[64];
			for (int i = 0; i<64; i++)
			{	cipherMac[i] = plainByte[i];	}
			byte[] plainText = new byte[plainByte.length-64];
			for (int i = 0; i<(plainByte.length-64) ; i++)
			{	plainText[i] = plainByte[i+64];	}
			String plainString = new String(plainText);
			System.out.print(plainString);
			
			byte[] plainMac = mac.doFinal(plainText);			
			boolean checkMac = true;
			for(int i=0;i<64;i++)
			{	if( cipherMac[i] != plainMac[i])
				{	checkMac = false;	}
			}
			if(checkMac)
			{	System.out.print("----Message Passed----");	}
			else
			{	System.out.print("----Message Corrupt---");	}
		}		
	}
	public static String byteToHex(byte[] bytetemp) 
	{	String hexString="";
		String stringTemp="";
		int bytelength = bytetemp.length;
		for (int i=0;i< bytelength;i++)
		{	stringTemp= Integer.toHexString(bytetemp[i] & 0xFF);
		    if (stringTemp.length()==1)
		    {	hexString=hexString+"0"+stringTemp;	}
		    else 
			{	hexString=hexString+stringTemp;	}
		}
		return hexString.toUpperCase();
	 }
	public static byte[] hexToByte(String stringtemp)
	{	if(stringtemp == null)
		{	return null;	}
		int stringLength = stringtemp.length();
		if((stringLength % 2)==1)
		{	return null;	}
		byte[] tempByte = new byte[stringLength/2];
		for(int i=0;i < stringLength/2;i++)
		{	 tempByte[i] = (byte)Integer.parseInt(stringtemp.substring(i *2,i*2 +2),16);	}
		return tempByte;
	}
	public static byte[] setKey(byte[] bytetemp)
	{	byte[] byteKey = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
		for(int i = 0;(i <= bytetemp.length-1)&&(i < 24);i++)
		{	byteKey[i] = bytetemp[i];	}
		return byteKey;
	}	
}
