If you cast a vote in a closed primary without being properly registered, yes, you would have committed a crime. This would be difficult in most primaries though, as they control access to the ballots or voting machines. For example, in two states where I resided, the process is to go up to someone who looks up your name on a list, possibly show ID, they give you a two part receipt, and a different person takes half the receipt, escorts you to the election machine, and activates it so that you can vote. So you can't just grab an empty machine and vote.
Now in a caucus, this seems more feasible. In Iowa, the process is to control entry to the room. Since everyone votes multiple times and at the same time as everyone else, it is more difficult to control individuals. So if you can bypass the entry check and enter the room in such a way that they didn't notice, you might be able to manage it. They do sanity checks though. For example, there should be no more people at the caucus than signed up for entry. So you need someone else to leave the caucus so that you can enter or you need to pretend to be a valid voter at the entry point. And yes, that would be illegal and there'd be many witnesses.
There was a previous question about voting twice, once for a candidate from each party.