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[, ];
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 = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
int number = + i + j;
if (number > )
{
number -= ;
}
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 = ; i < key.Length; i++)
{
string str = key.Substring(i, );
keyNum.Add((int)ascii.GetBytes(str)[] - );
} int index = -;
for (int i = ; i < this.text.Length; i++)
{
if (this.text.Substring(i, ).ToString() == " ")
{
code += " ";
continue;
}
index++;
code += matrix[keyNum[index % key.Length], (int)ascii.GetBytes(this.text.Substring(i, ))[] - ];
} 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 = ; i < key.Length; i++)
{
string str = key.Substring(i, );
keyNum.Add((int)ascii.GetBytes(str)[] - );
} int index = -;
for (int i = ; i < this.code.Length; i++)
{
if (this.code.Substring(i, ).ToString() == " ")
{
text += " ";
continue;
}
index++; for (int j = ; j < ; j++)
{
if (this.code.Substring(i, ).ToString() == matrix[keyNum[index % key.Length], j])
{
byte[] bt = new byte[] { (byte)(j + ) };
text += ascii.GetString(bt);
}
}
} this.txtText.Text = text.ToString();
}
}
} 对于维吉尼亚方阵及运用维吉尼亚方阵的加密与解密,可参考http://baike.baidu.com/view/270838.htm?fromTaglist 画面结果如下:


随机练习:C#实现维吉尼亚加密与解密(解密前提为已知密匙)的更多相关文章

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

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. [CTF]维吉尼亚密码(维基利亚密码)

    [CTF]维吉尼亚密码(维基利亚密码) ----------------------百度百科 https://baike.baidu.com/item/维吉尼亚密码/4905472?fr=aladdi ...

  3. Vigenère Cipher 维吉尼亚加解密算法

    维吉尼亚的加解密有两种方法. 第一种是查表:第一行为明文,第一列为密钥,剩余的为对应的密文 第二种方法是转化计算法:逐个将字符转化为从零开始的数字,对数字进行加密/解密后,再转化为字符. 本文要用c+ ...

  4. 维吉尼亚密码-攻防世界(shanghai)

    维吉尼亚密码 维吉尼亚密码是使用一系列 凯撒密码 组成密码字母表的加密算法,属于多表密码的一种简单形式. 加密原理 维吉尼亚密码的前身,是我们熟悉的凯撒密码. 凯撒密码的加密方式是依靠一张字母表中的每 ...

  5. python 维吉尼亚

    加密key='COMPUTER' plaintext='BLOCKCIPHERDESIGNPRINCIPLE' ascii='abcdefghijklmnopqrstuvwxyz'.upper() k ...

  6. 维吉尼亚密码java代码实现根据密钥长度计算IC值过程

    package cn.longxuzi; import java.util.Scanner; import org.junit.Test; public class ICUtils { /** * @ ...

  7. 维吉尼亚密码java完整版

    package cn.longxuzi; import org.junit.Test; public class Chi_SquareUtils { private static final ICUt ...

  8. python实现维吉尼亚解密

    # -*-coding:UTF-8-*- from sys import stdout miwen = "KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXR ...

  9. JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

    第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...

随机推荐

  1. linux内核中的##__VA_ARGS__有什么作用?

    答:   1.__VA_ARGS__是一个可变参数宏: 2. 当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错: 3. 示例 3.1 #defin ...

  2. Shell-3-文件之名

    1.生成任意大小的文件 [root@localhost tmp]# dd 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00219263 秒,478 MB/ ...

  3. 基于XML的类的属性的装配

    基于XML的属性装配 1.手动装配 <!-- 属性的装配:手动装配 --> <bean id="userService" class="com.neue ...

  4. 老男孩Day3作业:工资管理系统

    作业需求: 1.从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件. 2.能增查改员工工资 3.增.改员工工资用空格分隔 4.实现退出功能 1)编 ...

  5. atcoder 2579

    You are taking a computer-based examination. The examination consists of N questions, and the score ...

  6. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  7. manjaro linux KDE桌面网易云音乐点击托盘图标无反应

    这是一个很奇怪的问题,它的解决方法更奇怪... 找到网易云,右键,选择“编辑应用程序” 在打开的窗口中选中应用程序一栏 将“命令”的内容替换为: 1 env XDG_CURRENT_DESKTOP=D ...

  8. springcloud系列九 整合Hystrix Dashboard

    Hystrix Dashboard是Hystrix的仪表盘组件,主要用来实时监控Hystrix的各项指标信息,通过界面反馈的信息可以快速发现系统中存在的问题. 整合快速体验: pom.xml(这个是F ...

  9. 21. sessionStorage和localStorage的使用

    sessionStorage和localStorage的使用   前言 这是学习笔记,把从别人博客里转载的https://www.cnblogs.com/wangyue99599/p/9088904. ...

  10. 根据map中某一字段排序

    以上是从小到大的排序...要注意.! 需要jdk8...