تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
تشفير و فك تشفير DataSet
#1
كاتب الموضوع : AhmedEssawy

الدالهEncryptDataSet تقوم بعمليه التشفير .
الدالهDecryptDataSet تقوم بعمليه فك التشفير .

C#

كود :
// NOTE: This code snippet is designed for VS.NET 2005 or later.
// Special reference(s) for this code include: System.Security.dll.
// Special namespace usage includes:
// using System.IO, System.Security.Cryptography;
// using System.Runtime.Serialization;
// using System.Runtime.Serialization.Formatters.Binary;
private void EncryptDataSet(DataSet ds)
{
// Create the DES encryption provider:
DES des = new DESCryptoServiceProvider();
// Serialize the DES provider's key and IV to disk for decryption later:
using (StreamWriter sw = new StreamWriter("DES.bin"))
{
BinaryFormatter bf = new BinaryFormatter();
byte[][] stuff = new byte[2][];
stuff[0] = des.Key;
stuff[1] = des.IV;
bf.Serialize(sw.BaseStream, stuff);
}
using (FileStream fs = new FileStream("DataSet.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(), CryptoStreamMode.Write))
{
// Encrypt the DataSet to the file:
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(ds.GetXml());
}
}
}
// Now write the DataSet schema to disk:
ds.WriteXmlSchema($strDataSetSchemaFile$);
}
private DataSet DecryptDataSet()
{
DataSet ds = new DataSet();
// Setup the DES encryption provider:
DES des = new DESCryptoServiceProvider();
// Deserialize the DES provider's key and IV from disk:
using (StreamReader sr = new StreamReader("DES.bin"))
{
BinaryFormatter bf = new BinaryFormatter();
byte[][] stuff = (byte[][])bf.Deserialize(sr.BaseStream);
des.Key = stuff[0];
des.IV = stuff[1];
}
// Decrypt the Encrypted DataSet:
using (FileStream fs = new FileStream("DataSet.bin", FileMode.Open, FileAccess.Read))
{
// Decrypt the DataSet and store it into an instance:
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(), CryptoStreamMode.Read))
{
ds.ReadXml(cs);
}
}
// Now read the DataSet schema from disk:
ds.ReadXmlSchema($strDataSetSchemaFile$);
return ds;
}
VB.Net

