凯撒的密码. [英] Caesar´s cipher.

查看:99
本文介绍了凯撒的密码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在编写一个程序,其功能是对文本框中给出的气味进行加密和解密.

Hello i´m writing a program whos function is to encrypt and decrypt a scentence given in a textbox.

如果我按加密,则气味会被加密,但是如果我想解密加密的单词,那么我可以将其恢复为原始单词,它将无法正常工作.

If i press encrypt, the scentence do get encrypted, but if i want to decrypt the encrypted word, so i can get it back to the original word, it wont work.

提前感谢,维克多!

这是代码:

 private void Krypteraknapp_Click(object sender, EventArgs e)
        {
            // skapa en variabel för att hålla den inmatade meningen.
            string mening = kryptmeningbox.Text;
            // Sätt meningen till tolower för att returnera sant ifall
            // bokstaven är liten, annars falskt
            mening.ToLower();

            // kolla om textboxen är tom.
            if (kryptmeningbox.Text == "")
                MessageBox.Show("Skriv in något att kryptera");

            else
            {
                // deklarera en array som skall hålla det man skriver in i textboxen
                // mening.
                char[] array = mening.ToCharArray();
                // loopa igenom arrayen.
                for (int i = 0; i < array.Length; i++)
                {
                    // deklarera variabel som motsvarar modifieraren man matar in.
                    int modifierare = (int)array[i];
                    // Sätt gränserna inom alfabetet för modifieraren.
                    if (modifierare >= 'a' && modifierare <= 'ö')
                    {
                        modifierare += Convert.ToInt32(Modifierarebox.Text);
                        if (modifierare > 'ö')
                            modifierare = modifierare - 29;

                        if (modifierare < 'a')
                            modifierare = modifierare + 29;
                    }
                    else if (modifierare >= 'A' && modifierare <= 'ö')
                    {
                        modifierare += Convert.ToInt32(Modifierarebox.Text);
                        if (modifierare > 'Ö')
                            modifierare = modifierare - 29;

                        if (modifierare < 'A')
                            modifierare = modifierare + 29;
                    }
                    array[i] = (char)modifierare;
                }
                kryptmeningbox.Text = new string(array).ToUpper();

            }

        }

        private void dekrypteraknapp_Click(object sender, EventArgs e)
        {
            string mening = kryptmeningbox.Text;
            // Sätt meningen till tolower för att returnera sant ifall
            // bokstaven är liten, annars falskt
            mening.ToLower();

            // kolla om textboxen är tom.
            if (kryptmeningbox.Text == "")
                MessageBox.Show("Skriv in något att kryptera");

            else
            {
                // deklarera en array som skall hålla det man skriver in i textboxen
                // mening.
                char[] array = mening.ToCharArray();
                // loopa igenom arrayen.
                for (int i = 0; i < array.Length; i++)
                {
                    // deklarera variabel som motsvarar modifieraren man matar in.
                    int modifierare = (int)array[i];
                    // Sätt gränserna inom alfabetet för modifieraren.
                    if (modifierare >= 'a' && modifierare <= 'ö')
                    {
                        modifierare += Convert.ToInt32(Modifierarebox.Text);
                        if (modifierare > 'ö')
                            modifierare = modifierare - 29;

                        if (modifierare < 'a')
                            modifierare = modifierare + 29;
                    }
                    else if (modifierare >= 'A' && modifierare <= 'ö')
                    {
                        modifierare += Convert.ToInt32(Modifierarebox.Text);
                        if (modifierare > 'Ö')
                            modifierare = modifierare - 29;

                        if (modifierare < 'A')
                            modifierare = modifierare + 29;
                    }
                    array[i] = (char)modifierare;
                }
                kryptmeningbox.Text = new string(array).ToUpper();

            }
        }
    }
}

推荐答案

您好,我正在编写一个程序,其功能是对文本框中给出的气味进行加密和解密.

Hello i´m writing a program whos function is to encrypt and decrypt a scentence given in a textbox.

如果我按加密,则气味会被加密,但是如果我想解密加密的单词,那么我可以将其恢复为原始单词,它将无法正常工作.

If i press encrypt, the scentence do get encrypted, but if i want to decrypt the encrypted word, so i can get it back to the original word, it wont work.

提前感谢,维克多!

代码如下:

Here is the code:

看起来您的加密和解密例程完全相同.我怀疑这是否适用于凯撒的密码;-)但是有些密码会起作用.

It looks like your encrypt and decrypt routines are exactly the same. I doubt that that works for Caesar's cipher ;-) But there are ciphers where that would work.


这篇关于凯撒的密码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