0

Ok, i made a code to Encrypt a file (any file), but i have a problem to do the Decrypter


and the code to the Decrypter (doesn't work)

if (RestoreFile.ShowDialog() == DialogResult.OK)
            {
                FileStream fsFileIn = File.OpenRead(o.FileName);
                FileStream fsKeyFile = File.OpenRead(o.FileName);
                FileStream fsFileOut = File.Create(RestoreFile.FileName);

                TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
                BinaryReader brFile = new BinaryReader(fsKeyFile);
                cryptAlgorithm.Key = brFile.ReadBytes(24);
                cryptAlgorithm.IV = brFile.ReadBytes(8);

                CryptoStream csEncrypt = new CryptoStream(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
                StreamReader srCleanStream = new StreamReader(csEncrypt);
                StreamWriter swCleanStream = new StreamWriter(fsFileOut);
                swCleanStream.Write(srCleanStream.ReadToEnd());
                
                swCleanStream.Close();
                fsFileOut.Close();
                srCleanStream.Close();
            }

Edited by Razer_90: n/a

9
Contributors
27
Replies
29
Views
6 Years
Discussion Span
Last Post by hirenpatel53
Featured Replies
  • 1

    in your original code you open the encrypted file with two different handlers. One of which reads the key and IV values. The other then attempts to decrypt the *entire* file including the key and IV values. This won't work. Your encrypting code doesn't even write the key and IV … Read More

0

do look at the code it's just dose not work, if someone can help me with the code it will be great

0

it doesn't do en error, it's just don't work..
this is how i'm encrypt:

string encr = FilesListView.Items[0].Text + "(Encrypted)";
            encr = encr.Substring(0, FilesListView.Items[0].Text.Length - 4) + " (Encrypted)";

            saveEncFile.FileName = encr;
            saveEncFile.InitialDirectory = @"C:\";
            if (saveEncFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    FileStream fs = File.Create(saveEncFile.FileName);
                    TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
                    CryptoStream cs = new CryptoStream(fs, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
                    StreamWriter sw = new StreamWriter(cs);
                    StreamReader sr = new StreamReader(o.FileName);
                    string currLine = sr.ReadLine();
                    while (currLine != null)
                    {
                        sw.Write(currLine);
                        currLine = sr.ReadLine();
                    }
                    sr.Close();
                    sw.Flush();
                    sw.Close();
                }
                catch
                {
                    return;
                }
            }
1

in your original code you open the encrypted file with two different handlers. One of which reads the key and IV values. The other then attempts to decrypt the *entire* file including the key and IV values. This won't work.

Your encrypting code doesn't even write the key and IV values, so don't know what you are thinking there.

0

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
You must be the most informative guy over here :)

0

lol!!! i'm sorry i didn't notice it was change the page.. hh i always wrote " ??? " cus i save the first page as Favorite
hh i didn't notice.. sorry about that


for the post.. i don't want 2 files.. this is why i deleted the Key.. can't i do it without 2 files?

0
public class TripleDES_EncryptionDecryption
    {
        public static string EncryptTripleDES(string Data, string Password)
        {
            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(Data);

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
               new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });

            MemoryStream ms = new MemoryStream();

            TripleDES tsp = TripleDESCryptoServiceProvider.Create();
            tsp.Key = pdb.GetBytes(16);
            tsp.IV = pdb.GetBytes(8);


            CryptoStream cs = new CryptoStream(ms, tsp.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();


            byte[] encryptedData = ms.ToArray();
            return System.Convert.ToBase64String(encryptedData);
        }

        public static string DecryptTripleDES(string Data, string Password)
        {
            byte[] clearBytes = System.Convert.FromBase64String(Data);


            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,


                new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
            MemoryStream ms = new MemoryStream();

            TripleDES tsp = TripleDESCryptoServiceProvider.Create();
            tsp.Key = pdb.GetBytes(16);

            tsp.IV = pdb.GetBytes(8);
            CryptoStream cs = new CryptoStream(ms, tsp.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
            byte[] decryptedData = ms.ToArray();            
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }
    }

Now try this code

MessageBox.Show(TripleDES_EncryptionDecryption.DecryptTripleDES("C8+wW3PYxx0v9A0JvcquhgEBoc5/uU9i", String.Empty));
0

but what should i do in the button Event?
how can i connect this DecryptTripleDES();
i mean what to write in the ();

0
private string input;

private void button_Click(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader(@"pathtofile\withencrypteddata\goeshere"))
{
input = sr.ReadToEnd();
}
string decryptedData = TripleDES_EncryptionDecryption.DecryptTripleDES(input, String.Empty);
// now do what you want with the decrypted data
MessageBox.Show(decryptedData);
}