كود :
[color=darkgreen]' NOTE: This code snippet is designed for VS.NET 2005 or later.[/color]
[color=darkgreen]' Special reference(s) for this code include: System.Security.dll.[/color]
[color=darkgreen]' Special namespace usage includes: [/color]
[color=darkgreen]' using System.IO, System.Security.Cryptography;[/color]
[color=darkgreen]' using System.Runtime.Serialization;[/color]
[color=darkgreen]' using System.Runtime.Serialization.Formatters.Binary;[/color]
[color=blue]Private Sub [/color][color=black]EncryptDataSet([/color][color=blue]ByVal [/color][color=black]ds [/color][color=blue]As [/color][color=black]DataSet)[/color]
[color=darkgreen]' Create the DES encryption provider:[/color]
[color=blue]Dim [/color][color=black]des [/color][color=blue]As [/color][color=black]DES [/color][color=blue]= New [/color][color=black]DESCryptoServiceProvider[/color]
[color=darkgreen]' Serialize the DES provider's key and IV to disk for decryption later:[/color]
[color=blue]Dim [/color][color=black]sw [/color][color=blue]As [/color][color=black]StreamWriter [/color][color=blue]= New [/color][color=black]StreamWriter([/color][color=#808080]"DES.bin"[/color][color=black])[/color]
[color=blue]Dim [/color][color=black]bf [/color][color=blue]As [/color][color=black]BinaryFormatter [/color][color=blue]= New [/color][color=black]BinaryFormatter[/color]
[color=blue]Dim [/color][color=black][() [/color][color=blue]As Byte[/color]
[color=black]stuff [/color][color=blue]= New Byte[/color][color=black](([/color][color=maroon]2[/color][color=black]) - [/color][color=maroon]1[/color][color=black]) {}[/color]
[color=black]stuff([/color][color=maroon]0[/color][color=black]) [/color][color=blue]= [/color][color=black]des.Key[/color]
[color=black]stuff([/color][color=maroon]1[/color][color=black]) [/color][color=blue]= [/color][color=black]des.IV[/color]
[color=black]bf.Serialize(sw.BaseStream, stuff)[/color]
[color=blue]Dim [/color][color=black]fs [/color][color=blue]As [/color][color=black]FileStream [/color][color=blue]= New [/color][color=black]FileStream([/color][color=#808080]"DataSet.bin"[/color][color=black], FileMode.OpenOrCreate, FileAccess.ReadWrite)[/color]
[color=blue]Dim [/color][color=black]cs [/color][color=blue]As [/color][color=black]CryptoStream [/color][color=blue]= New [/color][color=black]CryptoStream(fs, des.CreateEncryptor, CryptoStreamMode.Write)[/color]
[color=darkgreen]' Encrypt the DataSet to the file:[/color]
[color=blue]Dim [/color][color=black]sw [/color][color=blue]As [/color][color=black]StreamWriter [/color][color=blue]= New [/color][color=black]StreamWriter(cs)[/color]
[color=black]sw.Write(ds.GetXml)[/color]
[color=darkgreen]' Now write the DataSet schema to disk:[/color]
[color=black]ds.WriteXmlSchema(strDataSetSchemaFile)[/color]
[color=blue]End Sub[/color]

[color=blue]Private Function [/color][color=black]DecryptDataSet() [/color][color=blue]As [/color][color=black]DataSet[/color]
[color=blue]Dim [/color][color=black]ds [/color][color=blue]As [/color][color=black]DataSet [/color][color=blue]= New [/color][color=black]DataSet[/color]
[color=darkgreen]' Setup the DES encryption provider:[/color]
[color=blue]Dim [/color][color=black]des [/color][color=blue]As [/color][color=black]DES [/color][color=blue]= New [/color][color=black]DESCryptoServiceProvider[/color]
[color=darkgreen]' Deserialize the DES provider's key and IV from disk:[/color]
[color=blue]Dim [/color][color=black]sr [/color][color=blue]As [/color][color=black]StreamReader [/color][color=blue]= New [/color][color=black]StreamReader([/color][color=#808080]"DES.bin"[/color][color=black])[/color]
[color=blue]Dim [/color][color=black]bf [/color][color=blue]As [/color][color=black]BinaryFormatter [/color][color=blue]= New [/color][color=black]BinaryFormatter[/color]
[color=blue]Dim [/color][color=black][() [/color][color=blue]As Byte[/color]
[color=black]bf.Deserialize(sr.BaseStream)[/color]
[color=black]des.Key [/color][color=blue]= [/color][color=black]stuff([/color][color=maroon]0[/color][color=black])[/color]
[color=black]des.IV [/color][color=blue]= [/color][color=black]stuff([/color][color=maroon]1[/color][color=black])[/color]
[color=darkgreen]' Decrypt the Encrypted DataSet:[/color]
[color=blue]Dim [/color][color=black]fs [/color][color=blue]As [/color][color=black]FileStream [/color][color=blue]= New [/color][color=black]FileStream([/color][color=#808080]"DataSet.bin"[/color][color=black], FileMode.Open, FileAccess.Read)[/color]
[color=darkgreen]' Decrypt the DataSet and store it into an instance:[/color]
[color=blue]Dim [/color][color=black]cs [/color][color=blue]As [/color][color=black]CryptoStream [/color][color=blue]= New [/color][color=black]CryptoStream(fs, des.CreateDecryptor, CryptoStreamMode.Read)[/color]
[color=black]ds.ReadXml(cs)[/color]
[color=darkgreen]' Now read the DataSet schema from disk:[/color]
[color=black]ds.ReadXmlSchema(strDataSetSchemaFile)[/color]
[color=blue]Return [/color][color=black]ds[/color]
[color=blue]End Function[/color]
}}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  طريقة تحويل صفحة في ملف Excel إلى DataSet RaggiTech 0 806 17-10-12, 10:09 PM
آخر رد: RaggiTech
  تشفير النصوص بطريقة رائعة RaggiTech 0 489 17-10-12, 08:42 PM
آخر رد: RaggiTech
  قراءة بيانات من ملف أكسيس إلى DataSet RaggiTech 0 421 17-10-12, 05:45 PM
آخر رد: RaggiTech
  لتعبئه كومبو بوكس ب عمود من dataset RaggiTech 0 323 17-10-12, 05:26 PM
آخر رد: RaggiTech
  عمل تشفير وفك تشفير للنصوص RaggiTech 0 366 17-10-12, 05:13 PM
آخر رد: RaggiTech
  عمل تشفير لملف RaggiTech 0 331 17-10-12, 05:13 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم