It's often the intentions that matter. Doing X may not be a crime, but doing it with the intention to commit crime could be. And while outsiders can't know the true intentions, courts are often happy accept the intentions seen by a "reasonable person" as the truth. Which means that if you want to write malware, you should look like a respectable person and not do anything too shady.