I have a password inside a file that's ciphered with the caesar cipher (you move the letter 3 spaces to the right, if you have 'a' it becomes a 'd', if you have a 'b' it becomes an 'e' and so on). I'm suppose to write a program that decodes the password inside the file and compare it with a password. To read the password you must first enter the name of the file.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char crypted[BUFSIZ];
char cleared[BUFSIZ];
int shift, i, a;
char *arch;
char pass[20];
arch=(char*)malloc(sizeof(char));
FILE *usua;
printf ("\n Ingrese su id de usuario(su rut sin puntos ni dígito verificador): ");
scanf ("%s", arch);
printf ("\n Ingrese su password : ");
scanf ("%s", pass);
a=strlen(pass);
pass[a]='\0';
usua=fopen(arch, "r");
fgets (crypted, BUFSIZ, usua);
shift = 3;
i=0;
while (crypted[i]!='\0')
{
if (crypted[i]==99)
{
crypted[i] =122;
cleared[i] = crypted[i] ;
i++;
}
if (crypted[i]==98)
{
crypted[i] =121;
cleared[i] = crypted[i] ;
i++;
}
if (crypted[i]==97)
{
crypted[i] =120;
cleared[i] = crypted[i] ;
i++;
}
else
{
cleared[i] = crypted[i] - shift;
i++;
}
}
cleared[i] = '\0';
printf("\nDesencriptado:\n%s", cleared);
i=0;
a=0;
while(pass[i]!='\0')
{
if (pass[i]==cleared[i])
{
i++;
}
else
{
a=1;
i++;
}
}
if (a==1)
{
printf ("\nClave Incorrecta");
}
else
{
printf ("\nClave Correcta");
}
return 0;
}
the file is named: 16486061
the password inside that file (after the decoding) is: fugazi
It decodes the password in the file, but when it comes to the comparison between the password in the file and the one the user enters it always throws the same result, no matter if the passwords match or not.