1public class Class1
2 {
3 public StringBuilder keyWord;
4 public char[] plaintext;
5 public char[] key;
6 public char[][] Vtable;
7 private string p="abcdefghijklmnopqrstuvwxyz";
8 public StringBuilder result;
9 public string[] v=
10 {
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZ","BCDEFGHIJKLMNOPQRSTUVWXYZA",
12 "CDEFGHIJKLMNOPQRSTUVWXYZAB","DEFGHIJKLMNOPQRSTUVWXYZABC",
13 "EFGHIJKLMNOPQRSTUVWXYZABCD","FGHIJKLMNOPQRSTUVWXYZABCDE",
14 "GHIJKLMNOPQRSTUVWXYZABCDEF","HIJKLMNOPQRSTUVWXYZABCDEFG",
15 "IJKLMNOPQRSTUVWXYZABCDEFGH","JKLMNOPQRSTUVWXYZABCDEFGHI",
16 "KLMNOPQRSTUVWXYZABCDEFGHIJ","LMNOPQRSTUVWXYZABCDEFGHIJK",
17 "MNOPQRSTUVWXYZABCDEFGHIJKL","NOPQRSTUVWXYZABCDEFGHIJKLM",
18 "OPQRSTUVWXYZABCDEFGHIJKLMN","PQRSTUVWXYZABCDEFGHIJKLMNO",
19 "QRSTUVWXYZABCDEFGHIJKLMNOP","RSTUVWXYZABCDEFGHIJKLMNOPQ",
20 "STUVWXYZABCDEFGHIJKLMNOPQR","TUVWXYZABCDEFGHIJKLMNOPQRS",
21 "UVWXYZABCDEFGHIJKLMNOPQRST","VWXYZABCDEFGHIJKLMNOPQRSTU",
22 "WXYZABCDEFGHIJKLMNOPQRSTUV","XYZABCDEFGHIJKLMNOPQRSTUVW",
23 "YZABCDEFGHIJKLMNOPQRSTUVWX","ZABCDEFGHIJKLMNOPQRSTUVWXY"};
24
25 public Class1()
26 {
27 keyWord=new StringBuilder();
28 plaintext=new char[100];
29 key=new char[100];
30 Vtable=new char[26][];
31 result=new StringBuilder();
32 for(int i=0;i<v.Length;i++)
33 {
34 Vtable[i]=v[i].ToCharArray();
35 }
36
37 //
38 // TODO: 在此处添加构造函数逻辑
39 //
40 }
41
42 private void GetKey()
43 {
44 int Pnum,KWnum,n,ne;
45 string k,k1,k2;
46 k2=keyWord.ToString();
47 Pnum=plaintext.Length;
48 KWnum=keyWord.Length;
49 if(Pnum>KWnum)
50 {
51 n=Pnum/KWnum;
52 ne=Pnum%KWnum;
53 for(int i=1;i<n;i++)
54 keyWord.Append(k2);
55 k=keyWord.ToString().Substring(0,ne);
56 keyWord.Append(k);
57 k1=keyWord.ToString();
58 key=keyWord.ToString().ToCharArray();
59 }
60 else
61 {
62 key=keyWord.ToString().Substring(0,Pnum).ToCharArray();
63 }
64
65 }
66
67 public void EncryptData(string str,string str2)//str为加密的明文,str2为密钥
68 {
69 int x,y;
70 keyWord.Append(str2);
71 plaintext=str.ToCharArray();
72 GetKey();
73 for(int i=0;i<key.Length;i++)
74 {
75 x=p.IndexOf(plaintext[i].ToString());
76 y=p.IndexOf(key[i].ToString());
77 result.Append(Vtable[x][y].ToString());
78 }
79 string fileName="c:\\Encryptdate.txt";
80 if(File.Exists(fileName))
81 {
82 File.Delete(fileName);
83 }
84 FileStream fs = File.Create(fileName);
85
86 StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Unicode);
87 sw.Write(result);
88 sw.Flush();
89 fs.Close();
90 keyWord.Remove(0,keyWord.Length);
91 result.Remove(0,result.Length);
92
93 }
94
95 public void GetPlainWord(string str,string str2)//str为解密的密文,str2为密钥
96 {
97 int x,y;
98 keyWord.Append(str2);
99 plaintext=str.ToCharArray();
100 GetKey();
101 for(int i=0;i<key.Length;i++)
102 {
103 y=p.IndexOf(key[i].ToString());
104 x=v[y].IndexOf(plaintext[i].ToString());
105 result.Append(p[x].ToString());
106 }
107 string fileName="c:\\plaintext.txt";
108 if(File.Exists(fileName))
109 {
110 File.Delete(fileName);
111 }
112 FileStream fs=File.Create(fileName);
113 StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Unicode);
114 sw.Write(result);
115 sw.Flush();
116 fs.Close();
117 keyWord.Remove(0,keyWord.Length);
118 result.Remove(0,result.Length);
119 }
120
121 }
2 {
3 public StringBuilder keyWord;
4 public char[] plaintext;
5 public char[] key;
6 public char[][] Vtable;
7 private string p="abcdefghijklmnopqrstuvwxyz";
8 public StringBuilder result;
9 public string[] v=
10 {
11 "ABCDEFGHIJKLMNOPQRSTUVWXYZ","BCDEFGHIJKLMNOPQRSTUVWXYZA",
12 "CDEFGHIJKLMNOPQRSTUVWXYZAB","DEFGHIJKLMNOPQRSTUVWXYZABC",
13 "EFGHIJKLMNOPQRSTUVWXYZABCD","FGHIJKLMNOPQRSTUVWXYZABCDE",
14 "GHIJKLMNOPQRSTUVWXYZABCDEF","HIJKLMNOPQRSTUVWXYZABCDEFG",
15 "IJKLMNOPQRSTUVWXYZABCDEFGH","JKLMNOPQRSTUVWXYZABCDEFGHI",
16 "KLMNOPQRSTUVWXYZABCDEFGHIJ","LMNOPQRSTUVWXYZABCDEFGHIJK",
17 "MNOPQRSTUVWXYZABCDEFGHIJKL","NOPQRSTUVWXYZABCDEFGHIJKLM",
18 "OPQRSTUVWXYZABCDEFGHIJKLMN","PQRSTUVWXYZABCDEFGHIJKLMNO",
19 "QRSTUVWXYZABCDEFGHIJKLMNOP","RSTUVWXYZABCDEFGHIJKLMNOPQ",
20 "STUVWXYZABCDEFGHIJKLMNOPQR","TUVWXYZABCDEFGHIJKLMNOPQRS",
21 "UVWXYZABCDEFGHIJKLMNOPQRST","VWXYZABCDEFGHIJKLMNOPQRSTU",
22 "WXYZABCDEFGHIJKLMNOPQRSTUV","XYZABCDEFGHIJKLMNOPQRSTUVW",
23 "YZABCDEFGHIJKLMNOPQRSTUVWX","ZABCDEFGHIJKLMNOPQRSTUVWXY"};
24
25 public Class1()
26 {
27 keyWord=new StringBuilder();
28 plaintext=new char[100];
29 key=new char[100];
30 Vtable=new char[26][];
31 result=new StringBuilder();
32 for(int i=0;i<v.Length;i++)
33 {
34 Vtable[i]=v[i].ToCharArray();
35 }
36
37 //
38 // TODO: 在此处添加构造函数逻辑
39 //
40 }
41
42 private void GetKey()
43 {
44 int Pnum,KWnum,n,ne;
45 string k,k1,k2;
46 k2=keyWord.ToString();
47 Pnum=plaintext.Length;
48 KWnum=keyWord.Length;
49 if(Pnum>KWnum)
50 {
51 n=Pnum/KWnum;
52 ne=Pnum%KWnum;
53 for(int i=1;i<n;i++)
54 keyWord.Append(k2);
55 k=keyWord.ToString().Substring(0,ne);
56 keyWord.Append(k);
57 k1=keyWord.ToString();
58 key=keyWord.ToString().ToCharArray();
59 }
60 else
61 {
62 key=keyWord.ToString().Substring(0,Pnum).ToCharArray();
63 }
64
65 }
66
67 public void EncryptData(string str,string str2)//str为加密的明文,str2为密钥
68 {
69 int x,y;
70 keyWord.Append(str2);
71 plaintext=str.ToCharArray();
72 GetKey();
73 for(int i=0;i<key.Length;i++)
74 {
75 x=p.IndexOf(plaintext[i].ToString());
76 y=p.IndexOf(key[i].ToString());
77 result.Append(Vtable[x][y].ToString());
78 }
79 string fileName="c:\\Encryptdate.txt";
80 if(File.Exists(fileName))
81 {
82 File.Delete(fileName);
83 }
84 FileStream fs = File.Create(fileName);
85
86 StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Unicode);
87 sw.Write(result);
88 sw.Flush();
89 fs.Close();
90 keyWord.Remove(0,keyWord.Length);
91 result.Remove(0,result.Length);
92
93 }
94
95 public void GetPlainWord(string str,string str2)//str为解密的密文,str2为密钥
96 {
97 int x,y;
98 keyWord.Append(str2);
99 plaintext=str.ToCharArray();
100 GetKey();
101 for(int i=0;i<key.Length;i++)
102 {
103 y=p.IndexOf(key[i].ToString());
104 x=v[y].IndexOf(plaintext[i].ToString());
105 result.Append(p[x].ToString());
106 }
107 string fileName="c:\\plaintext.txt";
108 if(File.Exists(fileName))
109 {
110 File.Delete(fileName);
111 }
112 FileStream fs=File.Create(fileName);
113 StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.Unicode);
114 sw.Write(result);
115 sw.Flush();
116 fs.Close();
117 keyWord.Remove(0,keyWord.Length);
118 result.Remove(0,result.Length);
119 }
120
121 }
在你的程序中调用这个类就可以实现简单的Vigenere加密,只能识别字母,其他的字符读者可以自己加入。其中两个公有函数中的两个参数表示的字符串,程序中已经写明自己看。有什么问题望大家指教!!