// 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);
}
}
} |