* ==========================================================================
* DES and 3DES Encryption and Decryption
* Description: 
* If DES_RSA library is loaded, the DES, 3DES and RSA can be implemented by the following steps:
*  (1) creat a internal data file (supper password is needed)
*  (2) write DES, RSA key (supper password is needed)
*  (3) encrypt/decrypt based on file ID and key ID. 
*	when encrypt, the length of data should be A*8, where A is an integer, otherwise the data will be filled automatically.
* 	when decrypt, please cut off the corresponding data.
* Note:
* (1)DES Key is 8 bytes, and 3DES key is 16 bytes
* (2)The struct for DES and 3DES key
*	DES Key struct (10 bytes): key ID (1 byte)+key length(1 byte, value is 8)+key(8 bytes)
*	3DES Key struct (18 bytes): key ID (1 byte)+key length(1 byte, value is 16)+key(16 byte)
* ========================================================================== 


#include "Dic32_Vfp.h"
clear 
DECLARE integer   DIC_Find IN Dic32u.dll
DECLARE integer   DIC_Open IN Dic32u.dll integer hic, string @  reader_ROCKEY
DECLARE integer   DIC_Close  IN Dic32u.dll  integer   hic
DECLARE integer   DIC_Command  IN Dic32u.dll  integer  hic, integer cmd, string @  cmddata
DECLARE integer   DIC_Get  IN Dic32u.dll  string @xdata, integer  p1, integer   p2, string @buffer
DECLARE integer   DIC_Set  IN Dic32u.dll  string @xdata, integer  p1, integer  p2, integer   p3, string @buffer


store 0 to  errcode		   && error code 
store 0 to  num		   && number of founded card reader(s)
store 0 to  hic		   && handle of ROCKEY device.
store 0 to  i,j
store 0 to temp

store 8 to dataLen
dimension temp[8]


cmddata = space(1024)
buffer = space(1024)

	num = DIC_Find()
	for i=0 to num-1
		hic = DIC_Open(i, NULL)
		if (hic >= 0) 
		  exit  	&& Stop searching once one is found.
  	  endif
	endfor
	if (i == num) 
	     return
    endif

	* creat DES and TDES key files and write corresponding keys
	* file id 0x6F01
	DIC_Set(@cmddata, FILL, 256, 0, NULL)					&& clear buffer
	DIC_Set(@cmddata, FILE_ID, BY_VALUE, 0x6F01, NULL)			&& file id
	DIC_Set(@cmddata, FILE_CLASS, BY_VALUE, 0xff, NULL)			&& file class
	DIC_Set(@cmddata, FILE_ATTRIBUTE, BY_VALUE, FILEATTR_NORMAL, NULL)	&& file attr, internal
	DIC_Set(@cmddata, FILE_SIZE, BY_VALUE, 28, NULL)			&& file size
	DIC_Set(@cmddata, FILE_ROCKEY, BY_ARRAY, 0, "des")			&& file ROCKEY
	errcode = DIC_Command(hic, CREATE_FILE, @cmddata)



    && write 8 bytes DES key, note the data struct
    DIC_Set(@buffer,0,1,1,NULL)
    DIC_Set(@buffer,1,1,8,NULL)
    for i=0 to 7
    	DIC_Set(@buffer,2+i,1,i,NULL)
    endfor
    
	DIC_Set(@cmddata, WRITE_DATA, 10, 0, @buffer)
	errcode = DIC_Command(hic, WRITE_FILE, @cmddata)
	
    DIC_Set(@buffer,0,1,2,NULL)
    DIC_Set(@buffer,1,1,16,NULL)
    for i=0 to 15
    	DIC_Set(@buffer,2+i,1,i,NULL)
    endfor   
    && write 18 bytes 3DES key
	&& note the offset is 10 
	
	DIC_Set(@cmddata, WRITE_DATA, 18, 10, @buffer)
	errcode= DIC_Command(hic, WRITE_FILE, @cmddata)
	

&&================================des encrypt =8=========================================&&
	?"des: "
	for i=0 to 7
    	DIC_Set(@buffer,i,1,i,NULL)
    endfor   
	
	DIC_Set(@cmddata,DES_KEYFILEID,0,0x6F01,NULL)
	DIC_Set(@cmddata,DES_KEYINDEX,0,0x01,NULL)
	DIC_Set(@cmddata,DES_DATA,BY_ARRAY + dataLen,0,@buffer)
	errcode = DIC_Command(hic, DESENC, @cmddata)
	wRet = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"Cipher text: "
	for i=0 to 7
	   temp[i+1]=DIC_Get(@buffer,i,1,NULL)
    endfor
    ?temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8]
		
 &&====================================des decrypt =8===================================&&  
   		
	DIC_Set(@cmddata,DES_KEYFILEID,0,0x6F01,NULL)
	DIC_Set(@cmddata,DES_KEYINDEX,0,0x01,NULL)
	DIC_Set(@cmddata,DES_DATA,BY_ARRAY + dataLen,0,@buffer)
	errcode = DIC_Command(hic, DESDEC, @cmddata)
	wRet = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"Plant text: "
	for i=0 to 7
	   temp[i+1]=DIC_Get(@buffer,i,1,NULL)
    endfor
    ?temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8]

	&&================================3des encrypt =8=========================================&&
	?"3des: "
	for i=0 to 7
    	DIC_Set(@buffer,i,1,i,NULL)
    endfor   
	
	DIC_Set(@cmddata,DES_KEYFILEID,0,0x6F01,NULL)
	DIC_Set(@cmddata,DES_KEYINDEX,0,0x02,NULL)
	DIC_Set(@cmddata,DES_DATA,BY_ARRAY + dataLen,0,@buffer)
	errcode = DIC_Command(hic, DESENC, @cmddata)
	wRet = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"Cipher text: "
	for i=0 to 7
	   temp[i+1]=DIC_Get(@buffer,i,1,NULL)
    endfor
    ?temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8]
		
 &&====================================3des decrypt =8===================================&&  
   		
	DIC_Set(@cmddata,DES_KEYFILEID,0,0x6F01,NULL)
	DIC_Set(@cmddata,DES_KEYINDEX,0,0x02,NULL)
	DIC_Set(@cmddata,DES_DATA,BY_ARRAY + dataLen,0,@buffer)
	errcode = DIC_Command(hic, DESDEC, @cmddata)
	wRet = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"Plant text: "
	for i=0 to 7
	   temp[i+1]=DIC_Get(@buffer,i,1,NULL)
    endfor
    ?temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8]
	
	errcode = DIC_Close(hic) 
	
clear dlls
return 	