Standard protocols for quantum key distribution (QKD) require that the sender be able to transmit in two or more mutually unbiased bases. Here, we analyze the extent to which the performance of QKD is degraded by diffraction effects that become relevant for long propagation distances and limited sizes of apertures. In such a scenario, different states experience different amounts of diffraction, leading to state-dependent loss and phase acquisition, causing an increased error rate and security loophole at the receiver. To solve this problem, we propose a pre-compensation protocol based on pre-shaping the transverse structure of quantum states. We demonstrate, both theoretically and experimentally, that when performing QKD over a link with known, symbol-dependent loss and phase shift, the performance of QKD will be better if we intentionally increase the loss of certain symbols to make the loss and phase shift of all states same. Our results show that the pre-compensated protocol can significantly reduce the error rate induced by state-dependent diffraction and thereby improve the secure key rate of QKD systems without sacrificing the security.