14 February 2014

OTP Encryption in c++

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
void cih(char*,int[],int);
void main(){
clrscr();
char *plain,*key;
int i,plen,klen,otp1,count=0,rkey,rem[50],count2=0,count1=0;
//while(count<2){
cout<<"\nEnter Plain Text:";
cin>>plain;
//cout<<"\nEnter Key Text:";
//cin>>key;

plen=strlen(plain);
int val=1;
for(i=0;i<plen;i++){
val=val*10;
}
 randomize();
rkey=rand();
otp1=rkey;
cout<<"\nOne time password will be:"<<rkey;

while(rkey>=10){
rem[count1]=rkey%10;
count2=rem[count1];
rkey=rkey/10;
key[count1]=count2;
count1++;
}
int otp;

while(count<2){
cout<<"\nEnter OTP:";
cin>>otp;
if(otp!=otp1){
cout<<"\nOTP Enterred is different try again!!!!";
count=0;
}
if(otp==otp1){
count=2;
cih(plain,rem,plen);
getch();
}
}



/*getch();
exit(0);
} */
//}

getch();
}

void cih(char *pt,int key[],int val){
char *plain,*cyh;
plain=pt;
int pasc[50],kasc[50],i,xor[50],mul[50];
for(i=0;i<val;i++){
pasc[i]=plain[i];
kasc[i]=key[i];
xor[i]=pasc[i]^kasc[i];
cout<<pasc[i]<<"\t"<<kasc[i]<<"\t"<<xor[i]<<"\n";
mul[i]=xor[i]/26;
xor[i]=xor[i]%26;
xor[i]=xor[i]+97;

cyh[i]=xor[i];
}
cout<<"\nCipher Text Will be:  ";
for(i=0;i<val;i++){
cout<<cyh[i];       //printing cypher Text
}


cout<<"\nDecrypted Text will be:";
//mul[] stores the queotent
char *dcyh;
int dval[50];
for(i=0;i<val;i++){
dval[i]=26*mul[i]+xor[i]-97;  //generating original xor value
dval[i]=dval[i]^kasc[i];    //generating plain text ascii value
dcyh[i]=dval[i];                 //Getting original text character
cout<<dcyh[i];             //printing original text
}

}

0 comments:

Post a Comment

Thank you for comment. We will try to enhance the quality of this website.