简体   繁体   中英

how to remove padding in decryption in c#

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);
    }

}

在此输入图像描述

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.

Try this

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

Extra characters are leftover from encrypt process. Move byte arrays from class variables into local variables. After which give encrypted string back to decrypt method as a parameter.

Also personally I think it's not good idea to have non static class that has only static methods. Either make class static or at least some of it's methods non-static, whichever is more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM