Random Homebrew: Road Dog
Wi-fi sniffer/ scanner
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

Encryption issues

Forum rules
Any post not directly related to programming will be moderated.
Do not request people to code something for you.
Avoid posting messages that do not bring anything to the conversation. We want the threads in this subforum to stay focused.

Encryption issues

Postby dj51_Shura » Sat Sep 17, 2011 10:46 am

Hi! Now I have a problem with an encryption code, it's a very-simple function and works almost-fine, but, almost...

The function basically takes a char array (string) and read a character from the input file. Then, it adds the key's ascii code (at the proper position of the key) to the ascii code of the readed character, and puts it to the output file. The below code:

For encryption:
Code: Select all
int ioEncryptFile(const char * source, const char * dest, const char * mode, char key[]) {
   FILE *finput = fopen(source, "r"); // Open the source file in read mode
   if(finput == NULL) // error at opening input descriptor
      return false;
   
   FILE *foutput;
   if(strcmp(mode, OVW) == 0) {
      foutput = fopen(dest, "w"); // Open the output file in write/binary mode
   } else if(strcmp(mode, APP) == 0) {
      foutput = fopen(dest, "a"); // Open the output file in append/binary mode
   } else if((strcmp(mode, SAFETY) == 0) && (ioFileExists(dest) == true)) {
      return false;
   } else {
      fclose(finput);
      return NULL;
   }
   
   if(foutput == NULL) { // error at opening output descriptor
      fclose(finput);
      return false;
   }
   
   char c; /* To put the character readed */
   char u; /* The character to write, after applied the key */
   
   int keylen = strlen(key); /* array index */
   
   int Y = 0; // index for the *loop*
   while((feof(finput) == 0) && (ferror(finput) == 0) && (ferror(foutput) == 0)) {
      
      /* if the array index has reached the lenght of the key */
      if(Y == keylen) {
         Y = 0; /* reinitialize the index or counter */
      }
      
      /* read a character from the input */
      c = fgetc(finput);
      
      /* control expression */
      if(c != '\0') {
         u = c + key[Y];
      } else if (c == '\0') {
         u = '\0';
      }
      
      /* put out the resulted encrypted character */
      fputc(u, foutput);
      
      /* advance the index or counter */
      Y++;
   }
   
   /* Close file descriptors */
   fclose(finput);
   fclose(foutput);
   
   return true;
}


For decryption:
Code: Select all
int ioDecryptFile(const char * source, const char * dest, const char * mode, char key[]) {
   FILE *finput = fopen(source, "r"); // Open the source file in read mode
   if(finput == NULL) // error at opening input descriptor
      return false;
   
   FILE *foutput;
   if(strcmp(mode, OVW) == 0) {
      foutput = fopen(dest, "w"); // Open the output file in write/binary mode
   } else if(strcmp(mode, APP) == 0) {
      foutput = fopen(dest, "a"); // Open the output file in append/binary mode
   } else if((strcmp(mode, SAFETY) == 0) && (ioFileExists(dest) == true)) {
      return false;
   } else {
      fclose(finput);
      return NULL;
   }
   
   if(foutput == NULL) { // error at opening output descriptor
      fclose(finput);
      return false;
   }
   
   char c; // the character readed
   char u; // the character to write, result of '(char) cc + key'
   
   int keylen = strlen(key);
   
   int Y = 0; // index for the *loop*
   while((feof(finput) == 0) && (ferror(finput) == 0) && (ferror(foutput) == 0)) {
      
      /* if the array index has reached the lenght of the key */
      if(Y == keylen) {
         Y = 0; /* reinitialize the index or counter */
      }
      
      /* read a character from the input */
      c = fgetc(finput);
      
      /* control expression */
      if(c != '\0') {
         u = c - key[Y];
      } else {
         u = '\0';
      }
      
      /* put out the resulted encrypted character */
      fputc(u, foutput);
      
      /* advance the index or counter */
      Y++;
   }
   
   /* Close file descriptors */
   fclose(finput);
   fclose(foutput);
   
   return true;
}


