-1

试图创建一个简单的替换密码,我的程序工作得很好,但它没有加密或解密数字。我真的不知道我应该在我的代码中添加什么以便它正常工作......任何想法?这是我的代码

namespace yaba
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            string encrypt = tboxIO.Text;
            encrypt.ToLower();


            bool tbNull = tboxIO.Text == "";

            if (tbNull)
                MessageBox.Show("There is nothing to encrypt.");

            else
            {
                char[] array = encrypt.ToCharArray();

                for (int i = 0; i < array.Length; i++)
                {
                    int num = (int)array[i];
                    if (num >= 'a' && num <= 'z')
                    {
                        num += Convert.ToInt32(tbShift.Text);
                        if (num > 'z')
                        {
                            num = num - 26;
                        }
                    }
                    else if (num >= 'A' && num <= 'Z')
                    {
                        num += Convert.ToInt32(tbShift.Text);
                        if (num > 'Z')
                        {
                            num = num - 26;
                        }
                    }
                    array[i] = (char)num;
                }
                lblIO.Text = "Encrypted Message";
                tboxIO.Text = new string(array).ToLower();
            }

            tboxIO.Copy();
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string decrypt = tboxIO.Text;
            decrypt.ToLower();

            bool tbNull = tboxIO.Text == "";

            if (tbNull)
                MessageBox.Show("There is nothing to decrypt.");

            else
            {
                char[] array = decrypt.ToCharArray();
                for (int i = 0; i < array.Length; i++)
                {
                    int num = (int)array[i];
                    if (num >= 'a' && num <= 'z')
                    {
                        num -= Convert.ToInt32(tbShift.Text);
                        if (num > 'z')
                            num = num - 26;

                        if (num < 'a')
                            num = num + 26;
                    }
                    else if (num >= 'A' && num <= 'Z')
                    {
                        num -= Convert.ToInt32(tbShift.Text);
                        if (num > 'Z')
                            num = num - 26;

                        if (num < 'A')
                            num = num + 26;
                    }
                    array[i] = (char)num;
                }
                lblIO.Text = "Decrypted Message";
                tboxIO.Text = new string(array).ToUpper();
            }

            tboxIO.Copy();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hehe");
        }

        private void tboxIO_MouseClick(object sender, MouseEventArgs e)
        {
            tboxIO.SelectAll();
            tboxIO.Copy();
        }

        private void tbShift_MouseClick(object sender, MouseEventArgs e)
        {
            tbShift.SelectAll();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


    }
}
4

3 回答 3

1

那么,你想通过什么逻辑来加密数字?他们也应该被转移Convert.ToInt32(tbShift.Text)时间吗?然后你可以添加另一个else if(num >= '0' && num <= '9')做同样的事情(用 +/- 10 而不是 +/- 26)。

不过,您需要注意,您可能会转两次甚至三次,具体取决于您想要转移多少。我希望你以某种方式确保tbShift.Textcan only(!) 的数字不高于 26。

但是需要知道你想如何加密/解密数字并告诉我们,这样我们甚至有机会帮助你。

于 2013-03-04T07:02:03.170 回答
1

我像这样更改了您的代码。你不应该使用 ToLower。

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string myText = "MusLum";
        string encrypted = "";
        string decrypted = "";
        char shift = 'a';
        public Form1()
        {
            InitializeComponent();
        }

        private void Encrypt_Click(object sender, EventArgs e)
        {
            string encrypt = myText;

            bool tbNull = myText == "";

            if (tbNull)
                MessageBox.Show("There is nothing to encrypt.");

            else
            {
                char[] array = encrypt.ToCharArray();

                for (int i = 0; i < array.Length; i++)
                {
                    int num = (int)array[i];
                    if (num >= 'a' && num <= 'z')
                    {

                        num += (Convert.ToInt32(shift.ToString().ToLower()[0]) - Convert.ToInt32('a')+1);

                        if (num > 'z')
                        {
                            num = num - 26;
                        }
                    }
                    else if (num >= 'A' && num <= 'Z')
                    {
                        num += (Convert.ToInt32(shift.ToString().ToUpper()[0]) - Convert.ToInt32('A')+1);

                        if (num > 'Z')
                        {
                            num = num - 26;
                        }
                    }
                    array[i] = (char)num;
                }

                encrypted = new string(array);
            }


        }

        private void Decrypt_Click(object sender, EventArgs e)
        {
            string decrypt = encrypted;


            bool tbNull = encrypted == "";

            if (tbNull)
                MessageBox.Show("There is nothing to decrypt.");

            else
            {
                char[] array = encrypted.ToCharArray();
                for (int i = 0; i < array.Length; i++)
                {
                    int num = (int)array[i];
                    if (num >= 'a' && num <= 'z')
                    {
                        num -= (Convert.ToInt32(shift.ToString().ToLower()[0]) - Convert.ToInt32('a')+1);

                        if (num > 'z')
                            num = num - 26;

                        if (num < 'a')
                            num = num + 26;
                    }
                    else if (num >= 'A' && num <= 'Z')
                    {
                        num -= (Convert.ToInt32(shift.ToString().ToUpper()[0]) - Convert.ToInt32('A')+1);

                        if (num > 'Z')
                            num = num - 26;

                        if (num < 'A')
                            num = num + 26;
                    }
                    array[i] = (char)num;
                }

                decrypted = new string(array);
            }
        }
    }
}
于 2013-03-04T07:13:57.917 回答
0

据我所知,您明确表示您只想加密大写和小写字母,所以不,数字没有加密。

此外(不相关)您对 ToLower 的调用是没有回报的,因为 ToLower 是一个函数,并且您没有将结果分配给任何东西。

于 2013-03-04T06:26:47.010 回答