In American English, these words do have a similar sound, and the final 't' often isn't audible. One way of telling the difference, though, is to listen to the stress pattern of the whole phrase.
If you say 'I can do it' it is usually the word 'do' which is stressed, while the 'can' is a shorter and weaker sound. If you say 'I can't do it', the word 'can't' is stressed, so 'can't' is a longer and stronger sound.
In standard British English, the pronunciation of 'can' and 'can't' is totally different, so these problems don't arise.