Google Answers Logo
View Question
 
Q: c program for changing passwords..need comments to understand ( No Answer,   4 Comments )
Question  
Subject: c program for changing passwords..need comments to understand
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $20.00
Posted: 18 Nov 2005 09:30 PST
Expires: 18 Dec 2005 09:30 PST
Question ID: 594759
I would like someone to comment this program for me so i can
understand best how it works and if i need to make changes.

#include <stdio.h>
#include <ctype.h>

char xlat[] = {
0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f,
0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72,
0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44
};

char pw_str1[] = "password 7 ";
char pw_str2[] = "enable-password 7 ";

char *pname;

cdecrypt(enc_pw, dec_pw)
char *enc_pw;
char *dec_pw;
{
unsigned int seed, i, val = 0;

if(strlen(enc_pw) & 1)
return(-1);

seed = (enc_pw[0] - '0') * 10 + enc_pw[1] - '0';

if (seed > 15 || !isdigit(enc_pw[0]) || !isdigit(enc_pw[1]))
return(-1);

for (i = 2 ; i <= strlen(enc_pw); i++) {
if(i !=2 && !(i & 1)) {
dec_pw[i / 2 - 2] = val ^ xlat[seed++];
val = 0;
}

val *= 16;

if(isdigit(enc_pw[i] = toupper(enc_pw[i]))) {
val += enc_pw[i] - '0';
continue;
}

if(enc_pw[i] >= 'A' && enc_pw[i] <= 'F') {
val += enc_pw[i] - 'A' + 10;
--More--(35%)
continue;
}

if(strlen(enc_pw) != i)
return(-1);
}

dec_pw[++i / 2] = 0;

return(0);
}

usage()
{
fprintf(stdout, "Usage: %s -p <encrypted password>\n", pname);
fprintf(stdout, " %s <router config file> <output file>\n", pname);

return(0);
}

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

{
FILE *in = stdin, *out = stdout;
char line[257];
char passwd[65];
unsigned int i, pw_pos;

pname = argv[0];

if(argc > 1)
{
if(argc > 3) {
usage();
exit(1);
}

if(argv[1][0] == '-')
{
switch(argv[1][1]) {
case 'h':
--More--(57%)
usage();
break;

case 'p':
if(cdecrypt(argv[2], passwd)) {
fprintf(stderr, "Error.\n");
exit(1);
}
fprintf(stdout, "password: %s\n", passwd);
break;

default:
fprintf(stderr, "%s: unknow option.", pname);
}

return(0);
}

if((in = fopen(argv[1], "rt")) == NULL)
exit(1);
if(argc > 2)
if((out = fopen(argv[2], "wt")) == NULL)
exit(1);
}

while(1) {
for(i = 0; i < 256; i++) {
if((line[i] = fgetc(in)) == EOF) {
if(i)
break;

fclose(in);
fclose(out);
return(0);
}
if(line[i] == '\r')
i--;

if(line[i] == '\n')
break;
}
pw_pos = 0;
line[i] = 0;
--More--(81%)


if(!strncmp(line, pw_str1, strlen(pw_str1)))
pw_pos = strlen(pw_str1);

if(!strncmp(line, pw_str2, strlen(pw_str2)))
pw_pos = strlen(pw_str2);

if(!pw_pos) {
fprintf(stdout, "%s\n", line);
continue;
}

if(cdecrypt(&line[pw_pos], passwd)) {
fprintf(stderr, "Error.\n");
exit(1);
}
else {
if(pw_pos == strlen(pw_str1))
fprintf(out, "%s", pw_str1);
else
fprintf(out, "%s", pw_str2);

fprintf(out, "%s\n", passwd);
}
}
}
Answer  
There is no answer at this time.

Comments  
Subject: Re: c program for changing passwords..need comments to understand
From: loca1host-ga on 18 Nov 2005 23:04 PST
 
It gives me a headache just looking at it.
Why don't you just describe what it is you require it to do.
Because I dont think this program does it.
Subject: Re: c program for changing passwords..need comments to understand
From: srinifinance-ga on 22 Nov 2005 10:08 PST
 
Looks like it will take in a numeric string password like "101190" and
encrypt the password. expects the numeric value to start with a 0 or 1
and the second digit should be between 0-5. it only accepts even no of
digits and gives you an encrypted password. the cdecript should
actually be a cencrypt function since it is converting from password
to encrypted binary. however the decrypt that does the opposite is
missing in the program.
Subject: Re: c program for changing passwords..need comments to understand
From: derekwtp-ga on 22 Nov 2005 18:35 PST
 
Well I thought it was a password change utility program, however I
could not figure out how it worked.
Subject: Re: c program for changing passwords..need comments to understand
From: srinifinance-ga on 24 Nov 2005 18:18 PST
 
// test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

char xlat[] = 
{
	0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f,
	0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72,
	0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44
};

char pw_str1[] = "password 7 ";
char pw_str2[] = "enable-password 7 ";

char *pname;
/***************************************
* Function Name : cdecrypt
* Desc			: take a password in plain text and encrypt it
*				  the password is acceptable only if it meets the following conditions
*				     contains even no of chars/digits
*				     first digit is 0 or 1
*				     second digit is 0-5
*					 the remaining chars are 0-9,A-F
*				  the encrypted password will be
*				  dec_pw[0] = (Hex(enc[2]0-9,A-F)*16+enc[3] ) ^ xlat[seed++]
*				  dec_pw[1] = (Hex(enc[4]0-9,A-F)*16+enc[5] ) ^ xlat[seed++]
*				  dec_pw[2] = (Hex(enc[6]0-9,A-F)*16+enc[7] ) ^ xlat[seed++]
*				  dec_pw[3] = (Hex(enc[8]0-9,A-F)*16+enc[9] ) ^ xlat[seed++]
*				  dec_pw[4] = (Hex(enc[9]0-9,A-F)*16+enc[10] ) ^ xlat[seed++]
*				  ..............
* Arguments		: enc_pw(plain text password) is passed to function, 
*				  dec_pw encrypted password returned back
* return		: int -1 if error
****************************************/
int cdecrypt(char *enc_pw, char *dec_pw)
{
	unsigned int seed, i, val = 0;

	// if the password has odd no of characters then
	// return error
	if(strlen(enc_pw) & 1)
		return(-1);

	seed = (enc_pw[0] - '0') * 10 + enc_pw[1] - '0';

	// make sure the first digit is 0 or 1
	// make sure the second digit is between 0 and 5 
	if (seed > 15 || !isdigit(enc_pw[0]) || !isdigit(enc_pw[1]))
		return(-1);

	// Process all the characters passed except the first 2 digits
	for (i = 2 ; i <= strlen(enc_pw); i++) 
	{
		// do if i = 4,6,8,10,12........
		if(i !=2 && !(i & 1)) 
		{
			//  convert the previous 2 digits into 1 digit in 
			//  resulting encrypted password
			dec_pw[i / 2 - 2] = val ^ xlat[seed++];
			val = 0;
		}

		// if i = 2,4,6,8,10 do nothing
		// if i=  3,5,7,9,11 multiply with 16 before adding the digit
		val *= 16;

		// if the character is between 0-9
		if(isdigit(enc_pw[i] = toupper(enc_pw[i]))) 
		{
			val += enc_pw[i] - '0';
			continue;
		}

		// if the character is between A-F convert value to 10-15
		if(enc_pw[i] >= 'A' && enc_pw[i] <= 'F') 
		{
			val += enc_pw[i] - 'A' + 10;
			continue;
		}

		// if the digit is not between 0-9,a-f return with error
		if(strlen(enc_pw) != i)
			return(-1);
	}

	// BUG here, Should be not be dec_pw[++i / 2 ] = 0;
	dec_pw[++i / 2  - 2] = 0;
	return(0);
}


/***************************************
* Function Name : usage
* Desc			: usage function
* Arguments		: none
* return		: int 0
****************************************/
int usage()
{
	fprintf(stdout, "Usage: %s -p <encrypted password>\n", pname);
	fprintf(stdout, " %s <router config file> <output file>\n", pname);
	return(0);
}

/***************************************
* Function Name : main
* Desc			: main function
* Arguments		: argc no of args
*				  command line arguments call the program like 
*	with arguments			test -p 0120AB
*   without arguments		test
*                           password 7 0120AB
*							enable-password 7 0120AB
* return		: int -1 if error
****************************************/
int main(int argc,char **argv)
{
	FILE *in = stdin, *out = stdout;
	char line[257];
	char passwd[65];
	unsigned int i, pw_pos;

	pname = argv[0];

	// if any more than 1 arguments are passed to the program
	// should do the required action on passed arguments
	if(argc > 1)
	{
		if(argc > 3) 
		{
			usage();
			exit(1);
		}


		// usage has - option
		if(argv[1][0] == '-')
		{
			switch(argv[1][1]) 
			{
				// usage wanted help on using the program
				case 'h':
					usage();
					break;

				// encrypt the password and return the encrypted password
				case 'p':
					if(cdecrypt(argv[2], passwd)) 
					{
						fprintf(stderr, "Error.\n");
						exit(1);
					}
					fprintf(stdout, "\npassword: %s\n", passwd);
					break;

				default:
					fprintf(stderr, "%s: unknow option.", pname);
			}
			return(0);
		}

		if((in = fopen(argv[1], "rt")) == NULL)
			exit(1);
		if(argc > 2)
			if((out = fopen(argv[2], "wt")) == NULL)
		exit(1);
	}

	// the user did not pass any arguments 
	while(1) 
	{
		// take in 256 commands from the program and show the 
		// encrypted passwords
		for(i = 0; i < 256; i++) 
		{
			if((line[i] = fgetc(in)) == EOF) 
			{
				if(i)
					break;

				fclose(in);
				fclose(out);
				return(0);
			}
			if(line[i] == '\r')
				i--;

			if(line[i] == '\n')
				break;
		}
		pw_pos = 0;
		line[i] = 0;

		// get the line from command line
		// if it is of the form "password 7 0111AB"
		if(!strncmp(line, pw_str1, strlen(pw_str1)))
			pw_pos = strlen(pw_str1);

		// get the line from command line
		// if it is of the form "enable-password 7 0111AB"
		if(!strncmp(line, pw_str2, strlen(pw_str2)))
			pw_pos = strlen(pw_str2);

		// if the commands "password 7 " "enable-password 7 " are not found
		// flag error and get the next command
		if(!pw_pos) 
		{
			fprintf(stdout, "%s\n", line);
			continue;
		}

		// call the encrypt password 
		if(cdecrypt(&line[pw_pos], passwd)) 
		{
			// flag error if error
			fprintf(stderr, "Error.\n");
			exit(1);
		}
		else 
		{
			//if the command is password 7 passwordstring
			if(pw_pos == strlen(pw_str1))
				fprintf(out, "\n%s", pw_str1);
			else // enable-password 7 passwordstring
				fprintf(out, "\n%s", pw_str2);

			fprintf(out, "%s\n", passwd);
		}
	}
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy