You have several options (hardest to easiest):
- Invent and code the algorithm.
- Implement an existing algorithm.
- Get the source code of an existing implementation (there are several available), and use it according to your needs.
- Use an existing library providing encryption/decryption facilities.
The choice is up to you (and
Google is your friend).