Cookie Related Code in C++




#include <iostream>
#include <ctime> // used for time stamp
#include <cstring> // strcmp,strcat
#include <string> // not needed
#include <stdio.h> //fopen fclose, EOF, sprintf
using namespace std;
int main( )
{
cout << "\n==================This Code is for Cookies=================\n";
cout << "\n==============Fetching the User Info from file=============\n";
FILE *fp1;
char oneword[15] = "", stri[2][15] = {};
int ij=0;
   fp1 = fopen("test.dat","r");
while(fscanf(fp1,"%s",oneword) != EOF)
{
   strcpy(stri[ij],oneword);
   ij++;
}
     fclose(fp1);
     cout << "Value needed : " << stri[0] << endl;
//Data need to be used outside - stri[0]
cout << "\n====================Time Stamp Creation====================\n";
 char year[20]="",month[2]="",day[2]="";
 time_t now = time(0);
 tm *ltm = localtime(&now);
 sprintf(year,"%d",1900 + ltm->tm_year);
 sprintf(month,"%d",1 + ltm->tm_mon);
 sprintf(day,"%d",ltm->tm_mday);
 cout << year << "-" << month << "-"<< day << endl;
//Data need to be used outside - year, month, day
cout << "\n=========Concatinating the User Info with Time Stamp=======\n";
   char str[300]="";
 char str2[2]="@";
 strcat(str, stri[0]);
 strcat(str, str2);
 strcat(str, year);
 strcat(str, str2);
 strcat(str, month);
 strcat(str, str2);
 strcat(str, day);
 cout << "After Concatination Result is : " << str ;
//Data need to be used outside - str
cout << "\n================Encoding the String generated==============\n";
 char b[300]="";
 int i=0,j=0;
 while(str[i]!='\0')
 {
 b[i]=str[i]+2;
 i++;
 }
 cout << "Encrypted string is: " << b << endl;
//Data need to be used outside - b
cout << "\n====================Decoding the String====================\n";

//   sprintf(dest,"%s",Fet.c_str());
   char c[300]="";
 while(b[j]!='\0')
 {
 c[j]=b[j]-2;
 j++;
 }
 cout << "Decrypted string is: " << c << endl;
//Data need to be used outside - c
cout << "\n====================Parsing the String=====================\n";
 int g=0,h=0,m=0;
 char token[6][15] = {};
 while (c[g] != '\0')
   {
if(c[g] != '@')
 {
   token[m][h] = c[g];
 }
   else
     {
m=m+1;
h=-1;
     }
     g++;
     h++;
   }
 int z=0;
 while (z<4)
 {
   cout <<"String : "<< z <<" : "<< token[z]<<endl;
   z++;
 }
//Data need to be used outside - token[z]
cout << "\n==================Comparing the strings====================\n";
 if(strcmp(token[0],stri[0])==0)
 {
   cout << "User Details Matched" << endl ;
 }
 else
 {
   cout << token[0] << " : != : " << stri[0] << endl;
 }
//===================================
 if(strcmp(token[1],year)==0)
 {
   cout << "Year timestamp Matched" << endl;
 }
 else
 {
   cout << token[1] << " : != : "<< year << endl;
 }
//===================================
 if(strcmp(token[2],month)==0)
 {
   cout << "Month timestamp Matched" << endl;
 }
 else
 {
   cout << token[2] << " : != : "<< month << endl;
 }
//===================================
 if(strcmp(token[3],day)==0)
 {
   cout << "Day timestamp Matched" << endl;
 }
 else
 {
   cout << token[3] << " : != : "<< day << endl;
 }
 //
cout << "\n====================Completed the Work=====================\n";

return 0;

}

Comments