Need help with this source code

Posts 1–3 of 3 · Page 1 of 1
Need help with this source code
Hello I got a problem with the padding after decrytion.

(I dont want to use convert.tobase64string metheod)

So, Ive found this source code, for encrypting and decrypting.

For example:

txtboxNormalString.text = "Hello"

and pressed on encrytion button, txtboxEncrytedString.text = "??:??7s???2?),?" (Hello encrypted)

Then pressed on Decryted button, And txtboxDecrytedString.text = "hello7s???2?),?"

Can somebody explain me how to remove the padding after decryption.

Encryption / decryption is new for me so i dont really know much about it for now.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace EncryptionDecryption
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DesObj = Rijndael.Create();
        }

        string NormalString;
        byte[] CipherBytes;
        byte[] PlainBytes;
        byte[] PlainBytes2;
        byte[] PlainKey;
        SymmetricAlgorithm DesObj;

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            NormalString = txtboxNormalString.Text;
            PlainBytes = Encoding.ASCII.GetBytes(NormalString);
            PlainKey = Encoding.ASCII.GetBytes("12845726gh3846gs");
            DesObj.Key = PlainKey;
            DesObj.Mode = CipherMode.CBC;
            DesObj.Padding = PaddingMode.PKCS7;
            System****.MemoryStream ms = new System****.MemoryStream();
            CryptoStream cs = new CryptoStream(ms,DesObj.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(PlainBytes, 0, PlainBytes.Length);
            cs.Close();
            CipherBytes = ms.ToArray();
            ms.Close();
            txtboxEncryptedString.Text = Encoding.ASCII.GetString(CipherBytes);
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            System****.MemoryStream ms1 = new System****.MemoryStream(CipherBytes);
            CryptoStream cs1 = new CryptoStream(ms1, DesObj.CreateDecryptor(), CryptoStreamMode.Read);
            cs1.Read(CipherBytes, 0, CipherBytes.Length);
            PlainBytes2 = ms1.ToArray();
            cs1.Close();
            ms1.Close();
            txtboxDecryptedString.Text = Encoding.ASCII.GetString(PlainBytes2);
        }
Might be a problem with the brackets
Are you sure you're typing everything in ASCII? problem might be unicode characters, since C# project is by default in unicode.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us