added bouncy castle code from stack overflow post