Welcome to my blog, enjoy reading.

Monday, March 1, 2010

Program for Prime numbers

1.

main()
{
int a,c=0,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
for(i=1;i<=n;i++)
   {
     a=n%i;
     if(a=0)
        {
           c=c+1;
        }
    }
if (c=2)
  { printf("the given number is prime"); }
else
    printf("the given number is not prime");
}


2.

main()
{
int i,j=2,ch=0;
clrscr();
       printf("\nENTER ANY NUMBER");
       scanf("%d",&i);
  while(j<=i/2)
       {
        if(i%j==0)
          {
            printf("%d IS NOT PRIME",i);
            ch=1;
            break;
          }
        else
          {
           j++;
          }
      }
 if(ch==0)
   {
   printf("%d IS PRIME",i);
   }
}

Fibonacci program using C

This program prints the Fibonacci series

#include
#include

void main(void)
{
    int i,j,k,n;
    clrscr();
    i=0;
    j=1;
    printf("%d    %d    ",i,j);
    for(n=0;n<=5;n++)
    {
    k=i+j;
    i=j;
    j=k;
    printf("%d    ",k);
    }
    getch();
}

RSA algorithm using C

/* C program for the Implementation Of RSA
Algorithm */

#include< stdio.h>
#include< conio.h>

int phi,M,n,e,d,C,FLAG;

int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}

void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}

void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

/*************** OUTPUT *****************

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}
Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

Decrypted keyword : 19 */

Making a small password/login form in C

Well, here's a very simple program to write the entries into a file, using the unix/linux/bsd crypt() function to encrypt the password and getpass() function to get the password unix style (no keyboard echo, at all):



#include
#include
#include
#include

int main(int argc, char **argv){

char salt[2]; /* Salt for the crypt() function */
const char *salt_chars = "abcdefghijklmnopqrstuvwxyz" /* Range of character supported */
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* as a value for salt in crypt() */
"0123456789";

char username[BUFSIZ], password1[BUFSIZ], /* Buffers for user input and comparison */
password2[BUFSIZ], *buf;
char filename[BUFSIZ]; /* Buffer for filename */
FILE *outfile; /* File handle */

/* Build salt */
srand(time(NULL));
salt[0] = salt_chars[rand() % 62];
salt[1] = salt_chars[rand() % 62];

/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);

/* Get the username */
printf("Username: ");
scanf("%s", username);

do {

/* Get the password */
buf = getpass("Password: ");

/* Copy to a "stable" pointer */
sprintf(password1, "%s", buf);

/* Get the password */
buf = getpass("Enter Again: ");

/* Copy to a "stable" pointer */
sprintf(password2, "%s", buf);

/* See if the passwords are the same */
if(strcmp(password1, password2) != 0)
printf("\nPasswords do not match!\nTry again.\n\n");

} while(strcmp(password1, password2) != 0);

/* Encrypt the password */
buf = crypt(password1, salt);

/* Open the output file for append */
if((outfile = fopen(filename, "a+")) == NULL){

printf("\nFile error!\nAborting...\n");

} else {

/* Print the record to the file */
fprintf(outfile, "%s:%s\n", username, buf);

} /* End if */

/* Close the file */
fclose(outfile);

} /* End main() */



And here's how you would check it:



#include
#include
#include
#include

int main(int argc, char **argv){

char username[BUFSIZ], *password; /* User input buffers */
char buf[BUFSIZ]; /* File input buffer */
char *user_file, *pass_file; /* Buffers for values in file */
char filename[BUFSIZ]; /* Filename buffer */
FILE *infile; /* File handle */

/* Get the filename */
printf("Enter Password Filename: ");
scanf("%s", filename);

/* Get the username from the user */
printf("Username: ");
scanf("%s", username);

/* Get the password from the user */
password = getpass("Password: ");

/* Open the file */
if((infile = fopen(filename, "r")) == NULL){

printf("\nFile error!\nAborting...\n");

} else {

/* Loop throught the file */
while (!feof(infile)) {

/* Initialize with empty string */
buf[0] = '\0';

/* Read in one line */
fscanf(infile, "%s", buf);

/* If it's an empty line, continue */
if(strlen(buf) == 0) continue;

/* Point to the buffer */
user_file = buf;

/* Point to the delimiter in the buffer */
pass_file = strchr(buf, ':');

/* Change the delimiter to a nul character */
pass_file[0] = '\0';

/* Move to the next character */
pass_file++;

/* See if this matches the name the user entered */
if(strcmp(user_file, username) == 0){

/* See if the passwords match */
if(strcmp(crypt(password, pass_file), pass_file) == 0){

printf("Correct password...\n");

} else {

printf("Invalid password!\n\n");

} /* End if */

/* We found what we wanted; get out of loop */
break;

} /* End if */

} /* End while */

} /* End if */

/* Close the file */
fclose(infile);

} /* End main() */



That's basically all there is to it. I used the most basic form--there are better ways (using a variety of encryption algorithms--like ones using stronger encryption than 56 bit, like this one does). Keep in mind, you have to compile in the crypt shared library--like this using gcc:

gcc -lcrypt -o program_name program_name.c

Otherwise, it'll blow up when you run it (coredump).