For my project I needed to generate the keys in a .NET application so I ported over most of this project to C#.
Class file:
gist.github.com/bagelxjames/29fc2fc
...
It generates keys that validate against the PHP version.
I didn't port the codeValidate function but trust me the hard part is done ;)