加密技术(学习笔记一)

加密很重要- -比如登陆密码在数据库中的保存,很少有明文的
安全的重要性
1秘密性
2身份验证(解密或者加密必须验证身份??估计)
3完整性
4不可抵赖性(实际上就是数字签名,发消息的人对所发消息必须负责)
              
加密技术功能 Security 类提供.

加密技术的基本术语

1密码: 一种算法.将可能输入的消息转为特定的加密消息,注意其可逆性必须存在,
     可在现原始消息
2密钥匙: 密码使用的输入,用来加密消息
3密钥空间:密码可能用来加密消息所有的密钥集合
4原始消息:明文
5加密消息:被加密的明文
6加密:将明文转换成密文
//----针对以上特性有如下代码,加密的基础类自己写的呵凑合用net2.0
//--加密类的接口
//---最后提醒下各位,本程序没有经过设计模式的优化

 

ContractedBlock.gif ExpandedBlockStart.gif 加密的接口类 IEncryptDecrypt
using System; 
using System.Collections.Generic; 
using System.Text; 
namespace Password 
ExpandedBlockStart.gifContractedBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary> 
        
/// 加密类,得接口 
        
/// </summary> 

    public interface IEncryptDecrypt 
ExpandedSubBlockStart.gifContractedSubBlock.gif    

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 保存 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <param name="inputInfo"></param> 

        void SaveFile(String Path, System.Text.StringBuilder inputInfo); 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
        
/// 读取 
        
/// </summary> 
        
/// <param name="Path">路径</param> 
        
/// <param name="br">被读取得字符串</param> 

        System.Text.StringBuilder LoadFile(String Path); 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
        
/// 保存凭证 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <param name="inputInfo"></param> 

        void SaveContext(String Path,Context inputInfo); 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
        
/// 读取凭证 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <returns></returns> 

        Context LoadContext(String Path); 

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
        
/// 序列化对象 
        
/// </summary> 
        
/// <param name="inputObjet"></param> 
        
/// <returns></returns> 

        byte[] Serialize(Object inputObjet); 

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
        
/// 反序列化对象 
        
/// </summary> 
        
/// <param name="serializedExtendedAttributes"></param> 
        
/// <returns></returns> 

        Object Deserialize(byte[] inputDeserialize); 



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

        Object CreateKey(int State, int KeyCount); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
///验证 
        
/// </summary> 
        
/// <returns></returns> 

        bool Validate(System.Text.StringBuilder sb); 

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

        void Encrypt(Context InputEncrypt); 

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

        void Decrypt(Context InputDecrypt); 

    }
 

   
}
 


 

ContractedBlock.gif ExpandedBlockStart.gif 加密的抽象类ABEncryptDecrypt
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> 
         
/// 加密类的抽象类,实现加密接口,并提供部分实现 
         
/// </summary> 

    public abstract class ABEncryptDecrypt:IEncryptDecrypt 
ExpandedSubBlockStart.gifContractedSubBlock.gif    

        
public ABEncryptDecrypt() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{ } 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 验证一下文件存在不,如果不存在就创建 
        
/// </summary> 
        
/// <param name="Path"></param> 

        private static void ValidFilePath(String Path) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            
if (!System.IO.File.Exists(Path)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                System.IO.File.Create(Path); 
            }
 
        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 保存 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <param name="inputInfo"></param> 

       public   void SaveFile(String Path, System.Text.StringBuilder inputInfo) 
ExpandedSubBlockStart.gifContractedSubBlock.gif       

               
                System.IO.File.WriteAllText(Path, inputInfo.ToString()); 
     
     
       }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 读取 
        
/// </summary> 
        
/// <param name="Path">路径</param> 
        
/// <param name="br">被读取得字符串</param> 

        public System.Text.StringBuilder LoadFile(String Path) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        


            System.Text.StringBuilder br 
= new StringBuilder(); 
            br.Append(System.IO.File.ReadAllText(Path)); 
            
return br; 
        }
 


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 保存凭证 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <param name="inputInfo"></param> 

        public void SaveContext(String Path, Context inputInfo) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            
byte[] SaveByte = this.Serialize(inputInfo); 
            System.IO.File.WriteAllBytes(Path, SaveByte); 
           
         
        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 读取凭证 
        
/// </summary> 
        
/// <param name="Path"></param> 
        
/// <returns></returns> 

        public Context LoadContext(String Path) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            Byte[] Output 
