All pastes #718653 Raw Edit

Anonymous

public text v1 · immutable
#718653 ·published 2007-09-28 16:28 UTC
rendered paste body
/*
 * cbc -	A simple block cipher using Cipher Block Chaining (CBC)
 *			cbc needs an initialization vector and a key to encrypt
 *			
 *
 * Copyright (C) 2007 - Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */


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

typedef uint8_t data;
typedef uint8_t* dataptr;


#define BLOCKSIZE 20


/* write a hex string to file
 */
static void write_hex_string (FILE *file, dataptr buf, size_t len) {	
	for (int i = 0; i < len; ++i) {
		fprintf(file, "%02x", buf[i]);
	}
}

/* Read hex string representation to 'data' repr.
 * takes an allocated buf for the data and the string
 * len is the length of buf
 */
static void read_hex_string (dataptr buf, size_t len, char *string) {	
	size_t str_len = strlen(string);
	size_t read = (len < str_len/2 ? len : str_len/2);
	char str[3];
	
	for (int i = 0; i < read; i++) {
		strncpy(str, string+2*i, 2);
		str[3] = '\0';
		
		buf[i] = strtol(str, NULL, 16);
		fprintf(stderr, "%s = %02x\n", str, strtol(str, NULL, 16));
		
	}
}



int main (int argc, char *argv[]) {
	const size_t block_size = BLOCKSIZE;
	data iv[block_size];
	
	
	read_hex_string(iv, block_size, "0000000000000078787807070707070707070707");
	write_hex_string(stderr, iv, block_size);
	return 0;

}