Hi guys....
These code snippets written by me.. can make a program written C to expire after certain number of executions. Check this out.
This program is to create a software that expires after 15 executions (I like coding such programs) the technique I used is first program here creates a Encrypted counter file named VALIDITY.DAT in your C drive that contains the counter but nobody can read or modify that.
I used 10 bit key for encryption here u can specify required number of executions and create the file
Code:
#include< stdio.h>
#include<conio.h>
void main()
{
int num_executions=15,decrypted=0,encrypted=0,i=0,digit=0;
int xor_bit[10];
int bin_num_executions[10];
int key[10]={'1','1','0','1','0','1','1','1','0','1'};
FILE *fp;
clrscr();
for(i=0;i<10;i++) //initialize array
{
bin_num_executions[i]=0;
xor_bit[i]=0;
}
//////////////////////////////////////////////////////
i=9;
do //converting to binary, the number of executions
{
digit=num_executions%2;
bin_num_executions[i]=digit;
i--;
num_executions=num_executions/2;
}while(num_executions!=0);
for(i=0;i<10;i++)
{
printf("%d",bin_num_executions[i]);
}
//////////////////////////////////////////////////////
//bitwise xoring,writing to file begins here
printf("\n");
for(i=0;i<10;i++)
{
bin_num_executions[i]=bin_num_executions[i]^key[i];
if(bin_num_executions[i]==48)
{
xor_bit[i]=0;
}
else
{
xor_bit[i]=1;
}
}//for loop ends here
fp=fopen("C:\\validity.dat","w"); //writing to a file
for(i=0;i<10;i++)
{
printf("%d",xor_bit[i]);
//fprintf(fp,"%d",xor_bit[i]);
}
fwrite(xor_bit,sizeof(int),10,fp);
fclose(fp);
// printf("\nInitial key %d\n",num_executions);
// printf("Encrypted %d",encrypted);
getch();
}
this code is for our software which reads the encrypted file, decrypts it and decreases the counter then updates file encrypted
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int validate()
{
int init_counter[10],cnt,n;
int i,k=9;
int product=1;
int bin_num_executions[10],xor_bit[10],digit;
int totl=0;
int total_copy=0;
int key[10]={'1','1','0','1','0','1','1','1','0','1'};
FILE *fp;
for(i=0;i<10;i++) //initializer
{
init_counter[i]=0;
bin_num_executions[i]=0;
xor_bit[i]=0;
}
fp=fopen("C:\\validity.dat","r");
fread(init_counter,sizeof(int),10,fp);
fclose(fp);
//decoding
for(i=0;i<10;i++)
{
init_counter[i]=init_counter[i]^key[i];
if(init_counter[i]==48)
{
init_counter[i]=0;
}
else
{
init_counter[i]=1;
}
}
//converting binary init_counter to decimal
for(i=0;i<10;i++)
{
totl=totl+(init_counter[i]*pow(2,k));
k--;
}
totl=totl-1; //reduce counter
total_copy=totl;
///////////////////////////////////////////////////////////////
//updating to the file
i=9;
do //converting to binary, the number of executions
{
digit=totl%2;
bin_num_executions[i]=digit;
i--;
totl=totl/2;
}while(totl!=0);
//////////////////////////////////////////////////////
//bitwise xoring,writing to file begins here
for(i=0;i<10;i++)
{
bin_num_executions[i]=bin_num_executions[i]^key[i];
if(bin_num_executions[i]==48)
{
xor_bit[i]=0;
}
else
{
xor_bit[i]=1;
}
}//for loop ends here
fp=fopen("C:\\validity.dat","w"); //writing to a file
fwrite(xor_bit,sizeof(int),10,fp);
fclose(fp);
return(total_copy);
}
////////////////////////////////////////////////////////////////////
void main()
{
int validity=validate();
clrscr();
if(validity==0)
{
printf("\nSoftware Expired\n");
getch();
exit();
}
if(!(validity>0&&validity<16))
{
printf("\nCurrupted associated file\n");
getch();
exit();
}
printf("\nYou have %d more executions\n",validity);
getch();
} // end of main
If u try to modify any file it gives error.....