There's a much easier way to build the same functionality: use encrypted sparse disk images. See the `hdiutil` command line tool.
I used this many years ago to build a Mac app with the exact same functionality, and probably much safer encryption: http://excesapp.com/