All pastes #797861 Raw Edit

Someone

public c v1 · immutable
#797861 ·published 2007-11-26 10:54 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "netsec_des.c"void encryptECB(char* input, char* output, int length, char* key){    const_DES_cblock inbuff[8];    DES_cblock outbuff[8];    int blockcount = length/8;    int i,n;    for(i=0; i<blockcount; i++)    {//        printf("key: %s\n", key);        memcpy(inbuff, &input[i*8], 8);    //    fprintf(stderr, "%s\n", inbuff);        netsec_des_crypt(NS_DES_ENCRYPT, inbuff, outbuff, key);           for(n=0; n<8;n++)        {            fputc(*((unsigned int*)inbuff+n), stdout);        }        //fprintf(stdout, "%8.8s", *outbuff);        //strncat(output, outbuff, 8);    }    strncat(output, "\0", 1);}void decryptECB(char* input, char* output, int length, char* key){    const_DES_cblock inbuff[8];    DES_cblock outbuff[8];    int blockcount = length/8;    int i;    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);           printf("%8.8s", *outbuff);//        strncat(output, outbuff, 8);    }}int main (int argc, char* argv[]) {	    int mode;    char* key[1024];    char* inbuffer = (char *)calloc(sizeof(char),1024);    char* outbuffer;    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);    strncat(*key, "\0", 1);        while((c=fgetc(stdin)) != EOF)    {        memcpy(&inbuffer[incount], &c, 1);        incount++;        if(incount%1024==1023)        {               bufflen += 1024;            inbuffer = realloc(inbuffer, bufflen);        }    }    memcpy(&inbuffer[incount], "\0", 1);    outbuffer = (char *)malloc(sizeof(char)*incount+1);    if (mode == NS_DES_ENCRYPT) {encryptECB(inbuffer, outbuffer, incount,(char*)key);}    else {decryptECB(inbuffer, outbuffer, incount, (char*)key);} //   printf("%s\n", outbuffer);    return 0;}