* ==========================================================================
* RSA encryption/decryption
* Description: 
* If DES_RSA library is loaded, the DES, 3DES and RSA can be implemented by the following steps:
* (1) creat RSA key pair
* (2) encrypt/decrypt based on public key file ID or private key file ID
* Note:
* (1) the unit of key is bit, only support 1024 or 512 bits key
* (2) the default value of e is 65537, and cannot be changed
* (3) when encrypt data, if the data is longer than n, please set the high bit of data to 0
* ==========================================================================


#include "Dic32_Vfp.h"
clear 
DECLARE integer   DIC_Find IN Dic32u.dll
DECLARE integer   DIC_Open IN Dic32u.dll integer hic, string @  reader_name
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[128]


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

    * a. Change to super user status (Only super users are allowed to create executable files)
    * Super password is 8 FFs for demo ROCKEY
	for i=0 to 7
	    DIC_Set(@cmddata,i,1,0xff,NULL)
    endfor
	errcode = DIC_Command(hic, CHECK_SUPER_PASS, @cmddata) 


	* b. Format ROCKEY, all files and directorys will be cleared.
    *    You can set ATR and VOLUME here.
*	DIC_Set(@cmddata, DIC_VOLUME, BY_ARRAY, 0, "DICSYSTEM") 
*	DIC_Set(@cmddata, ATR, BY_ARRAY, 0, "DIC Co.") 
*	errcode = DIC_Command(hic, FORMAT_CARD, @cmddata) 

	*************************************
	** c. creat RSA key
	DIC_Set(@cmddata, RSA_PUBKEYID, BY_VALUE, 0x7f01, NULL) &&public key file ID
	DIC_Set(@cmddata, RSA_PUBKEYSIZE, BY_VALUE, 1024, NULL) &&public key size in bit
	DIC_Set(@cmddata, RSA_PIVKEYID, BY_VALUE, 0x7f02, NULL) &&private key file ID
	errcode = DIC_Command(hic, RSAGENKEY, @cmddata)
	
	* i. cancel supper password status
	errcode = DIC_Close(hic) 
	errcode = DIC_Open(hic, NULL) 
	
	*set plaintext
	for i = 0 to 127
		DIC_Set(@buffer,i,1,i,NULL)
	endfor
	
	*RSA encryption, either public key or private key
	DIC_Set(@cmddata, RSA_KEYFILEID, BY_VALUE, 0x7f01, NULL) &&use public key here
	DIC_Set(@cmddata, RSA_DATA, BY_ARRAY + 128, 0, buffer)
	errcode = DIC_Command(hic, RSAENC, @cmddata)
	
	wRetLen = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"RSA:"
	?"Cipher text:"
	for i=0 to 127
	   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],temp[9],temp[10]
	?temp[11],temp[12],temp[13],temp[14],temp[15],temp[16],temp[17],temp[18],temp[19],temp[20]
	?temp[21],temp[22],temp[23],temp[24],temp[25],temp[26],temp[27],temp[28],temp[29],temp[30]
	?temp[31],temp[32],temp[33],temp[34],temp[35],temp[36],temp[37],temp[38],temp[39],temp[40]
	?temp[31],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47],temp[48],temp[49],temp[50]
	?temp[51],temp[52],temp[53],temp[54],temp[55],temp[56],temp[57],temp[58],temp[59],temp[60]
	?temp[61],temp[62],temp[63],temp[64],temp[65],temp[66],temp[67],temp[68],temp[69],temp[70]
	?temp[71],temp[72],temp[73],temp[74],temp[75],temp[76],temp[77],temp[78],temp[79],temp[80]
	?temp[81],temp[82],temp[83],temp[84],temp[85],temp[86],temp[87],temp[88],temp[89],temp[90]
	?temp[91],temp[92],temp[93],temp[94],temp[95],temp[96],temp[97],temp[98],temp[99],temp[100]
	?temp[101],temp[102],temp[103],temp[104],temp[105],temp[106],temp[107],temp[108],temp[109],temp[110]
	?temp[111],temp[112],temp[113],temp[114],temp[115],temp[116],temp[117],temp[118],temp[119],temp[120]
	?temp[121],temp[122],temp[123],temp[124],temp[125],temp[126],temp[127],temp[128]
	
	*RSA decrypt
	DIC_Set(@cmddata, RSA_KEYFILEID, BY_VALUE, 0x7f02, NULL)
	DIC_Set(@cmddata, RSA_DATA, BY_ARRAY + 128, 0, @buffer)
	errcode = DIC_Command(hic, RSADEC, @cmddata)   
	wRetLen = DIC_Get(@cmddata, AFTER_ENCDEC_DATA, BY_ARRAY, @buffer)
	?"Plant text:"
	for i=0 to 127
	   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],temp[9],temp[10]
	?temp[11],temp[12],temp[13],temp[14],temp[15],temp[16],temp[17],temp[18],temp[19],temp[20]
	?temp[21],temp[22],temp[23],temp[24],temp[25],temp[26],temp[27],temp[28],temp[29],temp[30]
	?temp[31],temp[32],temp[33],temp[34],temp[35],temp[36],temp[37],temp[38],temp[39],temp[40]
	?temp[31],temp[42],temp[43],temp[44],temp[45],temp[46],temp[47],temp[48],temp[49],temp[50]
	?temp[51],temp[52],temp[53],temp[54],temp[55],temp[56],temp[57],temp[58],temp[59],temp[60]
	?temp[61],temp[62],temp[63],temp[64],temp[65],temp[66],temp[67],temp[68],temp[69],temp[70]
	?temp[71],temp[72],temp[73],temp[74],temp[75],temp[76],temp[77],temp[78],temp[79],temp[80]
	?temp[81],temp[82],temp[83],temp[84],temp[85],temp[86],temp[87],temp[88],temp[89],temp[90]
	?temp[91],temp[92],temp[93],temp[94],temp[95],temp[96],temp[97],temp[98],temp[99],temp[100]
	?temp[101],temp[102],temp[103],temp[104],temp[105],temp[106],temp[107],temp[108],temp[109],temp[110]
	?temp[111],temp[112],temp[113],temp[114],temp[115],temp[116],temp[117],temp[118],temp[119],temp[120]
	?temp[121],temp[122],temp[123],temp[124],temp[125],temp[126],temp[127],temp[128]
	
	errcode = DIC_Close(hic) 
	
clear dlls
return 	