Skip to content

Instantly share code, notes, and snippets.

@bricef
bricef / gist:2438023
Created April 21, 2012 16:06
Encrypt with AES using C
int encrypt(
void* buffer,
int buffer_len,
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
@bricef
bricef / gist:2438865
Created April 21, 2012 18:04
Pad a byte[] to a given block size
public static byte[] padBytes(byte[] str, int blocksize){
byte[] barr;
if (str.length > blocksize){
int padbytes = str.length % blocksize ;
barr = new byte[str.length + (blocksize-padbytes)];
}else{
barr = new byte[blocksize];
}
@bricef
bricef / gist:2438900
Created April 21, 2012 18:13
Pad a byte[] to a given block size
void* padBuffer(void* buf, int buflen, int blocklen){
void* barr;
int barrlen;
if(buflen > blocklen){
barrlen = buflen + (blocklen-(buflen%blocklen));
}else{
barrlen = blocklen;
}
@bricef
bricef / gist:2438921
Created April 21, 2012 18:16
AES Decryption in Java
public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return new String(cipher.doFinal(cipherText),"UTF-8");
}
@bricef
bricef / gist:2438927
Created April 21, 2012 18:17
AES Decryption in C
int decrypt(
void* buffer,
int buffer_len,
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){return 1;}
@bricef
bricef / gist:2463908
Created April 22, 2012 12:22
Generating an IV in Java
import java.security.SecureRandom;
public static byte[] genIV(int len) throws Exception{
byte[] arr = new byte[len];
SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");
prng.nextBytes(arr);
return arr;
}
@bricef
bricef / gist:2464895
Created April 22, 2012 16:00
Generating an IV in C
int genIV(
void* buffer,
int buffer_len
){
/*
* Note that we propagate the return code from the library called.
*/
#ifndef _WIN32
return RAND_bytes(buffer, buffer_len);
#else
/* random.c */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void parser(char *input, double particles[][4]){
sscanf(
input,
"p[20] = nullvector(%lf,%lf,%lf,%lf)",
&particles[20][3], &particles[20][0],
#include <stdio.h>
#if defined(SMALL)
#define SIZE 30
#endif
#if defined(LARGE)
#define SIZE 3000
#endif
@bricef
bricef / smartystring.c
Last active April 15, 2020 06:56
Dynamically growing a string in C.
#include <stdio.h>
#include <stdlib.h>
#define SMARTY_SIZE_INIT 16
typedef struct {
char * str; // a null terminated C string
char * end; // a pointer to the null byte, to be able to repeatedly append
// without using strlen() every time.
size_t size; // currently allocated size for *str, so we know when we