All pastes #136339 Raw Edit

Untitled

public c v1 · immutable
#136339 ·published 2006-08-17 16:37 UTC
rendered paste body
#define _CRT_SECURE_NO_DEPRECATE#include <stdio.h>#include <windows.h>typedef unsigned int u32;typedef unsigned char u8;extern void SEED_keysetup(u32 ks[32], const u8 key[16]);extern void SEED_encrypt(u8 buf[16], const u32 ks[32]);extern void SEED_decrypt(u8 buf[16], const u32 ks[32]);static const u8 key[16] = { 'D',  'G',  '_',  'S',  'u',  'c',  'c',  'e',  's',  's', 0x6E, 0x84, 0x9C, 0xB6, 0xD2, 0xF0};static u32 ks[32];static int decryptblock(u8 *inblock, int inlen, u8* outblock, int *outlen){	u8 tmp[16];	int i;	int blocklen, cryptblocklen, totalblocklen;	if(inlen < 2) {		printf("undersize block, inlen %i\n", inlen);		return -1;	}	blocklen = (inblock[0] << 4) + inblock[1];	cryptblocklen = (blocklen + 15) & ~15; //round up to 16 bytes	totalblocklen = (cryptblocklen + 2 + 15) & ~15; //2 bytes header + cryptblock, round up to 16 bytes	if(inlen != totalblocklen) {		printf("wrong block size, inlen %i, expected %i\n", inlen, totalblocklen);		return -1;	}	for(i = 0; i < cryptblocklen; i += 16)	{		memcpy(tmp, inblock + 2 + i, 16);		SEED_decrypt(tmp, ks);		memcpy(outblock + i, tmp, 16);	}	*outlen += blocklen;	return 0;}int decryptbin(const char *infilename, const char *outfilename){	FILE *fi, *fo;	u8 ibuf[0x650], obuf[0x650];	int ilen, olen;	fi = fopen(infilename, "rb");	fo = fopen(outfilename, "wb");	for(;;) {		ilen = (int)fread(ibuf, 1, 0x650, fi);		if(ilen <= 0) { //unusual but possible end of file			printf("no partial block at end\n");			break;		}		olen = 0;		if(decryptblock(ibuf, ilen, obuf, &olen)) {			return -1;		}		if(olen == 0) {			printf("partial block with 0 length at end\n");			break;		}		if(fwrite(obuf, 1, olen, fo) != olen) {			printf("fwrite failed\n");			return -1;		}		if(ilen != 0x650) //end of file			break;	}	fclose(fo);	fclose(fi);	return 0;}int txttobin(const char *filename){	char onamebuf[512];	strcpy(onamebuf, filename);	strcpy(onamebuf + strlen(onamebuf) - 4, ".txt");	return decryptbin(filename, onamebuf);}int recursedir(const char *dirname){	char tname[512];	WIN32_FIND_DATA fd;	HANDLE h;	sprintf(tname, "%s/*", dirname);	h = FindFirstFile(tname, &fd);	if(h == INVALID_HANDLE_VALUE)		return 0;	do {		if(strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..")) {			if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {				sprintf(tname, "%s/%s", dirname, fd.cFileName);				if(recursedir(tname))					return -1;			} else {				if(strlen(fd.cFileName) >= 4 && strcmp(fd.cFileName + strlen(fd.cFileName) - 4, ".bin") == 0) {					sprintf(tname, "%s/%s", dirname, fd.cFileName);					printf("converting %s\n", tname);					if(txttobin(tname))						return -1;				}			}		}	} while(FindNextFile(h, &fd));	return 0;}int main(int argc, char **argv){	SEED_keysetup(ks, key);	if(recursedir("data")) {		return 2;	}	return 0;}