维吉尼亚密码(Vigenère Cipher)加密算法

算法简述
密钥:
K k 1 k 2 k d
i 位密钥 k i 表示采用 k= k i 的凯撒替换表
密钥重复使用
加密算法: C i = E( p i ) = ( p i + k i ) mod 26
解密算法: p i = D(C i ) = (C i - k i ) mod 26
维吉尼亚密码(Vigenère Cipher)加密算法_第1张图片
 
 
主要代码如下:

 int i,j,m,n,p,q,s,r,temp,res;
            s = 0;
            char[] str1 = new char[80];
            char[ ,] arr = new char[26,26];
            Console.WriteLine("Vigenère Cipher 密码表如下(行代表明文,列代表密钥):");
            int[] x=new int[80];
            int[] y=new int[80];
          
            char[] t = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
            char[] t1 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

            for (i = 0; i < 26;i++ )
            {
                for (j = 0; j < 26;j++ )
                {
                    temp =(i+j) % 26;
                    arr[i,j] = t1[temp];
                    Console.Write(arr[i,j] + "  ");
                }
                Console.WriteLine();
            }
           
            Console.WriteLine("请输入所需要加名的明文:");
            string str = Console.ReadLine();
            Console.WriteLine("请输入加密密钥:");
            string key = Console.ReadLine();
         

            for (m = 0; m < str.Length;m++ )
            {
                for (n = 0; n < t.Length; n++)
                {
                    if(str[m] == t[n])
                    {
                        x[m]=n;
                      
                    }

                }
            }
          
            for (p = 0; p < key.Length; p++)
            {
                for (q = 0; q < t.Length; q++)
                {
                    if (key[p] == t[q])
                    {
                        y[p] = q;
                      
                    }

                }
            }

           
            for ( r = 0; r < str.Length;r++ )
            {
              
                    res =( x[s] + y[s]) % 26;
                  
                    str1[r] = t1[res];

                    s++;
               
            }

            Console.WriteLine("加密得到的密文为:");
            Console.WriteLine(str1);
            Console.ReadKey();
         

运行结果
维吉尼亚密码(Vigenère Cipher)加密算法_第2张图片

你可能感兴趣的:(维吉尼亚密码(Vigenère Cipher)加密算法)