need help i'm writing a small program in c and want to know how to make it password protected

Asked by nichols garrett

well i'm writing a small (really small) program in c and i'm trying to declare a password within it so that if the password isn't known it won't run the program how do i write a password into it?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
nichols garrett
Solved:
Last query:
Last reply:
Revision history for this message
Nigel Babu (nigelbabu) said :
#1

The best thing you can do is first write a password to a file and then read it from the file. That way you can change the value of the password. Please keep in mind to obfuscate the password. Use this link to see more information about handling files in C, http://c-programming.suite101.com/article.cfm/c_tutorial_file_handling_commands

Revision history for this message
nichols garrett (nichols-garrett) said :
#2

can you explain the detailed method to me because i'm extremely new to this plus i'm only 16

Revision history for this message
Nigel Babu (nigelbabu) said :
#3

If I give you an example program, would that solve your doubt?

Revision history for this message
nichols garrett (nichols-garrett) said :
#4

it might so far i've based my entire program off of several programs already written so it can't hurt do you need my program?

Revision history for this message
nichols garrett (nichols-garrett) said :
#5

 here's my program

 #include <stdio.h>

int main()
{
  char cmv540;
  printf( "declare password first\n");
  gets(cmv540);

  if(!strcmp(cmv540))
  printf("I'm alive! Beware\n");
  else
  printf("incorrect password\n");

  return 0;
}

Revision history for this message
Nigel Babu (nigelbabu) said :
#6

I dont this would work, coz cmv540 is only declared, but not defined. When you compare strings, you need 2 strings, one original and one to compare with. Gimme a day, I'll write a program and you can check it out

Revision history for this message
nichols garrett (nichols-garrett) said :
#7

ok thank you for your help

Revision history for this message
nichols garrett (nichols-garrett) said :
#8

i think i got it down thanks for your help anyway though here's my new program...

#include <stdio.h>

int mult ( int x, int y );

int main()
{
  int password;
  int x;
  int y;

  printf( "please enter the password: \n" );
  scanf( "%d", &password);
  if(password==1211) {
  printf( "please enter two numbers to be multiplied:\n");
  scanf( "%d", &x );
  scanf( "%d", &y );
  printf( "The product of your two numbers is %d\n", mult( x, y ) );
  getchar();
}
  else{
  printf("wrong password get the hell off my calculator\n");}
}
int mult (int x, int y)
{
  return x * y;
}