Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove padding in decryption in c#

Tags:

c#

encryption

I wrote a simple encryption / decryption program, when I decrypted the encrypted text it shows grabridge value end of the decrypted text. my c# code and out put of the code are given below. please help me to get the original text after the decrypt without grabage

 public class CrypterText
{
    static byte[] chiperbytes;
    static byte[] plainbytes;
    static byte[] plainKey;
    static SymmetricAlgorithm desObj;
    public static string encryptData(string ciperData)
    {

        desObj = Rijndael.Create();

        plainbytes = Encoding.ASCII.GetBytes(ciperData);
        plainKey = Encoding.ASCII.GetBytes("0123456789abcdef");
        desObj.Key = plainKey;
        desObj.Mode = CipherMode.CBC;
        desObj.Padding = PaddingMode.ISO10126;
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(plainbytes, 0, plainbytes.Length);
        cs.Close();
        chiperbytes = ms.ToArray();
        ms.Close();
        return Encoding.ASCII.GetString(chiperbytes);

    }

    public static string decrypt() {
        MemoryStream ms = new MemoryStream(chiperbytes);
        CryptoStream cs = new CryptoStream(ms, desObj.CreateDecryptor(), CryptoStreamMode.Read);
        cs.Read(chiperbytes, 0, chiperbytes.Length);
        plainbytes = ms.ToArray();
        cs.Close();
        ms.Close();
        return Encoding.ASCII.GetString(plainbytes);
    }

}

enter image description here

like image 245
Roledenez Avatar asked Nov 16 '25 08:11

Roledenez


1 Answers

In all likelihood, the padding has been removed, however because you are writing to the same byte array that contains the encrypted data, the bytes of ciphertext after the plaintext are being included in the string. You should decrypt to a separate byte array, and then use that byte array to construct the plaintext string. It's also important to use the return value of Read() during the decryption which will indicate the number of bytes actually decrypted.

There are a number of other significant issues with the code here, such as the fact that your SymmetricAlgorithm is only initialized during the encryption process, making it currently impossible to decrypt without having first encrypted. You should also not attempt to convert the ciphertext into a string via any of the Encoding.GetString() methods - arbitrary byte arrays are generally not valid encoded strings, and it will not be possible to reconstruct the original byte array from the string in order to decrypt. Instead use Convert.ToBase64String() and Convert.FromBase64String() to ensure consistent round-trip from ciphertext byte array to string and back again.

like image 199
Iridium Avatar answered Nov 18 '25 01:11

Iridium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!