If the landlord says you owe 2 months rent yet you say that you have paid the rent every month then you should have the reciepts or cancelled checks showing proof that you have already paid. No contest as I see it if you have those receipt showing you have paid the months the landlord says you owe.
If you have no proof of payment and he proves you lived there during that time, then I suspect you are going to pay again.
Good reason to obtain rent receipts or cancelled checks for proof of payment at all times.
Your security deposit is a different subject from your rent owed and transferring of funds is not possible unless you can get an agreement with the landlord to give credit to you transferring the security money to your rent. I extremely doubt that will happen. But if you get that one pulled off then be sure to receive documentation of that transaction in writing.
Good Luck
Wg