Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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: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:2437994
Created April 21, 2012 15:59
Encrypt with AES using Java.
public static byte[] encrypt(String plainText, String encryptionKey, String IV) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(plainText.getBytes("UTF-8"));
}
@bricef
bricef / AES.c
Last active May 11, 2024 21:15
A simple example of using AES encryption in Java and C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* MCrypt API available online:
* http://linux.die.net/man/3/mcrypt
*/
#include <mcrypt.h>
@bricef
bricef / app.py
Created March 3, 2012 22:42
Trivially simple Bottlepy application
from bottle import route, run
@route('/hello')
def hello():
return "Hello World!"
application = bottle.default_app()
if __name__ == "__main__:
run(host='localhost', port=8080, debug=True)
@bricef
bricef / timeline.py
Created December 9, 2011 09:16
Script to generate a random timeline from nation names stored in names.txt
#!/usr/bin/env python
#
# to create a visualisation, run like this:
#
# ./timeline.py --dot | dot -Tpng > filename.png
#
import sys
@bricef
bricef / pinc.py
Created August 17, 2011 21:07
Arbitrary file parser allowing file include and include script output
import sys
import re
import shlex
import subprocess as sp
exe_pat = re.compile(r'(\s*)\(!>(.*)<\)\s*')
inc_pat = re.compile(r'(\s*)\(>(.*)<\)\s*')
if __name__ == "__main__":
for line in sys.stdin: