Need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need help

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Need help

    Ok so i want to write a windows form in c# that encrypts and/or decrypts words/sentence/letters with the cesar shift encryption, i think i already have some good coding but i don't really know where to put what for the form programming, when i execute it and start writing something to encrypt/decrypt it just freezes after the first letter i enter

    public partial class Encrypt : Form
    {
    public Encrypt()
    {
    InitializeComponent();
    }
    public string phrase;
    public char legend;
    public char val;
    public int key;
    public string encryptedstring;
    static public char EncryptChar(int key, char val, char[] legend)
    {
    int i = Array.IndexOf(legend, val) + key;
    if (i >= legend.Length)
    {
    i -= legend.Length;
    }
    return legend[i];
    }
    static public char DecryptChar(int key, char val, char[] legend)
    {
    int i = Array.IndexOf(legend, val) - key;
    if (i < 0)
    {
    i += legend.Length;
    }
    return legend[i];
    }
    private void txtPhrase_TextChanged(object sender, EventArgs e)
    {
    phrase = Convert.ToString(Console.ReadLine());
    }

    private void btnEncrypt_Click(object sender, EventArgs e)
    {
    char[] legend = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    int i = Array.IndexOf(legend, val) + key;
    if (i >= legend.Length)
    {
    i -= legend.Length;
    }
    string stringtoencrypt = phrase;
    string encryptedstring = "";
    foreach (char dd in stringtoencrypt.ToCharArray())
    {
    encryptedstring += EncryptChar(3, dd, legend);
    }
    lblPhrase.Text = ("Encryption: " + encryptedstring);
    lblPhrase.Visible = true;
    }

    private void btnDecrypt_Click(object sender, EventArgs e)
    {
    char[] legend = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    int i = Array.IndexOf(legend, val) - key;
    if (i < 0)
    {
    i += legend.Length;
    }
    string stringtoencrypt = phrase;
    string decryptedstring = "";
    foreach (char dd in encryptedstring.ToCharArray())
    {
    decryptedstring += DecryptChar(3, dd, legend);
    }
    lblPhrase.Text = ("Decryption: " + decryptedstring);
    lblPhrase.Visible = true;
    }

    private void lblPhrase_Click(object sender, EventArgs e)
    {

    }

    private void btnFin_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }
    }
    }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: Need help

    Hi,

    First, please surround your code snippets with [code] and [/code] tags to preserve formatting and make it easier to read.

    I think your error is found:

    Code:
    private void txtPhrase_TextChanged(object sender, EventArgs e)
    {
        phrase = Convert.ToString(Console.ReadLine());
    }
    I'm not sure what you're trying to do here, but anytime you change txtPhrase, it will prompt the console (the black command line window, which isn't being displayed when you're running a WinForms program. So, when you change txtPhrase, it waits for console input that will never come thus the hang.

    You have two options. If you want to leave your design as it is, set

    Code:
    phrase = txtPhrase.Text;
    Alternatively you could do away with the class variable phrase altogether, delete the entire method that is causing your program to hang and change every instance of phrase to txtPhrase.Text. This is the most correct option.

    Also, your encryption method could use a little work. In particular, (a) when you're building up a long string you should use StringBuilder instead of adding a lot of strings together, and (b) you can use modular arithmetic to calculate the new positions. To teach-by-example, I've re-written the method:

    Code:
    private void btnEncrypt_Click(object sender, EventArgs e)
    {
        char[] legend = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        
        System.Text.StringBuilder encString = new StringBuilder(txtPhrase.Txt);
        for(int i = 0; i < txtPhrase.Text.Length)
        {
            int indexOfEncryptedChar = (Array.IndexOf(legend, val) + key) &#37; legend.Length;
            encString.Append( legend[indexOfEncryptedChar] );
        }
        
        lblPhrase.Text = ("Encryption: " + encString.ToString());
        lblPhrase.Visible = true;
    }
    Actually, you could do even better by using a Dictionary<char,int> (MSDN link) to avoid the O(n) call to Array.IndexOf(...) and instead get O(1) lookup, but that's sort of beyond the scope of what you're learning right now.

    I hope that helps! You can try modifying the decrypt subroutine if you want to learn how this method works.

    (N.B.: I didn't actually run anything through a compiler; there might be a bug or two, not sure. Hopefully the idea is illustrated).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center