genkey function