[加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
2013-09-24 08:28
302 查看
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#中RSA加密解密和签名与验证的实现
- DES ECB模式JAVA PHP C#实现加密、解密兼容
- C#实现256位AES加密和解密
- 不对称加密和解密,用C#实现
- C#中RSA加密解密和签名与验证的实现
- C#实现DES加密解密,AES加密解密
- 用C#实现Base64处理,加密解密,编码解码
- 用C#实现字符串的加密解密操作
- C#实现RSA加密解密
- C#实现AES加密和解密函数
- c#使用SHA256算法实现对文件的加密和解密
- javascript AES加密 C#AES解密实现
- C#实现AES加密和解密函数原代码
- (转)使用Vernam(维尔南/弗纳姆)算法实现文件加密解密[C#]
- C#使用DES和AES实现加密解密功能示例
- C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩
- C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
- ASP.NET -DEC通过密匙实现加密/解密
- c#实现文件加密、解密及文件拖拽至程序图标直接打开(转)