Saturday, May 8, 2010

NIS MD5

/* Implementation of MD5 */

#include
#include
#include
#include
#include

unsigned long leftrotate(unsigned long,int);
double myabs(double x);

int main()
{
clrscr();
char tempchar;
int m,h;
char fileName[75];
char message[1000];
FILE *fp;
char ch;
unsigned long temp;
unsigned long w[20];
int n;
unsigned long sineInt[64];
int i;

int r[64]={7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21};
for(i=0;i<64;i++)
{
sineInt[i]=(unsigned long)floor(myabs(sin(i+1)*pow(2,32)));
}
//printf("\nEnter name of the file containing message:");
fp=fopen("c:\\message.txt","r");

i=0;
while(!feof(fp))
{
ch=fgetc(fp);
message[i]=ch;
i++;
}
message[i-1]=0x80;
message[i]='\0';
int no_of_bits=strlen(message)*8;
int message_length=no_of_bits-8;
unsigned long msg_length=message_length;

int bits_to_pad=((512-(no_of_bits%512))+448)%512;

int j=0;
for (j=0;j{
message[i+j]=0x00;
}

message[i+j]='\0';
i=i+j;

unsigned char *ptrchar;
ptrchar= (unsigned char *)(&msg_length);
message[i]=ptrchar[0];
message[i+1]=ptrchar[1];
message[i+2]=ptrchar[2];
message[i+3]=ptrchar[3];
message[i+4]=0x00;
message[i+5]=0x00;
message[i+6]=0x00;
message[i+7]=0x00;

i=i+8;

unsigned long A,B,C,D;

A=0x67452301;
B=0xefcdab89;
C=0x98badcfe;
D=0x10325476;

unsigned long a,b,c,d,f,g;

char message_chunk[70];
int no_of_chunks=(i*8)/512;
int k;
for(j=0;j{
for(k=0;k<64;k++)
{
message_chunk[k]=message[(j*64)+k];
}

for(n=0;n<16;n++)
{
w[n]=*((unsigned long *)(&message_chunk[n*4]));
}


a=A;
b=B;
c=C;
d=D;

for(k=0;k<64;k++)
{
if (k>=0 && k<=15) //round one
{
f=(b & c) | ((~b) & d);
g=k;
}
else if(k>=16 && k<=31) //round two
{
f=(b & d) | (c & (~d));
g=(5*k+1) % 16;
}
else if(k>=32 && k<=47) //round three
{
f=b^c^d;
g=(3*k+5)%16;
}
else if(k>=48 && k<=63) //round four
{
f=c ^ (b | (~d));
g=(7*k)%16;
}
temp=d;
d=c;
c=b;
b=b+leftrotate((a+f+sineInt[k]+w[g]),r[k]);
a=temp;

}
A=A+a;
B=B+b;
C=C+c;
D=D+d;
}

printf("\nThe MD5 hash is:");
ptrchar=(unsigned char *)(&A);
for (h=0;h<4;h++)
printf("%0.2X",ptrchar[h]);
cout<<" ";

ptrchar=(unsigned char *)(&B);
for (h=0;h<4;h++)
printf("%0.2X",ptrchar[h]);
cout<<" ";

ptrchar=(unsigned char *)(&C);
for (h=0;h<4;h++)
printf("%0.2X",ptrchar[h]);
cout<<" ";

ptrchar=(unsigned char *)(&D);
for (h=0;h<4;h++)
printf("%0.2X",ptrchar[h]);

getch();
return 0;
}



//This function circularly left-shifts 'x' by 'c' bits
unsigned long leftrotate(unsigned long x,int c)
{
return ((x<>(32-c)));
}
double myabs(double x)
{
if (x<0)
return -1*x;
else
return x;
}


/*
OUTPUT

Enter name of the file containing message:abc.txt
The message is:The quick brown fox jumps over the lazy dog.
The MD5 hash is:E4D909C2 90D0FB1C A068FFAD DF22CBD0

Enter name of the file containing message:abc.txt
The message is:The quick brown fox jumps over the lazy dog
The MD5 hash is:9E107D9D 372BB682 6BD81D35 42A419D6
*/

NIS RSA Algorithm

#include
#include
#include

void main()
{
clrscr();
unsigned long p,q,n,m,M,e,d,i,C,M1;
cout<<"Enter value of P : ";
cin>>p;
cout<<"Enter value of Q : ";
cin>>q;
n=p*q;
m=(p-1)*(q-1);
for(e=1;e {
if(m%e!=0)
break;
}
d=0;
do
{
d++;
i=(((e*d)-1)%m);
}while(i!=0);
cout<<"e : "< cout<<"d : "< cout<<"Enter value of M : ";
cin>>M;
C=pow(M,e);
C=C%n;
cout<<"Encryption C : "<
M1=pow(C,d);
M1=M1%n;
cout<<"Decryption M1 : "<
getch();
}

NIS Port Scanning

import java.net.*;
import java.io.*;
class Port
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String host;
int start,end,avail,unavail,i;
Socket sk;
avail=0;
unavail=0;
System.out.println("Enter the host name : ");
host=br.readLine();
System.out.println("Enter the starting port no : ");
start=Integer.parseInt(br.readLine());
System.out.println("Enter the ending port no : ");
end=Integer.parseInt(br.readLine());
for(i=start;i<=end;i++)
{
System.out.print("Port No. " + i);
try
{
sk=new Socket(host,i);
System.out.println(" : Available");
avail++;
}
catch(Exception E)
{
System.out.println(" : Unavailable");
unavail++;
}
}
System.out.println("Total available port : " + avail);
System.out.println("Total unavailable port : " + unavail);
}
}

NIS Host-IP & Vice versa

import java.io.*;
import java.net.*;

class Host
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress in_obj;
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//get name of local computer & initialise the object
in_obj=InetAddress.getLocalHost();
System.out.println("getLocalHost : " + in_obj);
System.out.println();
//get name of local computer
str=in_obj.getHostName();
System.out.println("getHostName : " + str);
System.out.println();
//get canonical host name [local computer]
str=in_obj.getCanonicalHostName();
System.out.println("getCanonicalHostName : " + str);
System.out.println();
//get Host address [local computer]
str=in_obj.getHostAddress();
System.out.println("getHostAddress : " + str);
System.out.println();
//get remote pc info
in_obj=InetAddress.getByName("localhost");
System.out.println(in_obj);
}
}

