Note that this is just plain RSA, which is not very secure[1]. To be secure, RSA must be used with some padding scheme, which is more complicated than it sounds (PKCS v1.5 was one such scheme that was eventually found vulnerable, for example).
When in doubt, one really should prefer existing crypto libraries that have been checked by people who understand crypto.