The minimal macros like APP (append), OVW (overwrite) and SAFETY are defined in the same file as this functions (I'm working in my own library :P) and they're not relevant.

The problem is that the function keeps a non-expected-characters at the end of the output file. I think the problem is both of the encrypt/decrypt functions, because the encrypted file has 1 byte more than the orig file, and the decrypted version of that, has 1 byte more.

Original -> X bytes
Encrypted version -> X + 1 bytes
Decrypted version -> X + 2 bytes

I think at the '\0' (NULL character) but,... nah, I put a conditional instruction in the encryption engine and does nothing.

Can anyone help me?

Thanks for all,
best regards!
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com
dj51_Shura
 
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain

Re: Encryption issues

Postby m0skit0 » Sat Sep 17, 2011 1:19 pm

First, a remark if you allow me: your function should not accept file paths as parameters, either the mode. You're making it too specific for a library. What if you don't want to write the result to a file? You have to write a whole new function with almost the same code? Best thing to do in this case is to pass buffers to the function. This is more flexible, and allows more reuse of the function. This will also make the function way smaller and faster.

Code: Select all
int ioEncryptFile(const char* source, const char* dest, char key[])

source and dest being actually buffers and not file paths.

And about your problem :mrgreen: I think the bug is here:

Code: Select all
if(Y == keylen) {

Y starts at 0, so it should go from 0 to keylen - 1.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Encryption issues

Postby dj51_Shura » Sat Sep 17, 2011 1:55 pm

Thanks for your reply.

The buffers... mmm... great idea, but, about the memory... if I use a buffer, then we need to put all the file/what_we_need in memory. Both buffers (original and encrypted). I have also functions to encrypt and decrypt strings (xD), so,... but thanks ;)
Of course I can read X bytes from a file, encrypt it with strenc(source, key[]) and then put the result encrypted string (returned by strenc) to the file, and reverse to decrypt (strdec(source, key[])).

About the problem, it still exists. The output is the same. I will continue trying... but... >.<
I also think in a 'patch': open (create) a temporary file (concat(dest, ".temp")), and write the proper bytes. Well... my English is very, very bad, so I think to explain it with some code :D
Code: Select all
/* Get the size of the file in bytes */
int size = ioGetFileSize(orig);
/* Encrypt the file. It will produce an output file of (size + 1) bytes */
ioEncryptFile(orig, enc, blah blah blah...);
/* Copy just the bytes that contains the original file, with the advanced copy function */
ioCopyFileAdv(enc, temp, blah blah blah, size);
/* Overwrites the old-encrypted-file with the fixed version of that file */
ioCopyFile(temp, enc, overwriting...);


Of course, it's a noob patch, an idiot idea, and I don't test it with binary files, but, it should work (well, I have wrote the code here, so it can contain an error...), no?

Thanks a lot for your help ;-)
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com
dj51_Shura
 
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain

Re: Encryption issues

Postby m0skit0 » Sat Sep 17, 2011 4:49 pm

Ok. First, your output file should be binary because it's most likely you'll get non-printable characters. And second, fgetc and fputc get int as argument, and not char, so the problem might be there.

Also don't forget about the overflow. If for example you add 1 to 255 you'll end up with 0 (since char is only one byte long).
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Encryption issues

Postby dj51_Shura » Sat Sep 17, 2011 5:13 pm

I change the open mode and the output is, sadly, the same.

About the fgetc() and fputc(), yes,... now the char c and char u "buffers" are integers: int c and int u. Same output.

About the overflow, yes, but, if a put a conditional sentence (if( (c + key[Y]) > 255) { ... }) like in the example, then the decryption engine will fail, because (sorry for my bad English) it can't apply correctly the key.

I'm so confused... I'm now learning C# and making an Encryption/Decryption GUI, but, how? How can I do that if I can't encrypt/decrypt correctly in C? xD :x

Thanks!
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com
dj51_Shura
 
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain

Re: Encryption issues

Postby m0skit0 » Sun Sep 18, 2011 3:51 pm

I'll try to debug your code later, if no one helps before ;)

And making a GUI is not a big deal. And C# is way more friendly than C.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Encryption issues

Postby dj51_Shura » Sun Sep 18, 2011 4:00 pm

m0skit0 wrote:
And making a GUI is not a big deal. And C# is way more friendly than C.


Yes, I've seen. Monodevelop is like... Visual Basic? xD

I'm working now with GTK. It isn't 'easy', but not difficult... mmm...

Thanks for all.
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com
dj51_Shura
 
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain

Re: Encryption issues

Postby Stobby » Sun Sep 18, 2011 10:00 pm

I've read your code... maybe the issue lies in moment when EOF indicator is set.
I'm not so sure, but it could happen that the EOF stream indicator is set when you perform a fgetch on EOF character, therefore the function feof(finput) could return != 0 after the last valid byte is read leading to the execution of a supplementary loop during which fgetch would read EOF and, not being filtered by control expression, could be "encrypted" creating a bogus, writeable, character.

For example, let's assume that your file contains characters AB\0'EOF'

After having read \0, the file pointer would point to EOF position but, not having read yet another character with fgetc, it would not set the EOF indicator.
This would result in feof(finput) returning 0 and would lead to a new fgetc execution.
This time fgetc would return EOF (setting EOF indicator), but your encryption algorithm would encrypt its value leading to a new byte..

As a rule of thumb, I prefer checking for a EOF read rather than using feof(...).

To check if this is true, you could modify your code in this way (it will result reduntant but it could help you to debug your code):

Code: Select all
/* control expression */
if(c != '\0' && c !=EOF) {
         u = c + key[Y];
      } else {
         u = c;
      }

  /* put out the resulted encrypted character */
if(u!=EOF)
{
     fputc(u, foutput);
}



Let me know if it works.

Good luck!
Stobby
 
Posts: 21
Joined: Mon Aug 29, 2011 7:28 pm

Re: Encryption issues

Postby dj51_Shura » Mon Sep 19, 2011 7:28 pm

Thanks! I can't test it now, because I have started the... school... >.<

But I will test it at the weekend.

Thanks m0skit0 and Stobby!
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com
dj51_Shura
 
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests