Click here to Skip to main content
15,357,358 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using a .txt file as input from network folder Pin
CaptainJack68310-Jul-14 3:32
MemberCaptainJack68310-Jul-14 3:32 
GeneralNeed Extension methods as mentioned below Pin
prasanna_durai9-Jul-14 7:09
Memberprasanna_durai9-Jul-14 7:09 
GeneralRe: Need Extension methods as mentioned below Pin
Matt T Heffron9-Jul-14 7:38
professionalMatt T Heffron9-Jul-14 7:38 
GeneralRe: Need Extension methods as mentioned below Pin
OriginalGriff9-Jul-14 8:11
mveOriginalGriff9-Jul-14 8:11 
QuestionSplit tag xml in more file xml, with C# Pin
Federico Barbieri8-Jul-14 22:50
MemberFederico Barbieri8-Jul-14 22:50 
SuggestionRe: Split tag xml in more file xml, with C# Pin
Richard MacCutchan8-Jul-14 23:20
mveRichard MacCutchan8-Jul-14 23:20 
AnswerRe: Split tag xml in more file xml, with C# Pin
Richard Deeming9-Jul-14 2:05
mveRichard Deeming9-Jul-14 2:05 
QuestionAdding code to encrypt and decrypt text in textbox with C# 2008 Pin
KaKoten8-Jul-14 21:29
MemberKaKoten8-Jul-14 21:29 
 i'am want to encrypt and decrypt text in textboxt.text in second form but what to adding the code, this is the code. thanks 

Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;

namespace Rc5_v2
{
    static class Program
    {
        /// summary
        /// The main entry point for the application.
        /// summary
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Main_form());
        }
    }
}

RC5.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Web;


namespace Rc5_v2
{
    class Rc5
    {
        uint[] s;
        uint[] l;
        uint b, u, t, c;
        byte[] key;
        int rounds;

        public Rc5()
        {
            string str = "ModifyRC5";
            key = GetKeyFromString(str);
            rounds = 16;
            b = (uint)key.Length;
            u = 4;
            t = (uint)(34);
            c = 12 / u;
            s = new uint[34];
            l = new uint[12];

            GenerateKey(key, rounds);
        }
        
        public Rc5(string password,int round)
        {
            key = GetKeyFromString(password);
            rounds = round;
            b = (uint)key.Length;
            u = 4;
            t = (uint)(2 * rounds + 2);
            c = Math.Max(b, 1) / u;
            s = new uint[2 * rounds + 2];
            l = new uint[key.Length];

            GenerateKey(key, rounds);
        }
        
        public Rc5(byte[] password, int round)
        {
            rounds = round;
            key = password;
            b = (uint)password.Length;
            u = 4;
            t = (uint)(2 * rounds + 2);
            c = Math.Max(b, 1) / u;
            s = new uint[2 * rounds + 2];
            l = new uint[password.Length];

            GenerateKey(key, rounds);
        }
        //to circulate int left
        private uint leftRotate(uint x, int offset)
        {
            uint t1, t2;
            t1 = x >> (32 - offset);
            t2 = x << offset;
            return t1 | t2;
        }
        //to circulate int right
        private uint RightRotate(uint x, int offset)
        {
            uint t1, t2;
            t1 = x << (32 - offset);
            t2 = x >> offset;
            return t1 | t2;
        }
        //encryption operation on two block
        private void Encode(ref uint r1, ref uint r2, int rounds)
        {
            r1 = r1 + s[0];
            r2 = r2 + s[1];
            for (int i = 1; i <= rounds; i++)
            {
                r1 = leftRotate(r1 ^ r2, (int)r2) + s[2 * i];
                r2 = leftRotate(r2 ^ r1, (int)r1) + s[2 * i + 1];
            }
        }
        //decryption operation on two block
        private void Decode(ref uint r1, ref uint r2, int rounds)
        {
            for (int i = rounds; i >= 1; i--)
            {
                r2 = (RightRotate(r2 - s[2 * i + 1], (int)r1)) ^ r1;
                r1 = (RightRotate(r1 - s[2 * i], (int)r2)) ^ r2;
            }
            r2 = r2 - s[1];
            r1 = r1 - s[0];
        }

