All pastes #797977 Raw Edit

Something

public c v1 · immutable
#797977 ·published 2007-11-26 13:42 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <openssl/rand.h>#include "netsec_des.c"int encryptEBC(char* input, char* output, unsigned int length, char* key, char* iv){    const_DES_cblock* inbuff=(const_DES_cblock*)malloc(sizeof(const_DES_cblock));    DES_cblock outbuff[1];    int newlength=length;    if((length+4)%8 != 0)    {        RAND_bytes((unsigned char*)(input+length), (length+4)%8);        newlength=length+(8-(length+4)%8);    }    int blockcount = (newlength+4)/8;    char* buff[8];    unsigned int* xorbuff, *xorbuff2;    int i;    xorbuff = (unsigned int*)calloc(sizeof(int), 2);    xorbuff2 = (unsigned int*)calloc(sizeof(int), 2);    for(i=0; i<blockcount; i++)    {        if(i==0)         {            memcpy(buff, &length, 4);             memcpy(buff+4, &input[0], 4);            memcpy(xorbuff, buff, 8);            memcpy(xorbuff2, iv, 8);            xorbuff=xorbuff ^ xorbuff2;                       memcpy(inbuff, xorbuff, 8);        }        else         {//            memcpy(inbuff, &input[i*8-4], 8);            xorbuff=((unsigned int)buff ^ (unsigned int)outbuff);            memcpy(inbuff, xorbuff, 8);        }        netsec_des_crypt(NS_DES_ENCRYPT, inbuff, outbuff, key);           memcpy(output+i*8, outbuff, 8);    }    return newlength+4;}int decryptCBC(char* input, char* output, unsigned int length, char* key, char* iv){    const_DES_cblock inbuff[8];    DES_cblock outbuff[8];    int blockcount = length/8;    int i;    unsigned int reallength=0;    for(i=0; i<blockcount; i++)    {        memcpy(inbuff, &input[i*8], 8);        netsec_des_crypt(NS_DES_DECRYPT, (const_DES_cblock*)inbuff, (DES_cblock*)outbuff, key);            if(i==0){             memcpy(&reallength, outbuff, 4);             memcpy(output, outbuff, 8);            memmove(output, output+4, 4);        }        else memcpy(output+(i*8-4), outbuff, 8);    }    return reallength;}int main (int argc, char* argv[]) {	    int mode;    char* key[1024], iv[1024];    char* inbuffer = (char *)calloc(sizeof(char),1024);    char* outbuffer;    unsigned int length=0;    int c;    int incount=0, bufflen=1024;    if(argc != 3) { printf("wrong number of arguments! \n USAGE: crypt_ecb -d|-e key\n"); return 1;}    if(strncmp(argv[1],"-e", 2) == 0) { mode=NS_DES_ENCRYPT;}    else if(strncmp(argv[1], "-d", 2) == 0) { mode=NS_DES_DECRYPT;}     else { printf("no mode flag set, use -e or -d %s\n", argv[1]); return 1;}        memcpy(key, argv[2], 1023);    memcpy(iv, argv[3], 1023);    strncat(*key, "\0", 1);    strncat(*iv, "\0", 1);        while((c=fgetc(stdin)) != EOF)    {        memcpy(&inbuffer[incount], &c, 1);        incount++;        if(incount%1024==0)        {               bufflen += 1024;            inbuffer = realloc(inbuffer, bufflen);        }    }        outbuffer = (char *)calloc(sizeof(char),incount+4);    if (mode == NS_DES_ENCRYPT) {length=encryptECB(inbuffer, outbuffer, incount,(char*)key, (char*)iv);}    else {length=decryptECB(inbuffer, outbuffer, incount, (char*)key, (char*)iv);}    fwrite(outbuffer, sizeof(char), length, stdout);    return 0;}