转载

[加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)

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 Vigenere
{
    public partial class Form1 : Form
    {
        private string[,] matrix = new string[26, 26];
        private ASCIIEncoding ascii = new ASCIIEncoding();

        //key
        private string key;
        //code
        private string code;
        //text
        private string text;

        public Form1()
        {
            InitializeComponent();
            #region Generate Virginia Martix
            for (int i = 0; i < 26; i++)
            {
                for (int j = 0; j < 26; j++)
                {
                    int number = 65 + i + j;
                    if (number > 90)
                    {
                        number -= 26;
                    }
                    byte[] bt = new byte[] { (byte)number };
                    matrix[i, j] = ascii.GetString(bt);
                }
            }
            #endregion
        }
        //加密
        private void button1_Click(object sender, EventArgs e)
        {
            key = this.txtKey.Text.ToString().ToUpper();
            code = "";
            text = this.txtText.Text.ToString().ToUpper();
            List<int> keyNum = new List<int>(); ;

            for (int i = 0; i < key.Length; i++)
            {
                string str = key.Substring(i, 1);
                keyNum.Add((int)ascii.GetBytes(str)[0] - 65);
            }

            int index = -1;
            for (int i = 0; i < this.text.Length; i++)
            {
                if (this.text.Substring(i, 1).ToString() == " ")
                {
                    code += " ";
                    continue;
                }
                index++;
                code += matrix[keyNum[index % key.Length], (int)ascii.GetBytes(this.text.Substring(i, 1))[0] - 65];
            }

            this.txtCode.Text = code.ToString();
        }
        //解密
        private void button2_Click(object sender, EventArgs e)
        {
            key = this.txtKey.Text.ToString().ToUpper();
            code = this.txtCode.Text.ToString().ToUpper();
            text = "";
            List<int> keyNum = new List<int>(); ;

            for (int i = 0; i < key.Length; i++)
            {
                string str = key.Substring(i, 1);
                keyNum.Add((int)ascii.GetBytes(str)[0] - 65);
            }

            int index = -1;
            for (int i = 0; i < this.code.Length; i++)
            {
                if (this.code.Substring(i, 1).ToString() == " ")
                {
                    text += " ";
                    continue;
                }
                index++;

                for (int j = 0; j < 26; j++)
                {
                    if (this.code.Substring(i, 1).ToString() == matrix[keyNum[index % key.Length], j])
                    {
                        byte[] bt = new byte[] { (byte)(j + 65) };
                        text += ascii.GetString(bt);
                    }
                }
            }

            this.txtText.Text = text.ToString();
        }
    }
}


 

对于维吉尼亚方阵及运用维吉尼亚方阵的加密与解密,可参考http://baike.baidu.com/view/270838.htm?fromTaglist

画面结果如下:

 

转自:http://www.cnblogs.com/coding4love/p/3296703.html
文章最后发布于: 2013-09-24 08:28:58
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览