***************************************
2月12 加密算法;数据的序列化;元数据 反射;元数据
****************************************
加密算法
·对称加密
算法公开,计算量小,加密速度快,效率高,但是交易双方使用同样的密钥。安全性得不到保障。
此加密算法有:DES和IDEA
DES:8位校验码,56位数据信息
命名空间
System.Security.Cryptography
****************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Console3
{
class Encryption
{
static void Main(string[] args)
{
string key="12345678";
//>8
string desIV="hello123456";
//将字符串转换为流字节
byte[] bKey=ASCIIEncoding.ASCII.GetBytes(key);
byte[] bdesIV=ASCIIEncoding.ASCII.GetBytes(desIV);
EncryptData("a.txt", "b.txt", bKey, bdesIV);
DecryptData("b.txt", "c.txt", bKey, bdesIV);
}
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);//路径,方式,可读
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);//流对象为0
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{ //Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);//路径,方式,可读
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);//流对象为0
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Decrypting...");
//Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
}
}
C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Console1\Console1\bin\Debug
(明文+密钥+向量)Key+IV
string desIV="hello123456";
//string--->byte[]
byte[] bdesIV=ASCIIEncoding.ASCII.GetBytes(desIV);
//string<---byte[]
String str = UnicodeEncoding.Unicode.GetString(bKey);
****************************************
MD5加密:存储Hash值
public string MD5Encrypt(string strCipher)
{
byte[] data = ASCIIEncoding.ASCII.GetBytes(strCipher);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
return UnicodeEncoding.Unicode.GetString(result);
}
三重Md5加密
****************************************
·非对称加密
两个密钥:公开密钥和私有密钥
加密解密使用的是不同的密钥
DSA RSA
---------------------------------------------------
---------------------------------------------------
** 数据的序列化**
将对象序列化为二进制或者SOAP格式
##
在异构网络中使用。SOAP:简单对象访问协议 是基于XMl(跨平台)的
在同构网络中可以使用二进制流,不需要跨平台
##
通过 XmlSerializer 类、 IXmlSerializable接口及 XML 序列化属性和委托,将对象序列化为自定义 XML 格式
对象:
DataTable dt = getDTa();
BinaryFormatter BinForm = new BinaryFormatter();
FileStream fs = File.Create("test.txt");
BinForm.Serialize(fs, dt);
fs = File.Open("test.txt", FileMode.Open);
dt = (DataTable)BinForm.Deserialize(fs);
fs.Close();
Xml:
string filename = "c:\\Company.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Company));
TextWriter writer = new StreamWriter(filename);
Company com = new Company();
com.CompanyName = "ABC InfoSystems";
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.Name = "John";
emp2.Name = "Peter";
com.Employees = new Employee[2] { emp1, emp2 };
serializer.Serialize(writer, com);
writer.Close();
FileStream fs = new FileStream(filename, FileMode.Open);
Company cmp = (Company)serializer.Deserialize(fs);
Console.WriteLine(cmp.CompanyName);
foreach (Employee e in cmp.Employees)
{
Console.WriteLine(" {0}", e.Name);
}
Console.Write("\nPress ENTER to exit...");
Console.ReadLine();
在一个类的前面加[Serializable],说明该类可以被序列化和反序列化,并且该类不可以被继承
****************************************
****************************************
Chapter 11 元数据
Assembly(程序集)包含若干个模块,模块包含若干类。
反射:在运行时检查程序集清单中的元数据的功能
为了更好的说明.Net反射的作用,先看两个殊途同归的例子:
//The conventional way to call an object.
BusinessObject bo=new BusinessObject();
bo.DoWork();
//The .Net reflection way to call an object.1、加载程序集 参数是namespace
Assembly assm=Assembly.Load("TestApp");
//2、获取类的类型
Type objType=assm.GetType("TestApp.BusinessObject");
//3、创建该类的实例
object objInstance=Activator.CreateInstance(objType);
//4、调用该实例中的方法
objType.InvokeMember("DoWork",BindingFlags.InvokeMethod,null,objInstance,null);
通过反射机制来调用一个方法,包括一下四步:
1、加载程序集
2、获取类的类型
3、创建该类的实例
4、调用该实例中的方法
应用:分布式网络编程中使用,系统的升级,只升级部分插件就可以
****************************************
****************************************
多线程:
命名空间:System.Threading
-------------------
public static void ThreadProc()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main()
{
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. Note that on a uniprocessor, the new
// thread does not get any processor time until the main thread
// is preempted or yields. Uncomment the Thread.Sleep that
// follows t.Start() to see the difference.
t.Start();
Thread.Sleep(1000);
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
-------------------
SynchronizationContext类控制同步
委托的同步和异步操作
浙公网安备 33010602011771号