Jump to content
JIHAD

C string encryption XOR & base64

Recommended Posts

Posted

Exemplu de criptare XOR.

String -> base64 -> XOR -> base64.

avem si encryption key.

se compileaza. se executa ./enc -e string ptr. criptare si ./enc -d hash ptr. decriptare


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TABLELEN 63
#define BUFFFERLEN 128

#define ENCODERLEN 4
#define ENCODEROPLEN 0
#define ENCODERBLOCKLEN 3

#define PADDINGCHAR '='
#define BASE64CHARSET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\
"abcdefghijklmnopqrstuvwxyz"\
"0123456789"\
"+/";
char key[100]="pass123";

int Base64Encode(char *input, char *output, int oplen);
int encodeblock(char *input, char *output, int oplen);
int Base64Decode(char *input, char *output, int oplen);
int decodeblock(char *input, char *output, int oplen);
int decodeblock(char *input, char *output, int oplen){
int rc = 0;
char decodedstr[ENCODERLEN + 1] = "";

decodedstr[0] = input[0] << 2 | input[1] >> 4;
decodedstr[1] = input[1] << 4 | input[2] >> 2;
decodedstr[2] = input[2] << 6 | input[3] >> 0;
strncat(output, decodedstr, oplen-strlen(output));

return rc;
}

int Base64Decode(char *input, char *output, int oplen){
char *charval = 0;
char decoderinput[ENCODERLEN + 1] = "";
char encodingtabe[TABLELEN + 1] = BASE64CHARSET;
int index = 0, asciival = 0, computeval = 0, iplen = 0, rc = 0;

iplen = strlen(input);
while(index < iplen){
asciival = (int)input[index];
if(asciival == PADDINGCHAR){
rc = decodeblock(decoderinput, output, oplen);
break;
}else{
charval = strchr(encodingtabe, asciival);
if(charval){
decoderinput[computeval] = charval - encodingtabe;
computeval = (computeval + 1) % 4;
if(computeval == 0){
rc = decodeblock(decoderinput, output, oplen);
decoderinput[0] = decoderinput[1] =
decoderinput[2] = decoderinput[3] = 0;
}
}
}
index++;
}

return rc;
}

int encodeblock(char *input, char *output, int oplen){
int rc = 0, iplen = 0;
char encodedstr[ENCODERLEN + 1] = "";
char encodingtabe[TABLELEN + 1] = BASE64CHARSET;

iplen = strlen(input);
encodedstr[0] = encodingtabe[ input[0] >> 2 ];
encodedstr[1] = encodingtabe[ ((input[0] & 0x03) << 4) |
((input[1] & 0xf0) >> 4) ];
encodedstr[2] = (iplen > 1 ? encodingtabe[ ((input[1] & 0x0f) << 2) |
((input[2] & 0xc0) >> 6) ] : PADDINGCHAR);
encodedstr[3] = (iplen > 2 ? encodingtabe[ input[2] & 0x3f ] : PADDINGCHAR);
strncat(output, encodedstr, oplen-strlen(output));

return rc;
}

int Base64Encode(char *input, char *output, int oplen){
int rc = 0;
int index = 0, ipindex = 0, iplen = 0;
char encoderinput[ENCODERBLOCKLEN + 1] = "";

iplen = strlen(input);
while(ipindex < iplen){
for(index = 0; index < 3; index++){
if(ipindex < iplen){
encoderinput[index] = input[ipindex];
}else{
encoderinput[index] = 0;
}
ipindex++;
}
rc = encodeblock(encoderinput, output, oplen);
}

return rc;
}

int main(int argc, char **argv) {
int rc = 0;
char encodedoutput[BUFFFERLEN + 1] = "";
char decodedoutput[BUFFFERLEN + 1] = "";
int i;



if(strcmp(argv[1],"-e")==0)
{
encrypt(argv[2],encodedoutput );
printf ("%s \n", encodedoutput);
}
if(strcmp(argv[1],"-d")==0)
{
decrypt(argv[2],decodedoutput );
printf ("%s \n", decodedoutput);
}

}

int encrypt(char *inputs, char encodedoutput[BUFFFERLEN + 1], char decodedoutput[BUFFFERLEN + 1]){
char encodedoutput1[BUFFFERLEN + 1] = "";
int i;
int rc = 0;
rc = Base64Encode(inputs, encodedoutput1, BUFFFERLEN);
for(i=0; i<strlen(encodedoutput1); i++)
{
encodedoutput1[i]=encodedoutput1[i]^key[i];
}
rc = Base64Encode(encodedoutput1, encodedoutput, BUFFFERLEN);
return 0;
}


int decrypt(char *inputs, char encodedoutput[BUFFFERLEN + 1], char decodedoutput[BUFFFERLEN + 1]){
char decodedoutput1[BUFFFERLEN + 1] = "";
int i;
int rc = 0;
rc = Base64Decode(inputs, decodedoutput1, BUFFFERLEN);
for(i=0; i<strlen(decodedoutput1); i++)
{
decodedoutput1[i]=decodedoutput1[i]^key[i];
}
rc = Base64Decode(decodedoutput1, decodedoutput, BUFFFERLEN);
return 0;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...