I have seen many people's questions who met this exception here but I simply did not understand the solution. Short review: My program is a GUI program in C# which suppose to run on two computers in local network and always synchronize, therefore, you receive some kind of shared space to work together with your friend from another computer. My cryptography is in a singleton instance which is responsible to the communication of my program. Here are the functions that Encrypt and Decrypt the data, they receive that data as text or bytes array and return it as byte array or text (depends on which one, decrypt/encrypt):
public static byte[] Encrypt(string text) { desObj = Rijndael.Create(); byte[] cipherBytes; byte[] plainBytes; byte[] plainKey; plainBytes = Encoding.ASCII.GetBytes(text); plainKey= Encoding.ASCII.GetBytes("0123456789abcdef");//Change the key. desObj.Key = plainKey; desObj.Mode = CipherMode.CFB; desObj.Padding = PaddingMode.PKCS7; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(plainBytes, 0, plainBytes.Length); cs.Close(); cipherBytes = ms.ToArray(); ms.Close(); return cipherBytes; } public static string Decrypt(byte[] x) { byte[] plainBytes; byte[] plainKey; desObj = Rijndael.Create(); plainKey = Encoding.ASCII.GetBytes("0123456789abcdef");//Change the key. desObj.Key = plainKey; desObj.Mode = CipherMode.CFB; desObj.Padding = PaddingMode.PKCS7; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, desObj.CreateDecryptor(), CryptoStreamMode.Read); cs.Read(x, 0, x.Length); plainBytes = ms.ToArray(); cs.Close(); ms.Close(); return Encoding.ASCII.GetString(plainBytes); } public static void RecievingMessage() { try { if (srvr != null && openForms != null)//openForms is a list of all the current open forms. { byte[] data = new byte[1024]; int i = 0; string[] message; if (srvr != null) while (true) { Thread.Sleep(20); int bytesRec = srvr.Receive(data); message = Decrypt(data).Split(' '); for (i = 0; i < openForms.Count; i++) { if (message[0].Equals(openForms[i].Name)) { openForms[i].Recieve(message); } } } } } ....//Catch clauses. } public static void SendMessage(string sender, string message) { if (srvr != null) { try { srvr.Send(Encrypt(sender + " " + message + " ")); } ...//Catch clauses. } } The “Padding is invalid and cannot be removed” is shown on the console when I run this program, I know that the encryption is successful (I see it encrypted when it moves through the server) but when I try to Decrypt it writes the exception.