A key and lock device comprises a key having a first electronic circuit and a lock having a second electronic circuit. The key and the lock store secret information, some of which is unique for each device. The key and the lock exchange a random number through connectors and perform a calculation in the respective circuitry based on the random number and secret information. An electrical blocking mechanism is moved to a non-blocking position if a comparison of the calculations in the circuits gives the correct result.