Solution (source code)

= Solution

Assume $\mathbf k\ne0$. Dotting the decomposition with $\mathbf k$ gives
$$
\boxed{A=\frac{\mathbf a\cdot\mathbf k}{|\mathbf k|^2}.}
$$
Crossing it on the left by $\mathbf k$ and using $\mathbf B\cdot\mathbf k=0$ gives $\mathbf k\times\mathbf a=|\mathbf k|^2\mathbf B$. Thus
$$
\boxed{\mathbf B=\frac{\mathbf k\times\mathbf a}{|\mathbf k|^2}.}
$$
Indeed $(\mathbf k\times\mathbf a)\times\mathbf k/|\mathbf k|^2=\mathbf a-A\mathbf k$, verifying the decomposition and the imposed transverse condition.