加密技术(学习笔记四矩阵加密)看名字就知道了,这是一个矩阵既然是矩阵就有 [0,0],就可以比方作x轴,和y轴了,.

 

1 首先,要确定,原文出现的所有字符串作为---x轴

2 对比文-------------y轴

3 x,y生成一个字符串矩阵

4 要求,x轴密码不可以重复

5 有x,y  的坐标点确定一个密码

采用此种方法,首先确定x轴,是明文所以,同一y轴下的所有x轴变量不可重复,

注意采用此方法写的密码不能被频率分析法

破解哦

 

 

 Password
{
    /// <summary>
    
/// 加密矩阵- -挺复杂的加密方法
    
/// </summary>
    public class VigenereRandom:ABEncryptDecrypt
    {



        
/// <summary>
        
/// 产生一组密钥
        
/// </summary>
        
/// <returns></returns>
        public override Object CreateKey(int State, int KeyCount)
        {
            
//---这个是钥匙类
            VigenereRandomKey Keys = new VigenereRandomKey();

            
//--首先给找y轴的落点
            RandomNumberGenerator rng = new RNGCryptoServiceProvider();
            Keys.Ys 
= new Byte[KeyCount];

            rng.GetNonZeroBytes(Keys.Ys);



            
//--实例华 y,x 矩阵(矩阵地图)
            Keys.Map_Y_X = new List<System.Collections.Generic.List<char>>();


            Random r 
= new Random();

            
//-------按照byte   的 范围--257 是y轴
            for (int i2 = 0; i2 < 257; i2++)
            {
                
//--   x轴--由于确定是0~255
                System.Collections.Generic.List<char> x = new List<char>();


                
for (int i = 0; i < 257; )
                {
                    
int keyt = r.Next(7030000);

                    
//--将字节转换成字符串
                    if (!x.Contains((char)keyt))
                    {
                        x.Add((
char)keyt);

                        i
++;
                    }


                }

                Keys.Map_Y_X.Add(x);

            }










            
return Keys;
        }
        
/// <summary>
        
///验证 
        
/// </summary>
        
/// <returns></returns>
        public override bool Validate(System.Text.StringBuilder sb)
        {

            
return false;
        }


        
/// <summary>
        
/// 加密
        
/// </summary>
        
/// <param name="InputEncrypt"></param>
        
/// <returns></returns>
        public override void Encrypt(Context InputEncrypt)
        {
            
//--密文
            System.Text.StringBuilder CryptographText = new StringBuilder();
           
            
//---将明文-转换成子节数祖
            byte[] OriginalTextByte = System.Text.Encoding.UTF8.GetBytes(InputEncrypt.OriginalText);
             
            
//--生成加密钥匙 
            VigenereRandomKey Keys = this.CreateKey(0, OriginalTextByte.Length) as VigenereRandomKey;


            
//--通过矩阵--找到密码
            for (int i = 0; i < OriginalTextByte.Length; i++)
            {

                
//---y轴坐标点--实际上--可以看成有多组秘钥-----y轴决定用那组秘钥
                int Y = Keys.Ys[i];

                
//---X轴,确定----用那个秘钥来代替--字节数祖
                int X = OriginalTextByte[i];

                
//--通过  纵轴 Y 的落点 ,加原文生成的 X落点----------组成密文
                Char CryptographChar = Keys.Map_Y_X[Y][X];


                CryptographText.Append(CryptographChar);





            }

            InputEncrypt.Key 
= Keys;
            InputEncrypt.CryptographText 
= CryptographText;


        }



        
/// <summary>
        
/// 解密
        
/// </summary>
        
/// <param name="InputEncrypt"></param>
        
/// <returns></returns>
        public override void Decrypt(Context InputDecrypt)
        {


            
//--生成加密钥匙 
            VigenereRandomKey Keys = InputDecrypt.Key as VigenereRandomKey;
       

            
byte[] not=new  byte[Keys.Ys.Length];
       

            
for (int i = 0; i < not.Length; i++)
            {
                
//---Y轴落点-实际上就是确定用的那一组加密钥
                int Y = Keys.Ys[i];

                
//---X轴参考落点--可以在通过参考落点在--Y轴--找到原来被加密的数据----------------------------
                Char X = InputDecrypt.CryptographText[i];
                
                
//----在确定的y轴上---------找x轴坐标---由于x轴坐标就是 原文的子节数祖
                not[i] = (byte)Keys.Map_Y_X[Y].IndexOf(X);

        



            }






        InputDecrypt.OriginalText
= System.Text.Encoding.UTF8.GetString(not);




       


        }








    }


    
/// <summary>
    
/// 矩阵钥匙
    
/// 
    
/// 包括  y轴坐标点
    
/// 
    
/// y,x 轴 矩阵坐标集合
    
/// </summary>
    public class VigenereRandomKey
    {
        
//---在y轴坐标点--就是在下面矩阵中y轴寻找的一点,  

        
//---他和密文中的一点 组和可以找到明文 
       public Byte[] Ys = null;

        
//--矩阵地图-- y轴,x轴----y轴就是上面 ys找到的点
        
//--下面这个集合 首先他的索引代表 y轴,而实际保存的东西代表x轴 ,System.Collections.Generic.List<char>
       public System.Collections.Generic.List<System.Collections.Generic.List<char>> Map_Y_X = new List<System.Collections.Generic.List<char>>();

    }
}

 

 

 

相关文章:

  • 2021-12-02
  • 2022-12-23
  • 2021-07-17
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
猜你喜欢
  • 2021-12-02
  • 2021-09-03
  • 2021-07-13
  • 2021-05-15
  • 2022-12-23
  • 2022-01-19
  • 2021-06-21
相关资源
相似解决方案