使用简单的替换密码加密和解密

时间:2013-03-04 06:01:09

标签: c# cryptography encryption

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

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)
        {

        }


    }
}

3 个答案:

答案 0 :(得分:1)

那么,你想用什么逻辑来加密数字?它们是否应该被Convert.ToInt32(tbShift.Text)次转移?然后,您可以添加另一个else if (num >= '0' && num <= '9')并执行相同的操作(使用+/- 10而不是+/- 26)。

你需要注意的是,你可能会走两到三次,这取决于你想换多少。我希望你以某种方式确保tbShift.Text只能(!)的数字不高于26。

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

答案 1 :(得分: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);
            }
        }
    }
}

答案 2 :(得分:0)

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

另外(无关)你对ToLower的调用是不值得的,因为ToLower是一个函数而你没有将结果分配给任何东西。

相关问题