Edited by CsharpChico: n/a

0

Ok, i made a code to Encrypt a file (any file), but i have a problem to do the Decrypter


and the code to the Decrypter (doesn't work)

if (RestoreFile.ShowDialog() == DialogResult.OK)
            {
                FileStream fsFileIn = File.OpenRead(o.FileName);
                FileStream fsKeyFile = File.OpenRead(o.FileName);
                FileStream fsFileOut = File.Create(RestoreFile.FileName);

                TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
                BinaryReader brFile = new BinaryReader(fsKeyFile);
                cryptAlgorithm.Key = brFile.ReadBytes(24);
                cryptAlgorithm.IV = brFile.ReadBytes(8);

                CryptoStream csEncrypt = new CryptoStream(fsFileIn, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
                StreamReader srCleanStream = new StreamReader(csEncrypt);
                StreamWriter swCleanStream = new StreamWriter(fsFileOut);
                swCleanStream.Write(srCleanStream.ReadToEnd());
                
                swCleanStream.Close();
                fsFileOut.Close();
                srCleanStream.Close();
            }

Please provide a complete listing so we can load it into our development environment and investigate.

0

CsharpChico, what do i need to do here??

if (saveEncFile.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(encr))
                {

                }

                string encryptData = TripleDES_EncryptionDecryption.EncryptTripleDES(WriteInput, String.Empty);
            }
0
if (saveEncFile.ShowDialog() == DialogResult.OK)
    {
    string encryptData = TripleDES_EncryptionDecryption.EncryptTripleDES(WriteInput, String.Empty);

    using (StreamWriter sw = new StreamWriter(saveEncFile.FileName))
    {
     sw.Write(encryptData);
    }
     

    }

note the String.Empty is the password if you want to add password protection to the encrypted data change String.Empty to a password. Also I Notice that the key that you use in your question = 24 but the answer i offered (the Security class) uses 16 as tsp.Key = pdb.GetBytes(16) to make 24 just change the 16 to 24

Edited by CsharpChico: n/a

0

is there encrypted data in the file your reading? are you setting anythin to WriteInput before trying to save? you have to be more descriptive are you recieving errors?

0

Here is a comple project so you can see how it works :)

namespace EncryptDecryptTest
{
    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using System.Security.Cryptography;

    public partial class Form1 : Form
    {
        private TableLayoutPanel StatusBarControl;
        private TableLayoutPanel ControlMain;

        private FlowLayoutPanel DecryptedControls;
        private FlowLayoutPanel EncryptedControls;
        private FlowLayoutPanel PasswordControl;

        private Button buttonEncrypt;
        private Button buttonClearDecryptBox;
        private Button buttonCopyDecryptToClipboard;
        private Button buttonReadFile;

        private Button buttonDecrypt;
        private Button buttonClearEncryptBox;
        private Button buttonCopyEncryptToClipboard;
        private Button buttonSaveFile;

        private RichTextBox richtextboxDecrypted;
        private RichTextBox richtextboxEncrypted;

        private TextBox textboxPassword;

        private Label labelPassword;

        private OpenFileDialog EncryptionReaderControl;
        private SaveFileDialog EncryptionWriterControl;

        private StreamReader EncryptionReader;
        private StreamWriter EncryptionWriter;

