Solution 1:
In the circumstances you describe both "key to" and "key for" are acceptable.
Likewise both "code for" and "code to" are acceptable.
In the circumstances you describe both "key to" and "key for" are acceptable.
Likewise both "code for" and "code to" are acceptable.