Otway-rees protocol
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<math.h>
struct KDC
{
int sender_key;
int receiver_key;
int session_key;
int random1;
int random2;
int ticket[3];
int ticket1[3];
}KDC;
struct A
{
int ticket[3];
int sender_key;
int session_key;
int got_KDC;
int msg4;
int random1;
int random2;
}A;
struct B
{
int ticket[3];
int receiver_key;
int received_KDC;
int session_key;
int random1;
int random2;
int msg3;
}B;
void main()
{
int i;
clrscr();
A.sender_key=accept_key_A();
KDC.sender_key=A.sender_key;//sender key given to kdc
B.receiver_key=accept_key_B();
KDC.receiver_key=B.receiver_key;//receiever key given to KDC
//message 1
A.random1=random_number();
B.random1=A.random1; //R
B.ticket[0]=A.random1 + A.sender_key;
B.ticket[1]=A.random2+A.sender_key;
//reply to kdc by bob
KDC.ticket[0]=B.ticket[0];
KDC.ticket[0]=KDC.ticket[0]-KDC.sender_key;
KDC.ticket[1]=B.ticket[1];
//now Bob generate random number
B.random2=random_number();
KDC.ticket1[0]=B.random1+B.receiver_key;
KDC.ticket1[1]=B.random2+B.receiver_key;
//now KDC decrypt the message
KDC.session_key=random_number();
B.received_KDC=KDC.session_key+KDC.receiver_key;//1
KDC.random2=KDC.ticket1[1]-KDC.receiver_key;
B.session_key=B.received_KDC-B.receiver_key;
B.msg3=KDC.session_key+KDC.random2;//b got challenge back ks+Rb 2
if(B.random2==B.msg3-B.session_key)
{
sound(100);
delay(500);
printf("\n Session key Accepted by Bob");
nosound();
}
else
{
printf("\n Transmission aborted due to intruders");
sound(100);
delay(500);
nosound();
goto exit;
}
printf("\n Now KDC send session Key to Alice");
sound(100);
delay(500);
nosound();
A.got_KDC=KDC.sender_key+KDC.session_key;
A.session_key=A.got_KDC-A.sender_key;
A.msg4=KDC.session_key+KDC.ticket[0];
if(A.random1==A.msg4-A.session_key)
{
sound(100);
delay(500);
printf("\n Alice has accepted the session key.");
printf("\n Alice and Bob started the communication with session key = %d",A.session_key);
printf(" & %d as NONCE.",random_number()+1);
nosound();
}
else
{
sound(100);
delay(500);
printf("\n Transmission aborted.");
nosound();
goto exit;
}
exit:
getch();
}
int accept_key_A()
{
int b;
do
{
printf("\n enter the private key of A (1-15):: ");
scanf("%d",&b);
if(b>15)
printf("\n Enter the key in prescribed range");
}while(b>16);
return (b);
}
int accept_key_B()
{
int b;
do
{
printf("\n enter the private key of B (1-15):: ");
scanf("%d",&b);
if(b>15)
printf("\n Enter the key in prescribed range");
}while(b>16);
return (b);
}
int random_number()
{
int a;
a=rand()%10;
return (a);
}
OUTPUT:
enter the private key of A (1-15):: 4
enter the private key of B (1-15):: 3
Session key Accepted by Bob
Now KDC send session Key to Alice
Alice has accepted the session key.
Alice and Bob started the communication with session key = 2 & 1 as NONCE.