= System.IO.File.ReadAllBytes(Path); 

            
return this.Deserialize(Output) as Context; 
         
         
        }
 


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 序列化对象 
        
/// </summary> 
        
/// <param name="inputObjet"></param> 
        
/// <returns></returns> 

        public byte[] Serialize(Object inputObjet) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        


            
// 序列化对象 
            BinaryFormatter binaryFormatter = new BinaryFormatter(); 
            
// 创建一个内存流,序列化后保存在其中 
            MemoryStream ms = new MemoryStream(); 
            
byte[] b; 
            
// 将extendedAttributes对象(里面保存了所有的用户扩展信息)序列化为内存流 
            
// 
            binaryFormatter.Serialize(ms, inputObjet); 
            
// 设置内存流的起始位置 
            
// 
            ms.Position = 0
            
// 读入到byte 数组 
            
// 
            b = new Byte[ms.Length]; 
            ms.Read(b, 
0, b.Length); 
            ms.Close(); 
            
return b; 
        }
 


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 反序列化对象 
        
/// </summary> 
        
/// <param name="serializedExtendedAttributes"></param> 
        
/// <returns></returns> 

        public  Object Deserialize(byte[] inputDeserialize) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        


            
object renObjet = null
            
if (inputDeserialize.Length == 0
                
return renObjet; 
            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                BinaryFormatter binaryFormatter 
= new BinaryFormatter(); 
                MemoryStream ms 
= new MemoryStream(); 
                
// 将byte 数组到内存流 
                
// 
                ms.Write(inputDeserialize, 0, inputDeserialize.Length); 
                
// 将内存流的位置到最开始位置 
                
// 
                ms.Position = 0
                
// 反序列化成NameValueCollection对象,创建出与原对象完全相同的副本 
                
// 
                renObjet = binaryFormatter.Deserialize(ms); 
                ms.Close(); 
            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch { } 
            
return renObjet; 
         
         
        }
 

        
//----将字节数组转换成字符串中间用*号分割 
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//// <summary> 
        
/// 将字节数组转换成字符串中间用*号分割 
        
/// </summary> 
        
/// <param name="inputBytes"></param> 
        
/// <returns></returns> 

        protected System.Text.StringBuilder ConvertStringBuilder(Byte[] inputBytes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            System.Text.StringBuilder renBytes 
= new StringBuilder(); 
            
foreach (byte tempByte in inputBytes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                renBytes.Append(tempByte); 
                renBytes.Append(
"*"); 
             
            }
 
            
//--删除最后一个*号 
            if (renBytes.Length > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                renBytes.Remove(renBytes.Length 
- 11); 
            }
 

            
return renBytes; 
               
        }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
/// 将字符船分割成一*分割的数组 
        
/// </summary> 
        
/// <param name="inputSb"></param> 
        
/// <returns></returns> 

        protected  System.Collections.Generic.List<String> ConvertStrings(System.Text.StringBuilder inputSb) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            System.Collections.Generic.List
<string> renStrings = new List<string>(inputSb.ToString().Split('*')); 
            
return renStrings; 
         
        }
 




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

        public abstract Object CreateKey(int State, int KeyCount); 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
        
///验证 
        
/// </summary> 
        
/// <returns></returns> 

        public abstract bool Validate(System.Text.StringBuilder sb); 

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

        public abstract  void Encrypt(Context InputEncrypt); 

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

        public abstract void Decrypt(Context InputDecrypt); 
    }
 
}
 

 

ContractedBlock.gif ExpandedBlockStart.gif 上下文对象
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Security.Cryptography; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 
namespace Password 


   
/// <summary> 
  
/// 加密用的上下文对象 
  
/// </summary> 
      public class Context 
    { 
       
/// <summary> 
        
/// 原文 
        
/// </summary> 
        public String OriginalText; 
       
/// <summary> 
        
/// 加密后得到的钥匙 
        
/// </summary> 
        public object Key; 
       
/// <summary> 
        
/// 秘文 
        
/// </summary> 
        public System.Text.StringBuilder CryptographText; 
        
/// <summary> 
        
/// 是对象还是文本 
        
/// </summary> 
        public bool IsObject = false

    } 


 

//--下次写具体的算法

//--有几点需要说明的-我写的是cs结构的bs的需要重写save ,load就可以,大概原理就是,save的时候序列化上下文对象

而load的时候返序列就--哈了

 

 

转载于:https://www.cnblogs.com/ajaxren/archive/2008/10/30/1323308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值