Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

What's wrong with my Encryption thingy??

Hi guys. I'm messing around with file encryption in C. I wrote a small program that will take a file, process it through 3 encryption algorithms and creates a new file with the encrypted output.

My problem is that after the string in the buffer goes through the first algorithm, it is not giving the right output I want.

Output:
PHP Code

<?php 
Thequickbrownfoxjumpsoverthelazydog
.   //buffer
a√L    //result of first algorithm
u√L
0╛g 

PHP Code

<?php 
#include<stdio.h>
#include<string.h>
#define XOR_KEY1 0x2B    //nice
#define XOR_KEY2 0x45    //golden rule


int main(int argc, char *argv[]) {

    
char buff[1024];
    
char placeHolder;
    
int len = strlen(buff);
    
int x;

    
FILE *sourceFile;
    
FILE *destFile;
    
sourceFile = fopen(argv[1], "r");
    
destFile = fopen("encryptedFile.txt", "a");


    while(
feof(sourceFile) == 0) {

        
fgets(buff, sizeof(buff), sourceFile);

        
puts(buff);                                                            //prints initial content of buffer

        
for(x = 0; buff[x] != '\0'; x++, len--) {                            //1st obfuscation
            
placeHolder = buff[len-1];
            
buff[len-1] = buff[x];
            
buff[x] = placeHolder;
            if(&
buff[x] == &buff[len-1] || &buff[x+1] == &buff[len-1]) {
                break;
            }
        }

        
puts(buff);                                                            //prints output of 1st

        
for(x = 0; buff[x] != '\0'; x++) {                                    //2nd obfuscation
            
switch(buff[x]) {
                case 
'a':
                    
buff[x] = 'u';
                    break;
                case 
'e':
                    
buff[x] = 'o';
                    break;
                case 
'o':
                    
buff[x] = 'e';
                    break;
                case 
'u':
                    
buff[x] = 'a';
                    break;
                case 
'A':
                    
buff[x] = 'U';
                    break;
                case 
'E':
                    
buff[x] = 'O';
                    break;
                case 
'O':
                    
buff[x] = 'E';
                    break;
                case 
'U':
                    
buff[x] = 'A';
                    break;
                default:
                    break;
            }
        }

        
puts(buff);                                                            //prints output of 2nd

        
for(x = 0; buff[x] != '\0'; x++) {                                    //3rd obfuscation
            
if(buff[x] == 'O') {
                continue;
            }                                
            if((
x % 2 == 0) && (x != 0)) {
                
buff[x] = buff[x] ^ XOR_KEY1;
                continue;
            }
            
buff[x] = buff[x] ^ XOR_KEY2;
        }

        
puts(buff);                                                            //prints output of 3rd

        
fprintf(destFile, "%s", buff);
    }

    
fclose(sourceFile);
    
fclose(destFile);

    return 
0;

} 

P.S. sorry for the messy code, still learning.
(Mar 3, 2020 04:08 AM)Consulting Detective Wrote:  Hi guys. I'm messing around with file encryption in C. I wrote a small program that will take a file, process it through 3 encryption algorithms and creates a new file with the encrypted output.

My problem is that after the string in the buffer goes through the first algorithm, it is not giving the right output I want.

Output:
PHP Code

<?php 
Thequickbrownfoxjumpsoverthelazydog
.   //buffer
a√L    //result of first algorithm
u√L
0╛g 

PHP Code

<?php 
#include<stdio.h>
#include<string.h>
#define XOR_KEY1 0x2B //nice
#define XOR_KEY2 0x45 //golden rule


int main(int argc, char *argv[]) {

 
char buff[1024];
 
char placeHolder;
 
int len = strlen(buff);
 
int x;

 
FILE *sourceFile;
 
FILE *destFile;
 
sourceFile = fopen(argv[1], "r");
 
destFile = fopen("encryptedFile.txt", "a");


 while(
feof(sourceFile) == 0) {

 
fgets(buff, sizeof(buff), sourceFile);

 
puts(buff); //prints initial content of buffer

 
for(x = 0; buff[x] != '\0'; x++, len--) { //1st obfuscation
 
placeHolder = buff[len-1];
 
buff[len-1] = buff[x];
 
buff[x] = placeHolder;
 if(&
buff[x] == &buff[len-1] || &buff[x+1] == &buff[len-1]) {
 break;
 }
 }

 
puts(buff); //prints output of 1st

 
for(x = 0; buff[x] != '\0'; x++) { //2nd obfuscation
 
switch(buff[x]) {
 case 
'a':
 
buff[x] = 'u';
 break;
 case 
'e':
 
buff[x] = 'o';
 break;
 case 
'o':
 
buff[x] = 'e';
 break;
 case 
'u':
 
buff[x] = 'a';
 break;
 case 
'A':
 
buff[x] = 'U';
 break;
 case 
'E':
 
buff[x] = 'O';
 break;
 case 
'O':
 
buff[x] = 'E';
 break;
 case 
'U':
 
buff[x] = 'A';
 break;
 default:
 break;
 }
 }

 
puts(buff); //prints output of 2nd

 
for(x = 0; buff[x] != '\0'; x++) { //3rd obfuscation
 
if(buff[x] == 'O') {
 continue;
 } 
 if((
x % 2 == 0) && (x != 0)) {
 
buff[x] = buff[x] ^ XOR_KEY1;
 continue;
 }
 
buff[x] = buff[x] ^ XOR_KEY2;
 }

 
puts(buff); //prints output of 3rd

 
fprintf(destFile, "%s", buff);
 }

 
fclose(sourceFile);
 
fclose(destFile);

 return 
0;

} 

P.S. sorry for the messy code, still learning.

You're getting the length of the string before reading it from the file and writing it into the buffer
(Mar 3, 2020 04:44 AM)Zexture Wrote:  You're getting the length of the string before reading it from the file and writing it into the buffer

Oh you're right duh. Thank you :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by hamburgermanMay 16, 2016 01:04 AM 6 335 May 16, 2016 01:04 AM
Last Post: Odus
[.NET] by Lor‌dMay 4, 2016 08:22 PM 0 186 May 4, 2016 08:22 PM
Last Post: Lor‌d
[.NET] by Eru ChitandaJul 7, 2015 08:21 PM 11 302 Jul 7, 2015 08:21 PM
Last Post: BahNahNah