65.9K
CodeProject 正在变化。 阅读更多。
Home

简单加密演示

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.24/5 (8投票s)

2016 年 6 月 29 日

CPOL

2分钟阅读

viewsIcon

18884

downloadIcon

575

一个简单的公钥密码学和字符替换加密方法的演示。

引言

这篇文章(我的第一篇)是一个公钥密码学和字符替换加密方法的简单演示。此外,GUI演示了如何在RichTextBox中选择和着色文本、线程以及从不在其创建的线程中引用GUI控件。

代码中使用的技术不适用于任何生产环境。这仅仅是一个学习工具。

背景

这是一个我在操作系统 - 安全课程中完成的作业项目。

我使用了我在维基百科 (https://en.wikipedia.org/wiki/RSA_(cryptosystem)) 和 CodeProject 网站上找到的代码。Visual Studio 2015 用于构建该项目。

使用代码

要使用演示应用程序,您可以打开一个纯文本文件或在输入框中输入您自己的纯文本。如果您选择“表格”而不是“公钥”,请注意只有一部分非字母数字字符将被加密。如果您想处理更多特殊字符,只需将它们添加到unencrypted_char_tableencryption_table'.

中。选择“加密”或“解密”选项。单击“下一步”以单步执行每个字符。或者单击“运行”以观看过程,“停止”以停止运行。“重置”以从头开始。

如果您想将加密的输出保存到文件,请单击“另存为”按钮。您可以在当前会话中稍后重新加载保存的文件进行解密。请注意,每次运行程序时都会生成一组新的密钥。

“第一个质数”和“第二个质数”选择是用于密钥生成的两个不同的“随机”质数。这些值被故意保持较小,以便可以使用简单的计算器来重现结果。在现实世界的公钥加密中,使用非常大的质数。

加密很简单且有注释。

        //-----------------------------------------------------------------------------------
        // Encrypt a single character
        //-----------------------------------------------------------------------------------
        private string Encrypt(string _inChar)
        {
            string _out = "\n";

            // public key encryption?
            if (radioButton_public_key.Checked)
            {
                input_p = _inChar[0];
                BigInteger _mod = BigInteger.ModPow(input_p, public_key_e, public_key_n);
                return _mod.ToString("00000");
            }

            // currently encrypting digits?
            if (unencoded_digits)
            {
                // we are currently processing unencrypted digits
                // is the input character a digit?
                if (digits_table.Contains(_inChar))
                {
                    // yes, return the digit character
                    _out = _inChar;
                }
                else
                {
                    // no, the input character is not a digit - we have reached the end of the digit sequence
                    unencoded_digits = false;
                    // end the unencoded digits sequence
                    _out = "^^";
                    // is this a normally unencrypted character?
                    if (unencrypted_char_table.Contains(_inChar))
                    {
                        // yes, add the character to the output
                        _out += _inChar;
                    }
                    // it's not a normally unencrypted character - should this character be encrypted?
                    else if (encryption_table.ContainsKey(_inChar.ToString()))
                    {
                        // yes, lookup cipher character in the dictionary
                        _out += encryption_table[_inChar].ToString();
                    }
                }
            }
            else  // not currently encrypting digits
            {
                // is the current character a digit?
                if (digits_table.Contains(_inChar))
                {
                    // begin the unencoded digit sequence
                    unencoded_digits = true;
                    _out = "^^" + _inChar;
                }
                // is this a normally unencrypted character?
                else if (unencrypted_char_table.Contains(_inChar))
                {
                    _out = _inChar;
                }
                // should this character be encrypted?
                else if (encryption_table.ContainsKey(_inChar.ToString()))
                {
                    // lookup cipher character(s) in the dictionary
                    _out = encryption_table[_inChar].ToString();
                }
            }
            if (unencoded_digits)
            {
                // is the digit the last character in the input?
                string _inText = GetControlProperty(richTextBox_input, "Text").ToString();
                // peek into the future
                if (inputCursor + _inChar.Length >= _inText.Length)
                {
                    // yes, it is the last character
                    _out += "^^";
                }
            }
            return _out;
        }

解密也有类似的注释。

        //-----------------------------------------------------------------------------------
        // Decrypt a single character or a pair of characters
        //-----------------------------------------------------------------------------------
        private string Decrypt(ref string _inChar)
        {
            string _out = string.Empty;

            // public key encryption?
            if (radioButton_public_key.Checked)
            {
                if (_inChar == "\n")
                    return _inChar;

                input_p = BigInteger.Parse(_inChar);
                char _mod = (char)BigInteger.ModPow(input_p, private_key, public_key_n);
                return _mod.ToString();
            }

            // is the input character a digit 0 - 9?
            if (digits_table.Contains(_inChar[0]))
            {
                // are we processing unencoded digits?
                if (unencoded_digits)
                {
                    // yes, unencoded digits
                    _inChar = _inChar.Substring(0, 1);
                    return _inChar;
                }
                else
                {
                    // no, these are cipher digits
                    // get two characters
                    // parse the ASCII number
                    int _cipherNumber;
                    if (int.TryParse(_inChar.Substring(0, 2), out _cipherNumber))
                    {
                        // reverse lookup in dictionary
                        if (encryption_table.ContainsValue(_cipherNumber))
                        {
                            _inChar = _inChar.Substring(0, 2);
                            return encryption_table.FirstOrDefault(x => x.Value == _cipherNumber).Key.ToString();
                        }
                    }
                }
            }
            // is this a normally unencrypted character?
            if (unencrypted_char_table.Contains(_inChar[0]))
            {
                // unencoded character
                _inChar = _inChar.Substring(0, 1);
                return _inChar;
            }
            // is this the enclosure for unencrypted digits?
            else if (_inChar[0] == '^')
            {
                // see if there is another carat
                if (_inChar.Substring(0, 2) == "^^")
                {
                    // flip the unencoded state
                    unencoded_digits = !unencoded_digits;
                    // discard the first two carats, return the third character
                    // skip over the first two carats
                    return _inChar.Substring(2, 1);
                }
            }

            _inChar = _inChar.Substring(0, 1);
            return _inChar;
        }
    

 

历史

版本 1.0

© . All rights reserved.