I believe a simpler way is to use the unique factorization to show that for
$$n = \prod_{i \, = \, 1}^{m} p_i^{a_i} \tag{1}\label{eq1}$$
where the $p_i$ are unique primes, means that
$$n^2 = \prod_{i \, = \, 1}^{m} p_i^{2a_i} \tag{2}\label{eq2}$$
Thus, if $3 \; \vert \; n^2$, then one of the $p_i$ must be $3$, so $3 \; \vert \; n$.
Also, using your suggestion, if $n = 3k + 1$ or $n = 3k + 2$, then $n^2 = 9k^2 + 6k + 1$ or $n^2 = 9k^2 + 12k + 4$. In either case, when dividing by $3$, there is a remainder of $1$, showing that $3$ doesn't divide $n^2$. But, as $3 \; \vert \; n^2$, then $n$ cannot be either of the $2$ forms, so it must be $n = 3k$, giving that $3 \; \vert \; n$. This is an example of proving what's requested using contrapositive as it shows that if $3 \not{\vert} \; n$, then $3 \not{\vert} \; n^2$.