You are looking at posts in the category CSharp.
Posted on February 2nd, 2006 by gernot.
Categories: CSharp.
A short tutorial how to encrypt, respectivelly decrypt a string in C# with Rijndael/AES.
NOTE: The Keygeneration is done in the encryption and decryption methods. For usage in a ‘real’ application this should be outsourced.
using System.Security.Cryptography;
// Input Values:
// pwdHash = “MD5″ / “SHA1″
// iv = “16 ASCII Characters”
// keySize = 128 / 192 / 256
public string Encrypt(string text, string pwdPhrase, string pwdSalt, string pwdHash,
int pwdIterations, string iv, int keySize)
{
byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
byte[] pwdSaltBytes = Encoding.ASCII.GetBytes(pwdSalt);
byte[] textBytes = Encoding.UTF8.GetBytes(text);
PasswordDeriveBytes pwd
= new PasswordDeriveBytes (pwdPhrase, pwdSaltBytes, pwdHash, pwdIterations);
byte[] keyBytes = pwd.GetBytes( keySize / 8 );
RijndaelManaged symmKey = new RijndaelManaged ();
symmKey.Mode = CipherMode.CBC;
ICryptoTransform enc
= symmKey.CreateEncryptor(keyBytes, ivBytes);
MemoryStream mem = new MemoryStream ();
CryptoStream cry
= new CryptoStream (mem, enc, CryptoStreamMode.Write);
cry.Write(textBytes, 0, textBytes.Length);
cry.FlushFinalBlock();
byte[] cipBytes = mem.ToArray();
mem.Close();
cry.Close();
return Convert.ToBase64String(cipBytes);
}
using System.Security.Cryptography;
// Input Values:
// pwdHash = “MD5″ / “SHA1″
// iv = “16 ASCII Characters”
// keySize = 128 / 192 / 256
public string Decrypt(string cip, string pwdPhrase, string pwdSalt, string pwdHash,
int pwdIterations, string iv, int keySize)
{
byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
byte[] pwdSaltBytes = Encoding.ASCII.GetBytes(pwdSalt);
byte[]cipTextBytes = Convert.FromBase64String(cip);
PasswordDeriveBytes pwd
= new PasswordDeriveBytes (pwdPhrase, pwdSaltBytes, pwdHash, pwdIterations);
byte[] keyBytes = pwd.GetBytes( keySize / 8 );
RijndaelManaged symmKey = new RijndaelManaged ();
symmKey.Mode = CipherMode.CBC;
ICryptoTransform dec
= symmKey.CreateDecryptor(keyBytes, ivBytes);
MemoryStream mem = new MemoryStream (cipTextBytes);
CryptoStream cry
= new CryptoStream (mem, dec, CryptoStreamMode.Read);
byte[] textBytes = new byte[cipTextBytes.Length];
int decByteCount
= cry.Read(textBytes, 0, textBytes.Length);
mem.Close();
cry.Close();
return Encoding.UTF8.GetString(textBytes, 0, decByteCount);
}
Posted on February 2nd, 2006 by gernot.
Categories: CSharp.
A small method for replacing an embedded wildcard in a string.
String: [GG] is 24 [Y] old
[GG] should be replaced with Gernot Goluch and [Y] with year
One possible usage would be including different IDs of xml elements or db entries in other entries by using wildcard delimiters in some content of the entry.
E.g. xml processing
‹testNode id=“123″›BlaBla‹/testNode›
‹testNode id=”456″›BlaBla and [123] is really dumb content‹/testNode›
After processing the content of testNode with id “456″ the content would be:
“BlaBla and Blabla is really dumb content”
The method call for the simple example above.
replaceWildCardInString(“[GG] is 24 [Y] old”,new char[] {‘[', ']‘});
The method for the simple example above.
public string replaceWildCardInString(string _string, char[] _wildCard) {
if (_string.IndexOf(_wildCard[0]) == -1) return _string;
else {
//split string and get content to replace
string[] contentSplit = _string.Split(_wildCard);
//What is inbetween the wildcards
string contentToReplace = contentSplit[1];
//The ‘real’ content
//***Add Code here how to get the real content***
//E.g.: very simple. Possible inclusion of db or xml query/method call
string contentReplace = “”;
if (contentToReplace.Equals(“GG”)) contentReplace = “Gernot Goluch”;
else if (contentToReplace.Equals(“Y”)) contentReplace = “year”;
//replace the wildcard with the ‘real’ content
//and check if other wildcards are in the content
WildCardClass wcc = new WildCardClass();
return wcc.replaceWildCardInString(
_string.Replace(
_wildCard[0] + contentToReplace + _wildCard[1]
, contentReplace)
, _wildCard);
}
}
Posted on January 30th, 2006 by gernot.
Categories: CSharp.
Tags: .NET, C#, code, schema, validation, xml
This is a short tutorial how one can validate a XML Document against a XML schema by using C# and the .NET framework.
using System.Xml;
using System.Xml.Schema;namespace XMLValidation
{
class XMLValidatorTest
{private bool XMLisValid = true;
public void ReadValidateXMLFile()
{
XmlTextReader reader = new XmlTextReader(“C:\\test.xml”);
XmlValidatingReader validator = new XmlValidatingReader(reader);
validator.ValidationType = ValidationType.Schema;
validator.ValidationEventHandler +=
new ValidationEventHandler(this.XMLValidationEventHandler);while (validator.Read())
{
//Add code here to process XML content
}
validator.Close();//Check if XML document is valid or not
if (this.XMLisValid)
Console.WriteLine(“XML Document is valid”);
else
Console.WriteLine(“XML Document is invalid”);
}
}public static void XMLValidationEventHandler(
object sender,ValidationEventArgs args)
{
this.XMLisValid = false;
Console.WriteLine(“XML Validation event” + args.Message);
}}
}
In the XML File:
‹?xml version=”1.0″ encoding=”utf-8″ ?›
‹testNS:testRoot
xmlns:testNS=”http://www.gego.info/testxmlns”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.gego.info/testxmlns test.xsd”›
In the XSD File:
‹?xml version=”1.0″ encoding=”utf-8″ ?›
‹xs:schema
id=”test”
targetNamespace=”http://www.gego.info/testxmlns”
elementFormDefault=”qualified”
attributeFormDefault=”unqualified”
xmlns=”http://www.gego.info/testxmlns”
xmlns:xs=”http://www.w3.org/2001/XMLSchema”›
Posted on January 25th, 2006 by gernot.
Categories: CSharp.
Tags: .NET, C#, code, gui
This is a short Tutorial how to create and use User Controls in the .NET Framework (C#):

Here I want to describe how one can fire an event (e.g.: from a button) in the UserControl and notify the Main Application, which uses the User Control:
In MyUserControl1.cs:
//Just a TestString
public string TestString = “Test123″;
// Declare delegate Button1 clicked.
public delegate void Button1_ClickHandler();
// Declare the event, which is associated with our delegate
[Category("Action")] [Description("Fires when the Button1 is clicked.")]
public event Button1_ClickHandler Button1_Clicked;
// Handler for Button1
// NOTE: Don’t forget to add this Handler to the Button itself
private void Button1_Click(object sender, System.EventArgs e)
{
if (Button1_Clicked != null)
// Notify Subscribers
Button1_Clicked();
else
Console.WriteLine(“No Subscribers for Button1″);
}
In Form1.cs:
// NOTE: Don’t forget to add this Handler to the UserControl itself
private void MyUserControl11_Button1_Clicked()
{
Console.WriteLine(“Button1 was clicked in MyUserControl11, TestString:”
+ MyUserControl11.TestString);
}
Posted on January 25th, 2006 by gernot.
Categories: CSharp.
Nachdem ich mich seit längerer Zeit ein mit C# und dem .NET Framework programmiere und selbst immer alle möglichen Dinge, die ich schon einmal gemacht habe vergesse, möchte ich in dieser Rubrik eine kleine Sammlung an nützlichen Code Snippets präsentieren bzw. Links zu guten Tutorials anbieten.