The best way to approach it, is to break it down into functional sections, and think about each section seperately.
For example, you might break it into:
1) Power supply
2) Keypad + keypad interface
3) Code checking logic
4) Timer
5) Electromagnet driver
6) Alarm output
To open the safe - instead of turning OFF the electromagnet, how about turning it ON? That way, if you have a power failure, the safe won't automatically unlock, and besides, having an electromagnet running continuously (whilst you are not accessing the safe) is wasteful of power.
If you have some programming experience (or wish to gain some), perhaps you can combine the majority of these functions into a small microprocessor (eg PIC or PICaxe).