[加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
来源:互联网 发布:中国教育现状知乎 编辑:程序博客网 时间:2024/05/16 08:44
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
- [加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
- 维吉尼亚加密解密
- C#实现维吉尼亚密码的加密和解密
- C#加密与解密
- C# 加密与解密
- C#加密与解密
- C#加密与解密
- c# 加密与解密
- C# 加密与解密
- c#实现DES加密与解密
- 维吉尼亚字符串加密解密小程序
- c#实现RSA加密解密
- C#实现AES加密解密
- C#实现RSA加密解密
- C#实现Base64加密解密
- C#数据加密与解密 (2)
- C#数据加密与解密
- C#加密与解密函数
- gcc如何获取系统头文件路径
- 学习
- 第二章 编译和链接
- js 类型检测
- Select ..for update(nowait)语句在表级别上产生RS锁,在数据行上产生行级锁(是一种X锁)
- [加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
- 内核模式 定时器学习
- c++的句柄(一)
- streaming mr
- test
- 黑马程序员-----C#编程基础
- try-catch-finally返回值
- arcgisserver服务发布时报 Bad login user错误
- 升升不息——给电脑升级的人一些建议