        private String EncryptedData;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.StatusBarControl = new System.Windows.Forms.TableLayoutPanel();
            this.ControlMain = new System.Windows.Forms.TableLayoutPanel();
            this.DecryptedControls = new System.Windows.Forms.FlowLayoutPanel();
            this.buttonEncrypt = new System.Windows.Forms.Button();
            this.buttonClearDecryptBox = new System.Windows.Forms.Button();
            this.buttonCopyDecryptToClipboard = new System.Windows.Forms.Button();
            this.buttonReadFile = new System.Windows.Forms.Button();
            this.EncryptedControls = new System.Windows.Forms.FlowLayoutPanel();
            this.buttonDecrypt = new System.Windows.Forms.Button();
            this.buttonClearEncryptBox = new System.Windows.Forms.Button();
            this.buttonCopyEncryptToClipboard = new System.Windows.Forms.Button();
            this.buttonSaveFile = new System.Windows.Forms.Button();
            this.richtextboxDecrypted = new System.Windows.Forms.RichTextBox();
            this.richtextboxEncrypted = new System.Windows.Forms.RichTextBox();
            this.PasswordControl = new System.Windows.Forms.FlowLayoutPanel();
            this.labelPassword = new System.Windows.Forms.Label();
            this.textboxPassword = new System.Windows.Forms.TextBox();
            this.EncryptionReaderControl = new System.Windows.Forms.OpenFileDialog();
            this.EncryptionWriterControl = new System.Windows.Forms.SaveFileDialog();
            this.StatusBarControl.SuspendLayout();
            this.ControlMain.SuspendLayout();
            this.DecryptedControls.SuspendLayout();
            this.EncryptedControls.SuspendLayout();
            this.PasswordControl.SuspendLayout();
            this.SuspendLayout();
            // 
            // StatusBarControl
            // 
            this.StatusBarControl.ColumnCount = 1;
            this.StatusBarControl.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.StatusBarControl.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.StatusBarControl.Controls.Add(this.ControlMain, 0, 0);
            this.StatusBarControl.Controls.Add(this.PasswordControl, 0, 1);
            this.StatusBarControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.StatusBarControl.Location = new System.Drawing.Point(0, 0);
            this.StatusBarControl.Name = "StatusBarControl";
            this.StatusBarControl.RowCount = 2;
            this.StatusBarControl.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 91.79601F));
            this.StatusBarControl.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 8.203991F));
            this.StatusBarControl.Size = new System.Drawing.Size(968, 451);
            this.StatusBarControl.TabIndex = 0;
            // 
            // ControlMain
            // 
            this.ControlMain.BackColor = System.Drawing.SystemColors.ControlDarkDark;
            this.ControlMain.ColumnCount = 3;
            this.ControlMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 38.14969F));
            this.ControlMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 23.49272F));
            this.ControlMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 38.35759F));
            this.ControlMain.Controls.Add(this.DecryptedControls, 0, 1);
            this.ControlMain.Controls.Add(this.EncryptedControls, 2, 1);
            this.ControlMain.Controls.Add(this.richtextboxDecrypted, 0, 0);
            this.ControlMain.Controls.Add(this.richtextboxEncrypted, 2, 0);
            this.ControlMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.ControlMain.Location = new System.Drawing.Point(3, 3);
            this.ControlMain.Name = "ControlMain";
            this.ControlMain.RowCount = 2;
            this.ControlMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 91.15479F));
            this.ControlMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 8.845209F));
            this.ControlMain.Size = new System.Drawing.Size(962, 408);
            this.ControlMain.TabIndex = 0;
            // 
            // DecryptedControls
            // 
            this.DecryptedControls.Controls.Add(this.buttonEncrypt);
            this.DecryptedControls.Controls.Add(this.buttonClearDecryptBox);
            this.DecryptedControls.Controls.Add(this.buttonCopyDecryptToClipboard);
            this.DecryptedControls.Controls.Add(this.buttonReadFile);
            this.DecryptedControls.Dock = System.Windows.Forms.DockStyle.Fill;
            this.DecryptedControls.Location = new System.Drawing.Point(3, 374);
            this.DecryptedControls.Name = "DecryptedControls";
            this.DecryptedControls.Size = new System.Drawing.Size(361, 31);
            this.DecryptedControls.TabIndex = 0;
            // 
            // buttonEncrypt
            // 
            this.buttonEncrypt.AutoSize = true;
            this.buttonEncrypt.Location = new System.Drawing.Point(3, 3);
            this.buttonEncrypt.Name = "buttonEncrypt";
            this.buttonEncrypt.Size = new System.Drawing.Size(75, 23);
            this.buttonEncrypt.TabIndex = 0;
            this.buttonEncrypt.Text = "Encrypt";
            this.buttonEncrypt.UseVisualStyleBackColor = true;
            this.buttonEncrypt.Click += new System.EventHandler(this.buttonEncrypt_Click);
            // 
            // buttonClearDecryptBox
            // 
            this.buttonClearDecryptBox.AutoSize = true;
            this.buttonClearDecryptBox.Location = new System.Drawing.Point(84, 3);
            this.buttonClearDecryptBox.Name = "buttonClearDecryptBox";
            this.buttonClearDecryptBox.Size = new System.Drawing.Size(75, 23);
            this.buttonClearDecryptBox.TabIndex = 2;
            this.buttonClearDecryptBox.Text = "Clear";
            this.buttonClearDecryptBox.UseVisualStyleBackColor = true;
            this.buttonClearDecryptBox.Click += new System.EventHandler(this.buttonClearDecryptBox_Click);
            // 
            // buttonCopyDecryptToClipboard
            // 
            this.buttonCopyDecryptToClipboard.AutoSize = true;
            this.buttonCopyDecryptToClipboard.Location = new System.Drawing.Point(165, 3);
            this.buttonCopyDecryptToClipboard.Name = "buttonCopyDecryptToClipboard";
            this.buttonCopyDecryptToClipboard.Size = new System.Drawing.Size(104, 23);
            this.buttonCopyDecryptToClipboard.TabIndex = 3;
            this.buttonCopyDecryptToClipboard.Text = "Copy To Clipboard";
            this.buttonCopyDecryptToClipboard.UseVisualStyleBackColor = true;
            this.buttonCopyDecryptToClipboard.Click += new System.EventHandler(this.buttonCopyDecryptToClipboard_Click);
            // 
            // buttonReadFile
            // 
            this.buttonReadFile.AutoSize = true;
            this.buttonReadFile.Location = new System.Drawing.Point(275, 3);
            this.buttonReadFile.Name = "buttonReadFile";
            this.buttonReadFile.Size = new System.Drawing.Size(75, 23);
            this.buttonReadFile.TabIndex = 4;
            this.buttonReadFile.Text = "Read File";
            this.buttonReadFile.UseVisualStyleBackColor = true;
            this.buttonReadFile.Click += new System.EventHandler(this.buttonReadFile_Click);
            // 
            // EncryptedControls
            // 
            this.EncryptedControls.Controls.Add(this.buttonDecrypt);
            this.EncryptedControls.Controls.Add(this.buttonClearEncryptBox);
            this.EncryptedControls.Controls.Add(this.buttonCopyEncryptToClipboard);
            this.EncryptedControls.Controls.Add(this.buttonSaveFile);
            this.EncryptedControls.Dock = System.Windows.Forms.DockStyle.Fill;
            this.EncryptedControls.Location = new System.Drawing.Point(595, 374);
            this.EncryptedControls.Name = "EncryptedControls";
            this.EncryptedControls.Size = new System.Drawing.Size(364, 31);
            this.EncryptedControls.TabIndex = 1;
            // 
            // buttonDecrypt
            // 
            this.buttonDecrypt.AutoSize = true;
            this.buttonDecrypt.Location = new System.Drawing.Point(3, 3);
            this.buttonDecrypt.Name = "buttonDecrypt";
            this.buttonDecrypt.Size = new System.Drawing.Size(75, 23);
            this.buttonDecrypt.TabIndex = 0;
            this.buttonDecrypt.Text = "Decrypt";
            this.buttonDecrypt.UseVisualStyleBackColor = true;
            this.buttonDecrypt.Click += new System.EventHandler(this.buttonDecrypt_Click);
            // 
            // buttonClearEncryptBox
            // 
            this.buttonClearEncryptBox.AutoSize = true;
            this.buttonClearEncryptBox.Location = new System.Drawing.Point(84, 3);
            this.buttonClearEncryptBox.Name = "buttonClearEncryptBox";
            this.buttonClearEncryptBox.Size = new System.Drawing.Size(75, 23);
            this.buttonClearEncryptBox.TabIndex = 1;
            this.buttonClearEncryptBox.Text = "Clear";
            this.buttonClearEncryptBox.UseVisualStyleBackColor = true;
            this.buttonClearEncryptBox.Click += new System.EventHandler(this.buttonClearEncryptBox_Click);
            // 
            // buttonCopyEncryptToClipboard
            // 
            this.buttonCopyEncryptToClipboard.AutoSize = true;
            this.buttonCopyEncryptToClipboard.Location = new System.Drawing.Point(165, 3);
            this.buttonCopyEncryptToClipboard.Name = "buttonCopyEncryptToClipboard";
            this.buttonCopyEncryptToClipboard.Size = new System.Drawing.Size(104, 23);
            this.buttonCopyEncryptToClipboard.TabIndex = 2;
            this.buttonCopyEncryptToClipboard.Text = "Copy To Clipboard";
            this.buttonCopyEncryptToClipboard.UseVisualStyleBackColor = true;
            this.buttonCopyEncryptToClipboard.Click += new System.EventHandler(this.buttonCopyEncryptToClipboard_Click);
            // 
            // buttonSaveFile
            // 
            this.buttonSaveFile.AutoSize = true;
            this.buttonSaveFile.Location = new System.Drawing.Point(275, 3);
            this.buttonSaveFile.Name = "buttonSaveFile";
            this.buttonSaveFile.Size = new System.Drawing.Size(75, 23);
            this.buttonSaveFile.TabIndex = 3;
            this.buttonSaveFile.Text = "Save File";
            this.buttonSaveFile.UseVisualStyleBackColor = true;
            this.buttonSaveFile.Click += new System.EventHandler(this.buttonSaveFile_Click);
            // 
            // richtextboxDecrypted
            // 
            this.richtextboxDecrypted.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richtextboxDecrypted.Location = new System.Drawing.Point(3, 3);
            this.richtextboxDecrypted.Name = "richtextboxDecrypted";
            this.richtextboxDecrypted.ShortcutsEnabled = false;
            this.richtextboxDecrypted.Size = new System.Drawing.Size(361, 365);
            this.richtextboxDecrypted.TabIndex = 2;
            this.richtextboxDecrypted.Text = "Write what you want here then you can encrypt it by pressing the Encrypt button b" +
                "elow. Also once your data is Encrypted you can save it to file for future loadin" +
                "g :-) Happy Codeing!!";
            this.richtextboxDecrypted.ZoomFactor = 2F;
            // 
            // richtextboxEncrypted
            // 
            this.richtextboxEncrypted.BackColor = System.Drawing.SystemColors.ActiveBorder;
            this.richtextboxEncrypted.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richtextboxEncrypted.Location = new System.Drawing.Point(595, 3);
            this.richtextboxEncrypted.Name = "richtextboxEncrypted";
            this.richtextboxEncrypted.ReadOnly = true;
            this.richtextboxEncrypted.ShortcutsEnabled = false;
            this.richtextboxEncrypted.Size = new System.Drawing.Size(364, 365);
            this.richtextboxEncrypted.TabIndex = 3;
            this.richtextboxEncrypted.Text = "";
            this.richtextboxEncrypted.ZoomFactor = 2F;
            this.richtextboxEncrypted.TextChanged += new System.EventHandler(this.richtextboxEncryptedText_TextChanged);
            this.richtextboxEncrypted.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richtextboxEncryptedText_KeyDown);
            this.richtextboxEncrypted.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richtextboxEncryptedText_KeyPress);
            // 
            // PasswordControl
            // 
            this.PasswordControl.Controls.Add(this.labelPassword);
            this.PasswordControl.Controls.Add(this.textboxPassword);
            this.PasswordControl.Location = new System.Drawing.Point(3, 417);
            this.PasswordControl.Name = "PasswordControl";
            this.PasswordControl.Size = new System.Drawing.Size(364, 31);
            this.PasswordControl.TabIndex = 1;
            // 
            // labelPassword
            // 
            this.labelPassword.AutoSize = true;
            this.labelPassword.Location = new System.Drawing.Point(3, 0);
            this.labelPassword.Name = "labelPassword";
            this.labelPassword.Size = new System.Drawing.Size(53, 13);
            this.labelPassword.TabIndex = 0;
            this.labelPassword.Text = "Password";
            // 
            // textboxPassword
            // 
            this.textboxPassword.Location = new System.Drawing.Point(62, 3);
            this.textboxPassword.Name = "textboxPassword";
            this.textboxPassword.PasswordChar = '#';
            this.textboxPassword.Size = new System.Drawing.Size(291, 20);
            this.textboxPassword.TabIndex = 1;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(968, 451);
            this.Controls.Add(this.StatusBarControl);
            this.Name = "Form1";
            this.StatusBarControl.ResumeLayout(false);
            this.ControlMain.ResumeLayout(false);
            this.DecryptedControls.ResumeLayout(false);
            this.DecryptedControls.PerformLayout();
            this.EncryptedControls.ResumeLayout(false);
            this.EncryptedControls.PerformLayout();
            this.PasswordControl.ResumeLayout(false);
            this.PasswordControl.PerformLayout();
            this.ResumeLayout(false);

        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        public class TripleDES_EncryptionDecryption
        {
            public static string EncryptTripleDES(string Data, string Password)
            {
                byte[] clearBytes = Encoding.Unicode.GetBytes(Data);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
                   new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });

                MemoryStream ms = new MemoryStream();

                TripleDES tsp = TripleDESCryptoServiceProvider.Create();
                tsp.Key = pdb.GetBytes(16);
                tsp.IV = pdb.GetBytes(8);


                CryptoStream cs = new CryptoStream(ms, tsp.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();


                byte[] encryptedData = ms.ToArray();
                return Convert.ToBase64String(encryptedData);
            }

            public static string DecryptTripleDES(string Data, string Password)
            {
                byte[] clearBytes = Convert.FromBase64String(Data);


                PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,


                    new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });
                MemoryStream ms = new MemoryStream();

                TripleDES tsp = TripleDESCryptoServiceProvider.Create();
                tsp.Key = pdb.GetBytes(16);

                tsp.IV = pdb.GetBytes(8);
                CryptoStream cs = new CryptoStream(ms, tsp.CreateDecryptor(), CryptoStreamMode.Write);

                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
                byte[] decryptedData = ms.ToArray();
                return Encoding.Unicode.GetString(decryptedData);
            }
        }

        private void richtextboxEncryptedText_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void richtextboxEncryptedText_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
        }

        private void richtextboxEncryptedText_TextChanged(object sender, EventArgs e)
        {
            EncryptedData = richtextboxEncrypted.Text;
        }

        private void buttonSaveFile_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(EncryptedData))
            {
                if (EncryptionWriterControl.ShowDialog() == DialogResult.Cancel)
                    return;
                using (EncryptionWriter = new StreamWriter(EncryptionWriterControl.FileName))
                {
                    EncryptionWriter.Write(EncryptedData);
                }
            }
        }

        private void buttonClearDecryptBox_Click(object sender, EventArgs e)
        {
            richtextboxDecrypted.Text = String.Empty;
        }

        private void buttonCopyDecryptToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(richtextboxDecrypted.Text);
        }

        private void buttonReadFile_Click(object sender, EventArgs e)
        {
            if (EncryptionReaderControl.ShowDialog() == DialogResult.Cancel)
                return;
            using (EncryptionReader = new StreamReader(EncryptionReaderControl.FileName))
            {
                richtextboxEncrypted.Text = EncryptionReader.ReadToEnd();
            }
        }

        private void buttonDecrypt_Click(object sender, EventArgs e)
        {
            richtextboxDecrypted.Text = TripleDES_EncryptionDecryption.DecryptTripleDES(richtextboxEncrypted.Text, textboxPassword.Text);
        }

        private void buttonClearEncryptBox_Click(object sender, EventArgs e)
        {
            richtextboxEncrypted.Text = String.Empty;
        }

        private void buttonCopyEncryptToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(richtextboxEncrypted.Text);
        }

        private void buttonEncrypt_Click(object sender, EventArgs e)
        {
            richtextboxEncrypted.Text = TripleDES_EncryptionDecryption.EncryptTripleDES(richtextboxDecrypted.Text, textboxPassword.Text);
        }
    }
}

Edited by CsharpChico: n/a

-1

write thise code on Encrypt button

byte[] encData_byte = new byte[textBox1.Text.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(textBox1.Text.Trim());
            textBox2.Text = Convert.ToBase64String(encData_byte);

and write this code on decript the same

System.Text.UTF8Encoding Encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder Decoder = Encoder.GetDecoder();
            byte[] todecode_byte=Convert.FromBase64String(textBox2.Text.Trim());
            int charCount=Decoder.GetCharCount(todecode_byte,0,todecode_byte.Length);
            char[] decode_char=new char[charCount];
            Decoder.GetChars(todecode_byte,0,todecode_byte.Length,decode_char,0);
            textBox3.Text=new string(decode_char);

it will work not no so secured

and use following NameSpaces

using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

Best Of Luck

Votes + Comments
Op asked about TripleDES Encryption and Decryption
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.