how to remove padding in decryption in c#

Roledenez

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

Iridium

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

c# - How to remove matching items from a list using Linq or otherwise

来自分类Dev

How to remove xmlns="" from xml request

来自分类Dev

How to remove script initialisation from javaScript file

来自分类Dev

How can I zero just the padding bytes of a class?

来自分类Dev

How to remove an object in a JSON object?

来自分类Dev

How to Remove JavaScript Remnants from String in PHP

来自分类Dev

How can I add padding to the intrinsic content size of UILabel?

来自分类Dev

how to remove buttons created dynamically

来自分类Dev

Remove cell padding on top and bottom

来自分类Dev

How to remove unused images from an Xcode project

来自分类Dev

How to remove � from a String?

来自分类Dev

How to remove tweet photos from twitter widget?

来自分类Dev

How do I remove YUI delegated events?

来自分类Dev

How to remove apostrophe from text?

来自分类Dev

Heatmap R: how to remove dashed lines

来自分类Dev

How to rename branch to remove capitalization?

来自分类Dev

How do i remove NULL input in C?

来自分类Dev

How to remove border of diagram when clicking it?

来自分类Dev

How to remove/eliminate outliers using IQR method

来自分类Dev

How to remove every text from a website with Javascript

来自分类Dev

RSA_NO_PADDING在Java和C ++之间有所不同吗?

来自分类Dev

OpenSSL libssl.so错误未定义对'ssl3_cbc_remove_padding'的引用

来自分类Dev

C#等效于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding

来自分类Dev

How to automate ansible vault decryption?

来自分类Dev

MSChart Winforms. How to remove blank points?

来自分类Dev

How to remove a directory without removing its content?

来自分类Dev

C#中的Java AES / CBC / PKCS5Padding

来自分类Dev

How does encode and decode 64 figure out that the last few zeros are mere padding?

来自分类Dev

C#控制台文本与string.Padding()的对齐问题

Related 相关文章

  1. 1

    c# - How to remove matching items from a list using Linq or otherwise

  2. 2

    How to remove xmlns="" from xml request

  3. 3

    How to remove script initialisation from javaScript file

  4. 4

    How can I zero just the padding bytes of a class?

  5. 5

    How to remove an object in a JSON object?

  6. 6

    How to Remove JavaScript Remnants from String in PHP

  7. 7

    How can I add padding to the intrinsic content size of UILabel?

  8. 8

    how to remove buttons created dynamically

  9. 9

    Remove cell padding on top and bottom

  10. 10

    How to remove unused images from an Xcode project

  11. 11

    How to remove � from a String?

  12. 12

    How to remove tweet photos from twitter widget?

  13. 13

    How do I remove YUI delegated events?

  14. 14

    How to remove apostrophe from text?

  15. 15

    Heatmap R: how to remove dashed lines

  16. 16

    How to rename branch to remove capitalization?

  17. 17

    How do i remove NULL input in C?

  18. 18

    How to remove border of diagram when clicking it?

  19. 19

    How to remove/eliminate outliers using IQR method

  20. 20

    How to remove every text from a website with Javascript

  21. 21

    RSA_NO_PADDING在Java和C ++之间有所不同吗?

  22. 22

    OpenSSL libssl.so错误未定义对'ssl3_cbc_remove_padding'的引用

  23. 23

    C#等效于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding

  24. 24

    How to automate ansible vault decryption?

  25. 25

    MSChart Winforms. How to remove blank points?

  26. 26

    How to remove a directory without removing its content?

  27. 27

    C#中的Java AES / CBC / PKCS5Padding

  28. 28

    How does encode and decode 64 figure out that the last few zeros are mere padding?

  29. 29

    C#控制台文本与string.Padding()的对齐问题

热门标签

归档