// des_3des.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "crypt2.h"
#include "stdio.h"
/*------------------------------------------------
Functionality implemented: Encrypt and decrypt with DES,3DES functions.
--------------------------------------------------*/
int main(int argc, char* argv[])
{
	int nRet = 0;
	int i = 0,j=0;
	char bIn[128],bOut[128],bKey[8],bKey3[16];

	
	printf("DES Encrypt.........\n");
	printf("\nPlease input plaintext(8 bytes):");
	scanf("%s",bIn);
	printf("\nPlease input the key (8 bytes):");
	scanf("%s",bKey);

	//Encrypt with DES function
	nRet = DES((unsigned char *)bKey,1,(unsigned char *)bOut,(unsigned char *)bIn,8);
	if(nRet != RE_SUCCESS)
		printf("\nerror! return: 0x%04x",nRet);
	else
	{
		printf("\nciphertext:\n");
		for(i = 0; i < 8; i++)
		{
			printf("%02x ", ((BYTE *)&bOut)[i]);
		}
		
		//Decrypt with DES function
		printf("\nDES Decrypt ........\n");
		nRet = DES((unsigned char *)bKey,0,(unsigned char *)bIn,(unsigned char *)bOut,8);
		if(nRet != RE_SUCCESS)
			printf("\nerror! return: 0x%04x",nRet);
		else
		{
			printf("\nplaintext:\n");
			for(i = 0; i < 8; i++)
			{
				printf("%02x ", ((BYTE *)&bIn)[i]);
			}
		}
	}

	printf("\n\n3DES Encrypt..............\n");
	printf("\nPlease input plaintext(8 bytes):");
	scanf("%s",bIn);
	printf("\nPlease input the key (16 bytes):");
	scanf("%s",bKey3);

	//Encrypt with 3DES function
	nRet = DES3((unsigned char *)bKey3,1,(unsigned char *)bOut,(unsigned char *)bIn,8);
	if(nRet != RE_SUCCESS)
		printf("\nerror! return: 0x%04x",nRet);
	else
	{
		printf("\nciphertext:\n");
		for(i = 0; i < 8; i++)
		{
			printf("%02x ", ((BYTE *)&bOut)[i]);
		}
		
		//Decrpt with 3DES function
		printf("\n3DES Decrypt ........\n");
		nRet = DES3((unsigned char *)bKey3,0,(unsigned char *)bIn,(unsigned char *)bOut,8);
		if(nRet != RE_SUCCESS)
			printf("\nerror! return: 0x%04x",nRet);
		else
		{
			printf("\nplaintext:\n");
			for(i = 0; i < 8; i++)
			{
				printf("%02x ", ((BYTE *)&bIn)[i]);
			}
		}
	}

	return 0;
}
