惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 探索

字符串倒序算法最优 - 探索 - 博客园 extern关键字 C++的基本概念和术语 关于软件汉化 郁闷! 命名空间语法 c++指针问题 - 探索 - 博客园 对程序集的理解 c#中重写(覆盖)和隐藏类的方法 最近好累呀~~~~ 人工智能规则正向演绎系统简单程序演示(c++) 爱情与婚姻 我的论坛刚刚建立起来,希望大家能支持一下~~~ oracle数据库中数据控制 初学者读书笔记数据库篇(一) C#中只允许产生一个类的实例的方法 今天申请了一个Gmail~~ 关于对SQL Server连接访问问题 今天罪孽深重~~~
Vigenere加密算法类
探索 · 2005-05-24 · via 博客园 - 探索

Posted on 2005-05-24 21:42  探索  阅读(2916)  评论()    收藏  举报

  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    }

在你的程序中调用这个类就可以实现简单的Vigenere加密,只能识别字母,其他的字符读者可以自己加入。其中两个公有函数中的两个参数表示的字符串,程序中已经写明自己看。有什么问题望大家指教!!