加密技术(学习笔记一)
加密很重要- -比如登陆密码在数据库中的保存,很少有明文的
2身份验证(解密或者加密必须验证身份??估计)
3完整性
4不可抵赖性(实际上就是数字签名,发消息的人对所发消息必须负责)
加密技术功能
1密码: 一种算法.将可能输入的消息转为特定的加密消息,注意其可逆性必须存在,
可在现原始消息
2密钥匙: 密码使用的输入,用来加密消息
3密钥空间:密码可能用来加密消息所有的密钥集合
4原始消息:明文
5加密消息:被加密的明文
6加密:将明文转换成密文
//----针对以上特性有如下代码,加密的基础类
//--加密类的接口
//---
加密的接口类 IEncryptDecrypt
using System;
using System.Collections.Generic;
using System.Text;
namespace Password
{
/**//// <summary>
/// 加密类,得接口
/// </summary>
public interface IEncryptDecrypt
{
/**//// <summary>
/// 保存
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
void SaveFile(String Path, System.Text.StringBuilder inputInfo);
/**//// <summary>
/// 读取
/// </summary>
/// <param name="Path">路径</param>
/// <param name="br">被读取得字符串</param>
System.Text.StringBuilder LoadFile(String Path);
/**//// <summary>
/// 保存凭证
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
void SaveContext(String Path,Context inputInfo);
/**//// <summary>
/// 读取凭证
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
Context LoadContext(String Path);
/**//// <summary>
/// 序列化对象
/// </summary>
/// <param name="inputObjet"></param>
/// <returns></returns>
byte[] Serialize(Object inputObjet);
/**//// <summary>
/// 反序列化对象
/// </summary>
/// <param name="serializedExtendedAttributes"></param>
/// <returns></returns>
Object Deserialize(byte[] inputDeserialize);
/**//// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
Object CreateKey(int State, int KeyCount);
/**//// <summary>
///验证
/// </summary>
/// <returns></returns>
bool Validate(System.Text.StringBuilder sb);
/**//// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
void Encrypt(Context InputEncrypt);
/**//// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
void Decrypt(Context InputDecrypt);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Password
{
/**//// <summary>
/// 加密类,得接口
/// </summary>
public interface IEncryptDecrypt
{
/**//// <summary>
/// 保存
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
void SaveFile(String Path, System.Text.StringBuilder inputInfo);
/**//// <summary>
/// 读取
/// </summary>
/// <param name="Path">路径</param>
/// <param name="br">被读取得字符串</param>
System.Text.StringBuilder LoadFile(String Path);
/**//// <summary>
/// 保存凭证
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
void SaveContext(String Path,Context inputInfo);
/**//// <summary>
/// 读取凭证
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
Context LoadContext(String Path);
/**//// <summary>
/// 序列化对象
/// </summary>
/// <param name="inputObjet"></param>
/// <returns></returns>
byte[] Serialize(Object inputObjet);
/**//// <summary>
/// 反序列化对象
/// </summary>
/// <param name="serializedExtendedAttributes"></param>
/// <returns></returns>
Object Deserialize(byte[] inputDeserialize);
/**//// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
Object CreateKey(int State, int KeyCount);
/**//// <summary>
///验证
/// </summary>
/// <returns></returns>
bool Validate(System.Text.StringBuilder sb);
/**//// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
void Encrypt(Context InputEncrypt);
/**//// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
void Decrypt(Context InputDecrypt);
}
}
加密的抽象类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
{
/**//// <summary>
/// 加密类的抽象类,实现加密接口,并提供部分实现
/// </summary>
public abstract class ABEncryptDecrypt:IEncryptDecrypt
{
public ABEncryptDecrypt()
{ }
/**//// <summary>
/// 验证一下文件存在不,如果不存在就创建
/// </summary>
/// <param name="Path"></param>
private static void ValidFilePath(String Path)
{
if (!System.IO.File.Exists(Path))
{
System.IO.File.Create(Path);
}
}
/**//// <summary>
/// 保存
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
public void SaveFile(String Path, System.Text.StringBuilder inputInfo)
{
System.IO.File.WriteAllText(Path, inputInfo.ToString());
}
/**//// <summary>
/// 读取
/// </summary>
/// <param name="Path">路径</param>
/// <param name="br">被读取得字符串</param>
public System.Text.StringBuilder LoadFile(String Path)
{
System.Text.StringBuilder br = new StringBuilder();
br.Append(System.IO.File.ReadAllText(Path));
return br;
}
/**//// <summary>
/// 保存凭证
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
public void SaveContext(String Path, Context inputInfo)
{
byte[] SaveByte = this.Serialize(inputInfo);
System.IO.File.WriteAllBytes(Path, SaveByte);
}
/**//// <summary>
/// 读取凭证
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public Context LoadContext(String Path)
{
Byte[] Output = System.IO.File.ReadAllBytes(Path);
return this.Deserialize(Output) as Context;
}
/**//// <summary>
/// 序列化对象
/// </summary>
/// <param name="inputObjet"></param>
/// <returns></returns>
public byte[] Serialize(Object inputObjet)
{
// 序列化对象
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;
}
/**//// <summary>
/// 反序列化对象
/// </summary>
/// <param name="serializedExtendedAttributes"></param>
/// <returns></returns>
public Object Deserialize(byte[] inputDeserialize)
{
object renObjet = null;
if (inputDeserialize.Length == 0)
return renObjet;
try
{
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();
}
catch { }
return renObjet;
}
//----将字节数组转换成字符串中间用*号分割
/**//// <summary>
/// 将字节数组转换成字符串中间用*号分割
/// </summary>
/// <param name="inputBytes"></param>
/// <returns></returns>
protected System.Text.StringBuilder ConvertStringBuilder(Byte[] inputBytes)
{
System.Text.StringBuilder renBytes = new StringBuilder();
foreach (byte tempByte in inputBytes)
{
renBytes.Append(tempByte);
renBytes.Append("*");
}
//--删除最后一个*号
if (renBytes.Length > 0)
{
renBytes.Remove(renBytes.Length - 1, 1);
}
return renBytes;
}
/**//// <summary>
/// 将字符船分割成一*分割的数组
/// </summary>
/// <param name="inputSb"></param>
/// <returns></returns>
protected System.Collections.Generic.List<String> ConvertStrings(System.Text.StringBuilder inputSb)
{
System.Collections.Generic.List<string> renStrings = new List<string>(inputSb.ToString().Split('*'));
return renStrings;
}
/**//// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
public abstract Object CreateKey(int State, int KeyCount);
/**//// <summary>
///验证
/// </summary>
/// <returns></returns>
public abstract bool Validate(System.Text.StringBuilder sb);
/**//// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public abstract void Encrypt(Context InputEncrypt);
/**//// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public abstract void Decrypt(Context InputDecrypt);
}
}
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 abstract class ABEncryptDecrypt:IEncryptDecrypt
{
public ABEncryptDecrypt()
{ }
/**//// <summary>
/// 验证一下文件存在不,如果不存在就创建
/// </summary>
/// <param name="Path"></param>
private static void ValidFilePath(String Path)
{
if (!System.IO.File.Exists(Path))
{
System.IO.File.Create(Path);
}
}
/**//// <summary>
/// 保存
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
public void SaveFile(String Path, System.Text.StringBuilder inputInfo)
{
System.IO.File.WriteAllText(Path, inputInfo.ToString());
}
/**//// <summary>
/// 读取
/// </summary>
/// <param name="Path">路径</param>
/// <param name="br">被读取得字符串</param>
public System.Text.StringBuilder LoadFile(String Path)
{
System.Text.StringBuilder br = new StringBuilder();
br.Append(System.IO.File.ReadAllText(Path));
return br;
}
/**//// <summary>
/// 保存凭证
/// </summary>
/// <param name="Path"></param>
/// <param name="inputInfo"></param>
public void SaveContext(String Path, Context inputInfo)
{
byte[] SaveByte = this.Serialize(inputInfo);
System.IO.File.WriteAllBytes(Path, SaveByte);
}
/**//// <summary>
/// 读取凭证
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public Context LoadContext(String Path)
{
Byte[] Output = System.IO.File.ReadAllBytes(Path);
return this.Deserialize(Output) as Context;
}
/**//// <summary>
/// 序列化对象
/// </summary>
/// <param name="inputObjet"></param>
/// <returns></returns>
public byte[] Serialize(Object inputObjet)
{
// 序列化对象
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;
}
/**//// <summary>
/// 反序列化对象
/// </summary>
/// <param name="serializedExtendedAttributes"></param>
/// <returns></returns>
public Object Deserialize(byte[] inputDeserialize)
{
object renObjet = null;
if (inputDeserialize.Length == 0)
return renObjet;
try
{
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();
}
catch { }
return renObjet;
}
//----将字节数组转换成字符串中间用*号分割
/**//// <summary>
/// 将字节数组转换成字符串中间用*号分割
/// </summary>
/// <param name="inputBytes"></param>
/// <returns></returns>
protected System.Text.StringBuilder ConvertStringBuilder(Byte[] inputBytes)
{
System.Text.StringBuilder renBytes = new StringBuilder();
foreach (byte tempByte in inputBytes)
{
renBytes.Append(tempByte);
renBytes.Append("*");
}
//--删除最后一个*号
if (renBytes.Length > 0)
{
renBytes.Remove(renBytes.Length - 1, 1);
}
return renBytes;
}
/**//// <summary>
/// 将字符船分割成一*分割的数组
/// </summary>
/// <param name="inputSb"></param>
/// <returns></returns>
protected System.Collections.Generic.List<String> ConvertStrings(System.Text.StringBuilder inputSb)
{
System.Collections.Generic.List<string> renStrings = new List<string>(inputSb.ToString().Split('*'));
return renStrings;
}
/**//// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
public abstract Object CreateKey(int State, int KeyCount);
/**//// <summary>
///验证
/// </summary>
/// <returns></returns>
public abstract bool Validate(System.Text.StringBuilder sb);
/**//// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public abstract void Encrypt(Context InputEncrypt);
/**//// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public abstract void Decrypt(Context InputDecrypt);
}
}
上下文对象
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;
}
}
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的时候返序列就--哈了