        private void GenerateKey(byte[] key, int rounds)
        {
            uint P32 = uint.Parse("b7e15163", System.Globalization.NumberStyles.HexNumber);
            uint Q32 = uint.Parse("9e3779b9", System.Globalization.NumberStyles.HexNumber);


            for (int i = key.Length - 1; i >= 0; i--)
            {
                l[i] = leftRotate((uint)i, 8) + key[i];
            }

            s[0] = P32;
            for (int i = 1; i <= t - 1; i++)
            {
                s[i] = s[i - 1] + Q32;
            }

            uint ii, jj;
            ii = jj = 0;
            uint x, y;
            x = y = 0;
            uint v = 3 * Math.Max(t, c);
            //mixing key arrayes
            for (int counter = 0; counter <= v; counter++)
            {
                x = s[ii] = leftRotate((s[ii] + x + y), 3);
                y = l[jj] = leftRotate((l[jj] + x + y), (int)(x + y));
                ii = (ii + 1) % t;
                jj = (jj + 1) % c;
            }
        }
        //convert key from string to byte array
        private byte[] GetKeyFromString(string str)
        {
            char[] mykeyinchar = str.ToCharArray();
            byte[] mykeyinbytes = new byte[mykeyinchar.Length];
            for (int i = 0; i < mykeyinchar.Length; i++)
            {
                mykeyinbytes[i] = (byte)mykeyinchar[i];
            }
            return mykeyinbytes;
        }

        public void Encrypt(FileStream streamreader,FileStream streamwriter)
        {
            uint r1, r2;

            System.IO.BinaryReader br = new System.IO.BinaryReader(streamreader);
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
            long filelength = streamreader.Length;
            while (filelength > 0)
            {
                try
                {
                    r1 = br.ReadUInt32();
                    try
                    {
                        r2 = br.ReadUInt32();
                    }
                    catch
                    {
                        r2 = 0;
                    }
                }
                catch
                {
                    r1 = r2 = 0;
                }

                Encode(ref r1, ref r2, rounds);
                bw.Write(r1);
                bw.Write(r2);

                filelength -= 8;
            }

            streamreader.Close();
            streamwriter.Close();
            
        }

        public void Decrypt(FileStream streamreader,FileStream streamwriter)
        {
            uint r1, r2;

            System.IO.BinaryReader br = new System.IO.BinaryReader(streamreader);
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
            long filelength = streamreader.Length;
            while (filelength > 0)
            {
                try
                {
                    r1 = br.ReadUInt32();
                    r2 = br.ReadUInt32();
                    Decode(ref r1, ref r2, rounds);

                    if (!(r1 == 0 && r2 == 0 && (filelength - 8 <= 0)))
                    {
                        bw.Write(r1);
                        bw.Write(r2);
                    }
                    if (r2 == 0 && (filelength - 8 <= 0))
                    {
                        bw.Write(r1);
                    }
                    filelength -= 8;
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show("May be U try to decrypt an normal file (plain file)", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return;
                }
            }

            streamreader.Close();
            streamwriter.Close();
        }

    }
   

    }

Form4.cs
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.Net.Mail;
using System.Net;

namespace Rc5_v2
{
    public partial class Form4 : Form
    {
        SmtpClient obj_SMTPClient;
        MailMessage Obj_MailMsg;
        Attachment obj_Attachment;
        //int rounds01;
        //string fileName01; 
        //string saveFileName01;
        //string password01;
        
