Are you doing encryption and decryption using "MyKey".
Listen, Every Encryption and Decryption algorithm has its valid key sizes depending on the Algorithm like 256 bit key means 32 bytes. So your code seems fine to me except you are using an invalid key.
Better Try to get a key like this:
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
string key = Convert.ToBase64String(DES.Key);
use key // for Encryption and Decryption becuase its now valid in size
Second point to get bytes from key string, dont use ASCII encoding or some other try convertying it with Convert.FromBase64String();
If you dont want to use above method and want to use your own password for encryption then explore class PasswordDeriveBytes in System.Security.CryptoGraphy but before doing this I would recomend you to atleast run your code with success with the method defing above by me.
I hope it'll work!
Best Regards,
|