加密技术(学习笔记=)Caesar算法

一个传统的算法(拥有2000年历史)非常古老的算法,就是字母移动位..

 

比如:

//--直接让字符串+偏移位(甚至可以减去偏移位)
        char aaa = Convert.ToChar(('中'+2) );
        Console.WriteLine(aaa);
//---解密的时候反转就可以了
        char bbc = Convert.ToChar(((aaa) - 2));

但是这就设计到一个问题,可以用反转的方式,比如说让所有的字符串减几来推测,可能的明文,这样不安全的说.


for(int a=0;a<100;a++)
{
  //----看这里就可以循环破译了
 char bbc = Convert.ToChar(((aaa) - a));
}

 

修改下 针对上面的破译可以有通过随机函数针对每一位进行偏移- -这时候就需要一个对应的偏移key记录偏移顺序的阿阿

 

关键点:

 RandomNumberGenerator rng = new RNGCryptoServiceProvider(); 生成随机 数组

 

对随机数组+上原文

            for (int i = 0; i < Key.Length; i++)
            {
                if (Key[i] < 2)
                {
                    Key[i] = 2;
                }

                sb[i] = (char)(sb[i] + Key[i]);
            }

 

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif CaesarRandom

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Password
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// caesar--最简单的加密算法就是将字符串中的子符都移位
    
/// CaesarRandom 是对caesar的升级采用随机算法
    
/// </summary>

    public  class CaesarRandom:ABEncryptDecrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 产生一组密钥
        
/// </summary>
        
/// <returns></returns>

        public override Object CreateKey(int State, int KeyCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            RandomNumberGenerator rng 
= new RNGCryptoServiceProvider();
            
byte[] kyes = new Byte[KeyCount];
            rng.GetNonZeroBytes(kyes);

            
return kyes;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
///验证
        
/// </summary>
        
/// <returns></returns>

        public override bool Validate(System.Text.StringBuilder sb)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
return false;
        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 加密
        
/// </summary>
        
/// <param name="InputEncrypt"></param>
        
/// <returns></returns>

        public override void Encrypt(Context InputEncrypt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//--将原文生成StringBuider
            System.Text.StringBuilder sb =new StringBuilder( InputEncrypt.OriginalText as  String);

            
//---生成加密钥匙
            Byte[] Key = this.CreateKey(0,sb.Length) as Byte[];


            
//---将原文的每一个字符与钥匙的相应位进行---加密
            for (int i = 0; i < Key.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (Key[i] < 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Key[i] 
= 2;
                }


                sb[i] 
= (char)(sb[i] + Key[i]);
            }



            
//---设置钥匙
            InputEncrypt.Key = Key;

            
//---设置密文
            InputEncrypt.CryptographText = sb;

            
//---清除--原文
            InputEncrypt.OriginalText = String.Empty;
          
        }




ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 解密
        
/// </summary>
        
/// <param name="InputEncrypt"></param>
        
/// <returns></returns>

        public override void Decrypt(Context InputDecrypt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            System.Text.StringBuilder sb 
= InputDecrypt.CryptographText as System.Text.StringBuilder;
            
byte[] Key = InputDecrypt.Key as byte[];
            
for (int i = 0; i < Key.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                sb[i] 
= (char)(sb[i] - Key[i]);
            }



            InputDecrypt.OriginalText 
= sb.ToString();
        
        }









    }

}


 

 

 

ContractedBlock.gif ExpandedBlockStart.gif 调用
            Console.WriteLine("请输入要加密的信息");
         System.Text.StringBuilder IfAdd 
= new StringBuilder(Console.ReadLine());

           ABEncryptDecrypt AED 
= new CaesarRandom();


           Context C 
= new Context();
           C.OriginalText 
= IfAdd.ToString();


            AED.Encrypt(C);

        Console.WriteLine(C.CryptographText.ToString());

           AED.Decrypt(C);


          Console.WriteLine(C.OriginalText 
as String);

 

 

转载于:https://www.cnblogs.com/ajaxren/archive/2008/11/01/1324092.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值