        public Form4()

        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                label3.Text = open.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("Please require, column must be content !!!");
            }
            else
                try
                {
                    obj_SMTPClient = new SmtpClient("smtp.gmail.com");
                    Obj_MailMsg = new MailMessage();
                    obj_Attachment = new System.Net.Mail.Attachment(label3.Text);
                    Obj_MailMsg.From = new MailAddress(textBox1.Text);
                    Obj_MailMsg.To.Add(textBox3.Text);
                    Obj_MailMsg.Body = textBox5.Text;
                    Obj_MailMsg.Attachments.Add(obj_Attachment);
                    Obj_MailMsg.Subject = textBox4.Text;
                    SmtpClient smtps = new SmtpClient("smtp.gmail.com", 587);
                    obj_SMTPClient.Credentials = new NetworkCredential();
                    obj_SMTPClient.Port = 587;
                    obj_SMTPClient.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
                    obj_SMTPClient.EnableSsl = true;
                    obj_SMTPClient.Send(Obj_MailMsg);

                    obj_SMTPClient.EnableSsl = true;
                    obj_SMTPClient.Send(Obj_MailMsg);
                    MessageBox.Show("Message Successful!!!");                   
                }
                catch
                {
                    MessageBox.Show("Request TimeOut!!!");
                }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (Control sayre in this.Controls)
            {
                if (sayre is TextBox)
                {
                    (sayre as TextBox).Clear();
                }
            }       
    }     
     
         
    

        private void button8_Click(object sender, EventArgs e)
        {
            foreach (Control sayre in this.Controls)
            {
                if (sayre is TextBox)
                {
                    (sayre as TextBox).Clear();
                }
            } 
        }

        private void Encrypt_Click(object sender, EventArgs e)
        {

        }

        private void Decrypt_Click(object sender, EventArgs e)
        {

        }

        }
    }


modified 9-Jul-14 20:03pm.

AnswerRe: Adding code to encrypt and decrypt text in textbox with C# 2008 Pin
OriginalGriff8-Jul-14 22:56
mveOriginalGriff8-Jul-14 22:56 
GeneralRe: Adding code to encrypt and decrypt text in textbox with C# 2008 Pin
KaKoten9-Jul-14 13:15
MemberKaKoten9-Jul-14 13:15 
GeneralRe: Adding code to encrypt and decrypt text in textbox with C# 2008 Pin
OriginalGriff9-Jul-14 21:31
mveOriginalGriff9-Jul-14 21:31 
GeneralRe: Adding code to encrypt and decrypt text in textbox with C# 2008 Pin
KaKoten9-Jul-14 23:17
MemberKaKoten9-Jul-14 23:17 
AnswerRe: Adding code to encrypt and decrypt text in textbox with C# 2008 Pin
Eddy Vluggen11-Jul-14 7:19
professionalEddy Vluggen11-Jul-14 7:19 
QuestionC# Questions Pin
shiftwik8-Jul-14 8:32
Membershiftwik8-Jul-14 8:32 
AnswerRe: C# Questions Pin
Eddy Vluggen8-Jul-14 8:48
professionalEddy Vluggen8-Jul-14 8:48 
GeneralRe: C# Questions Pin
shiftwik8-Jul-14 8:56
Membershiftwik8-Jul-14 8:56 
AnswerRe: C# Questions Pin
PIEBALDconsult8-Jul-14 9:08
professionalPIEBALDconsult8-Jul-14 9:08 
AnswerRe: C# Questions Pin
Pete O'Hanlon8-Jul-14 9:19
mvaPete O'Hanlon8-Jul-14 9:19 
GeneralRe: C# Questions Pin
Richard Andrew x648-Jul-14 11:00
professionalRichard Andrew x648-Jul-14 11:00 
GeneralRe: C# Questions Pin
Pete O'Hanlon8-Jul-14 11:16
mvaPete O'Hanlon8-Jul-14 11:16 
GeneralRe: C# Questions Pin
shiftwik8-Jul-14 21:10
Membershiftwik8-Jul-14 21:10 
AnswerRe: C# Questions Pin
OriginalGriff8-Jul-14 9:32
mveOriginalGriff8-Jul-14 9:32 
GeneralRe: C# Questions Pin
shiftwik8-Jul-14 21:09
Membershiftwik8-Jul-14 21:09 
GeneralRe: C# Questions Pin
OriginalGriff8-Jul-14 21:21
mveOriginalGriff8-Jul-14 21:21 
GeneralRe: C# Questions Pin
Erik Westermann11-Jul-14 3:48
professionalErik Westermann11-Jul-14 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.