NIS One time padding

Code :
#include
#include
#include
void main(int argc,char *argv[])
{
FILE *ip, *key, *op;
unsigned char ic, kc, oc;
clrscr();
if(argc!=4)
{
printf("Not valid parameter list...");
}
else
{
fseek(ip,SEEK_SET,0);
ip=fopen(argv[1],"r");
key=fopen(argv[2],"r");
op=fopen(argv[3],"w");
if( (ip==NULL) || (key==NULL) || (op==NULL) )
{
printf("File opening error...");
}
else
{
fread(&ic, sizeof(unsigned char), 1, ip);
do
{
fread(&kc, sizeof(unsigned char), 1, key);
oc=ic ^ kc;
printf("%c",ic);
fwrite(&oc, sizeof(unsigned char), 1, op);
}while(fread(&ic, sizeof(unsigned char), 1, ip));
}
fclose(op);
fclose(key);
fclose(ip);
}
getch();
}

Key File
QWERTYUIOPASDFGHJKLZXCVBNM

NIS Diffe Helman Program

#include
#include
#include

long double mod (long double q,long n)
{
long double rem=0, quo;
quo=q/n;
quo=floor(quo);
rem=q-(quo*n);
return rem;
}

int check_root(long int q,long int a)
{
long double arr[100];
int i,j,k=0,flag=1;
for(i=1;i {
arr[k]=mod(pow(a,i),q);
k++;
}
for(i=0;i {
for(j=0;j {
if ((i!=j) && (arr[i]==arr[j]))
{
flag=0;
break;
}
}
}
return flag;
}
void main()
{
clrscr();
long q,prem_root=2;
long p_A,k_A,p_B,k_B,pub_A,pub_B;
cout<<"Enter prime no : ";
cin>>q;
do
{
prem_root++;
}while(check_root(q,prem_root));

cout<<"Enter private key of A : ";
cin>>p_A;
pub_A=mod(pow(prem_root,p_A),q);

cout<<"Enter private key of B : ";
cin>>p_B;
pub_B=mod(pow(prem_root,p_B),q);

k_A=mod(pow(pub_B,p_A),q);
k_B=mod(pow(pub_A,p_B),q);

cout<<"Public key A : "< cout<<"Public key B : "< if(k_A==k_B)
{
cout<<"Key exchange successful...";
}
else
{
cout<<"Key exchange unsuccessful...";
}
getch();
}

Sunday, January 31, 2010

Invitation to join Amulyam-Free Prepaid Mobile Recharge and Movie Tickets

Invitation to join Amulyam-Free Prepaid Mobile Recharge and Movie Tickets

Happy to invite you to join Amulyam,that gives considerable amount of free prepaid mobile recharge and
free movie tickets immediately for doing what you normally do online:creating accounts on
social networking websites, photo sharing websites,video sharing websites,buying train tickets..etc.

Create your amulyam account and refer your friends to get more prepaid mobile recharge and movie tickets for free.

You can copy and paste below link to the address bar of your browser
or Click Here to create your account on amulyam.


http://www.amulyam.in/register.do?id=07d6101f-4dd1-4065-96a4-7ef021426cb9&sid=1


Viraj Kulkarni