string tempFolder = Request.PhysicalApplicationPath + "Assets\\temp";
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
Monday, December 27, 2010
Design Pattern
//Design Pattern Link
http://www.dofactory.com/Patterns/Patterns.aspx
Design Patterns
Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.
The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. Here you will find information on these important patterns.
To give you a head start, the C# source code is provided in 2 forms: 'structural' and 'real-world'. Structural code uses type names as defined in the pattern definition and UML diagrams. Real-world code provides real-world programming situations where you may use these patterns.
A third form, '.NET optimized' demonstrates design patterns that exploit built-in .NET 4.0 features, such as, generics, attributes, delegates, object and collection initializers, automatic properties, and reflection. These and much more are available in our Design Pattern Framework 4.0TM. See our Singleton page for a .NET 4.0 Optimized code sample.
Creational Patterns:
Abstract Factory Creates an instance of several families of classes
Builder Separates object construction from its representation
Factory Method Creates an instance of several derived classes
Prototype A fully initialized instance to be copied or cloned
Singleton A class of which only a single instance can exist
Structural Patterns:
Adapter Match interfaces of different classes
Bridge Separates an object’s interface from its implementation
Composite A tree structure of simple and composite objects
Decorator Add responsibilities to objects dynamically
Facade A single class that represents an entire subsystem
Flyweight A fine-grained instance used for efficient sharing
Proxy An object representing another object
Behavioral Patterns:
Chain of Resp. A way of passing a request between a chain of objects
Command Encapsulate a command request as an object
Interpreter A way to include language elements in a program
Iterator Sequentially access the elements of a collection
Mediator Defines simplified communication between classes
Memento Capture and restore an object's internal state
Observer A way of notifying change to a number of classes
State Alter an object's behavior when its state changes
Strategy Encapsulates an algorithm inside a class
Template Method Defer the exact steps of an algorithm to a subclass
Visitor Defines a new operation to a class without change
http://www.dofactory.com/Patterns/Patterns.aspx
Design Patterns
Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.
The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. Here you will find information on these important patterns.
To give you a head start, the C# source code is provided in 2 forms: 'structural' and 'real-world'. Structural code uses type names as defined in the pattern definition and UML diagrams. Real-world code provides real-world programming situations where you may use these patterns.
A third form, '.NET optimized' demonstrates design patterns that exploit built-in .NET 4.0 features, such as, generics, attributes, delegates, object and collection initializers, automatic properties, and reflection. These and much more are available in our Design Pattern Framework 4.0TM. See our Singleton page for a .NET 4.0 Optimized code sample.
Creational Patterns:
Abstract Factory Creates an instance of several families of classes
Builder Separates object construction from its representation
Factory Method Creates an instance of several derived classes
Prototype A fully initialized instance to be copied or cloned
Singleton A class of which only a single instance can exist
Structural Patterns:
Adapter Match interfaces of different classes
Bridge Separates an object’s interface from its implementation
Composite A tree structure of simple and composite objects
Decorator Add responsibilities to objects dynamically
Facade A single class that represents an entire subsystem
Flyweight A fine-grained instance used for efficient sharing
Proxy An object representing another object
Behavioral Patterns:
Chain of Resp. A way of passing a request between a chain of objects
Command Encapsulate a command request as an object
Interpreter A way to include language elements in a program
Iterator Sequentially access the elements of a collection
Mediator Defines simplified communication between classes
Memento Capture and restore an object's internal state
Observer A way of notifying change to a number of classes
State Alter an object's behavior when its state changes
Strategy Encapsulates an algorithm inside a class
Template Method Defer the exact steps of an algorithm to a subclass
Visitor Defines a new operation to a class without change
Labels:
Design Pattern
Encrypt and Decrypt Xml and Text File
using System.Security.Cryptography;
/* System.Security.Cryptography namespace
Microsoft provides cryptographic services, including secure encoding and decoding of data.
*/
//hash algorithm are applied to encrpt the string and store in un-readable format..
public XmlDocument OpenXmlDocument(String filePath)//Decrypt Xml File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray
if (useHashing){
//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlData);
return xDoc;
}
public void SaveXmlDocument(XmlDocument xmldocument, string FilePath, bool EncryptXmlFile)//EncryptXmlFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(xmldocument.OuterXml);//convert Xml file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptXmlFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.
ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}
public void SaveTextDocument(string Textdocument, string FilePath, bool EncryptTextFile)//EncryptTextFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(Textdocument);//convert Text file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptTextFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.
ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}
protected void btnGenerateXml_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
//........for check if xml file before Encryption
StreamReader sr = new StreamReader(FilePath);
String str = sr.ReadToEnd();
sr.Close();
string[] k = str.Split(' ');
string s = k[0];
if (str.Contains("<"))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FilePath);
SaveXmlDocument(xmlDoc, FilePath, true);
}
//...........................
//if (s == " // {
// }
}
protected void Read_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
XmlDocument xResultDoc = OpenXmlDocument(FilePath);
}
protected void btnGenerateText_Click(object sender, EventArgs e)
{
string FilePath1 = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
StreamReader streamReader = new StreamReader(FilePath1);
string text = streamReader.ReadToEnd();
streamReader.Close();
SaveTextDocument(text, FilePath1, true);
}
protected void btnReadText_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
string xResultDoc = OpenTextDocument(FilePath);
}
public string OpenTextDocument(String filePath) //Decrypt text File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray
if (useHashing)
{
//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
string s= UTF8Encoding.UTF8.GetString(resultArray);
return s;
//string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
//XmlDocument xDoc = new XmlDocument();
//xDoc.LoadXml(xmlData);
//return xDoc;
}
/* System.Security.Cryptography namespace
Microsoft provides cryptographic services, including secure encoding and decoding of data.
*/
//hash algorithm are applied to encrpt the string and store in un-readable format..
public XmlDocument OpenXmlDocument(String filePath)//Decrypt Xml File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray
if (useHashing){
//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlData);
return xDoc;
}
public void SaveXmlDocument(XmlDocument xmldocument, string FilePath, bool EncryptXmlFile)//EncryptXmlFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(xmldocument.OuterXml);//convert Xml file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptXmlFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.
ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}
public void SaveTextDocument(string Textdocument, string FilePath, bool EncryptTextFile)//EncryptTextFile will be true
{
// bool useHashing = true;
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(Textdocument);//convert Text file into ByteArray
string Key = "Subhash"; //set the key here for encrypting
if (EncryptTextFile)
{
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);//convert into ByteArray
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;//Gets or sets the mode for operation of the symmetric algorithm
tdes.Padding = PaddingMode.PKCS7;//Gets or sets the padding mode used in the symmetric algorithm.
ICryptoTransform cTransform = tdes.CreateEncryptor();//for Encrypting Data.
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
StreamWriter _sw = new StreamWriter(FilePath);//For write Encrypted file into given Path
_sw.WriteLine(Convert.ToBase64String(resultArray, 0, resultArray.Length));
_sw.Flush();
_sw.Close();
_sw.Dispose();
}
protected void btnGenerateXml_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
//........for check if xml file before Encryption
StreamReader sr = new StreamReader(FilePath);
String str = sr.ReadToEnd();
sr.Close();
string[] k = str.Split(' ');
string s = k[0];
if (str.Contains("<"))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(FilePath);
SaveXmlDocument(xmlDoc, FilePath, true);
}
//...........................
//if (s == " // {
// }
}
protected void Read_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "QueryMaster.xml");
XmlDocument xResultDoc = OpenXmlDocument(FilePath);
}
protected void btnGenerateText_Click(object sender, EventArgs e)
{
string FilePath1 = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
StreamReader streamReader = new StreamReader(FilePath1);
string text = streamReader.ReadToEnd();
streamReader.Close();
SaveTextDocument(text, FilePath1, true);
}
protected void btnReadText_Click(object sender, EventArgs e)
{
string FilePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Configuration"), "a.txt");
string xResultDoc = OpenTextDocument(FilePath);
}
public string OpenTextDocument(String filePath) //Decrypt text File
{
bool useHashing = true;
string Key = "Subhash";
byte[] keyArray;
StreamReader sr = new StreamReader(filePath);//here we read the file of given Path
String str = sr.ReadToEnd();
sr.Close();
byte[] toEncryptArray = Convert.FromBase64String(str);//convert string into ByteArray
if (useHashing)
{
//All Hash function take input of type Byte[]
//To generate a hash value, create an instance of a hash algorithm and call ComputeHash() on it.
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//The ComputeHash method accepts only an array of bytes or a stream..
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));//key also convert into ByteArray
hashmd5.Clear();//Releases all resources used by the HashAlgorithm class
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();//Data Encryption/Decpription Standard
tdes.Key = keyArray;//Gets or sets the secret key for the TripleDES algorithm.
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();//for Decrypting Data
//TransformFinalBlock:Transforms(Change) the specified region of the specified byte array.
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
string s= UTF8Encoding.UTF8.GetString(resultArray);
return s;
//string xmlData = UTF8Encoding.UTF8.GetString(resultArray);
//XmlDocument xDoc = new XmlDocument();
//xDoc.LoadXml(xmlData);
//return xDoc;
}
Subscribe to:
Posts (Atom)