Just because it tells you there's an error doesn't mean your using try/catch block correctly. It will tell you there is an error even if you have any try/catch blocks. You need to try to handle the error in the try catch block.
From what you described, you need to reset the variables you're using inside the try/catch blocks or reset them when the encrypting is done (i.e. in a finally block). I'm like 99.99% sure that's what your problem is.