C# Project Tools
This project is converted and modified from the
VB.Net Project Tools. Since I need a Common Class in C# projects replacing VB.Net's Common Module (including the source code in C# rather than using the DLL built by VB.Net), I've to build this one. It's a good practice coding in C#, anyway.
Three of the classes are very useful for DotNet developers. The RegisterKey class can be used to generate, verify, encrypt and save Register Key in App.Config file. The Crypto class can be used to encrypt / decrypt files or string for security. The AppConfig class can be used to manage DotNet's default Config Settings. For more info, see
VB.Net Project Tools (they're not identical).
Using these classes, you can, for instance as shown by the test EXE, generate the Register Key, encrypt it, save it in ConfigFile, verify it late. In this way, you can protect your product professionally by the Key.
The Crypto Class can encrypt and decrypt text and binary file, which can be used to backup data and file, database records, protect privacy at work, distribute user's products, encode the QueryString or Form Submit (convert to/from Base64 or HEX supported) ...
It works also for ASP.Net so that you can pass secured data to / from other pages or forms; or using the Web.Config to store the encrypted Database connection string, passwords; to encrypt your web or program's output text or binary file for security reasons, or vice versa.
The Validate Class uses RegExp only (except the IsValidCCNumber used to validate the Credit Card Number), which are some conversions of VB6 functions.
The Converting Class is used for other classes. All methods there are Public, so user can benefit them.
The CSTool_Test.EXE is a simple app used to test the DLL regardless Trial or Registered. Its source code is included in the downloaded ZIP.
The Zipped Files (¡À65 KB, including DLL, a test EXE and its source code) are
available to download here.
The Dependencies and Requirments: DotNet Framework 1.1 (
free download from Microsoft).
The XML Comment Doc: shows the inline <summary> of each public items in the DLL in details (place it at the same folder as the DLL for the intellisense tooltip).
The Class Reference (ASPSimply.CSTool.DLL):
 |
|
 |
|
|
|
| Class: RegisterKey; ASPSimply.CSTool.RegisterKey mcRK = new ASPSimply.CSTool.RegisterKey() |
| Property / Method |
Type |
In/Out |
Default |
Description / Arguments |
| CryptoKey * |
string |
Input |
"MoreSecrets" |
Property; for en/decrypt RegisterKey Name/Value; [a-zA-Z0-9], >= 4 |
| IsKeyNameEncrypted |
bool |
Input |
false |
Property; whether en/decrypt RegisterKey Name |
| IsKeyValueEncrypted |
bool |
Input |
false |
Property; whether en/decrypt RegisterKey Value |
| GenerateRegisterKey * |
string |
Output |
- |
(string KeyPassword, int RegisterKeyLength) |
| IsRegisterKeyValid * |
bool |
Output |
- |
(string RegisterKey, string KeyPassword) |
| SaveRegisterKey |
void |
- |
- |
(string RegisterKey, string ConfigFile, string ConfigTag, string ConfigKey) |
| GetRegisterKey |
string |
Output |
DefaultValue |
(string ConfigFile, string ConfigTag, string ConfigKey, string DefaultValue) |
| VerifyConfigRegisterKey * |
bool |
Output |
- |
(string KeyPassword, string ConfigFile, string ConfigTag, string ConfigKey) |
| DeleteRegisterKey |
void |
- |
- |
(string ConfigFile, string ConfigTag, string ConfigKey) |
| Arguments |
KeyPassword: [a-zA-Z0-9], length >= 4; RegisterKeyLength >= 16 (default 25) |
| Class: Crypto; ASPSimply.CSTool.Crypto mcCry = new ASPSimply.CSTool.Crypto() |
| Property / Method |
Type |
In/Out |
Default |
Description / Arguments |
| CryptoKey * |
string |
Input |
"MoreSecrets" |
Property; for en/decrypt file/string; [a-zA-Z0-9], length >= 4 |
| CryptoType |
CSPType |
Input |
CSPType.DES |
Property; CryptoType: CSPType.DES, .TripleDES, .Rijndael or .RC2 |
| CryptoIV |
string |
Input |
"" |
Property; Initialization Vector; Optional, use built-in Generator if Empty |
| SourceString |
string |
Input |
- |
Property; Text String to be encrypted or decrypted |
| SourceFile |
string |
Input |
- |
Property; File (path + name) to be encrypted or decrypted |
| TargetFile |
string |
Input |
- |
Property; File (path + name) after encryption or decryption |
| DeleteSource |
bool |
Input |
false |
Property; whether delete the SourceFile after en/decryption |
| UseHEX |
bool |
Input |
false |
Property; whether convert en/decrypted string to/from HEX format |
| UseBase64 |
bool |
Input |
true |
Property; whether convert en/decrypted string to/from Base64 |
| CSPType |
enum |
- |
- |
Enumerates CSP Crypto Type: DES, TripleDES, Rijndael, RC2 |
| EncryptFile() |
void |
- |
- |
Encrypt the SourceFile to the TargetFile |
| DecryptFile() |
void |
- |
- |
Decrypt the SourceFile to the TargetFile |
| EncryptString() |
string |
Output |
- |
Encrypt the SourceString; return encrypted string |
| DecryptString() |
string |
Output |
- |
Decrypt the SourceString; return: decrypted string |
| EncryptByte() |
byte[] |
Output |
- |
Encrypt the SourceString; return: encrypted Byte Array |
| DecryptByte() |
byte[] |
Output |
- |
Decrypt the SourceString; return: decrypted Byte Array |
| Class: AppConfig #; ASPSimply.CSTool.AppConfig mcAC = new ASPSimply.CSTool.AppConfig() |
| Property / Method |
Type |
In/Out |
Default |
Description / Arguments |
| CryptoKey * |
string |
Input |
"MoreSecrets" |
Property; for en/decrypt RegisterKey Name/Value; [a-zA-Z0-9], length >= 4 |
| IsKeyNameEncrypted |
bool |
Input |
false |
Property; whether en/decrypt RegisterKey Name |
| IsKeyValueEncrypted |
bool |
Input |
false |
Property; whether en/decrypt RegisterKey Value |
| IsCaseSensitive |
bool |
Input |
false |
Property; whether Key and ConfigTag are Case-Sensitive |
| SaveSetting |
void |
- |
- |
(string ConfigFile, string ConfigTag, string Key, string Value) |
| GetSetting |
string |
Output |
DefaultValue |
(string ConfigFile, string ConfigTag, string Key, string DefaultValue) |
| DeleteSetting |
void |
- |
- |
(string ConfigFile, string ConfigTag, string Key) |
| ContainKey |
bool |
Output |
- |
(string ConfigFile, string ConfigTag, string Key) |
| ContainTag |
bool |
Output |
- |
(string ConfigFile, string ConfigTag) |
| DeleteTag |
void |
- |
- |
(string ConfigFile, string ConfigTag) |
| GetTagNames |
string[] |
Output |
- |
(string ConfigFile) |
| Arguments |
Key and Value are the setting pair in XML format; DefaultValue return Value if Key not exists (default empty string "") |
| Class: WinRegistry; ASPSimply.CSTool.WinRegistry mcWR = new ASPSimply.CSTool.WinRegistry() |
| Property / Method |
Type |
In/Out |
Default |
Description / Arguments |
| RootKeyName |
string |
Input |
"LocalMachine" |
Property; Registry Root: "ClassesRoot", "CurrentUser", "LocalMachine", "Users", "PerformanceData", "CurrentConfig" or "DynData" |
| SubKeyPath |
string |
Input |
"SOFTWARE\Microsoft" |
Property; Registry Subkey Path Name |
| CreateSubKey |
void |
- |
- |
(string SubKeyName) |
| DeleteSubKey |
void |
- |
- |
(string SubKeyName) |
| SearchSubKey |
string |
Output |
- |
(string SubKeyName) |
| ContainSubKey |
bool |
Output |
- |
(string SubKeyName, bool IsCaseSensitive) |
| SetValue |
void |
- |
- |
(string ValueName, string ValueData) |
| GetValue |
string |
Output |
- |
(string ValueName) |
| DeleteValue |
void |
- |
- |
(string ValueName) |
| SearchValueName |
string |
Output |
- |
(string ValueName) |
| ContainValueName |
bool |
Output |
- |
(string ValueName, bool IsCaseSensitive) |
| Arguments |
ValueName and ValueData are the Name/Data pair in Registry; SubKeyName: Registry Subkey Name; IsCaseSensitive: Whether use Case-Sensitive to search Key/Value Name |
| Class: Converting; ASPSimply.CSTool.Converting mcCon = new ASPSimply.CSTool.Converting() |
| Method |
Type |
In/Out |
Default |
Description / Arguments |
| StringToBase64 |
string |
Output |
- |
(string SourceString) |
| ByteToBase64 |
string |
Output |
- |
(byte[] ByteArray) |
| Base64ToString |
string |
Output |
- |
(string SourceString) |
| StringToByteArray |
byte[] |
Output |
- |
(string SourceString, int CodePage) |
| ByteArrayToString |
string |
Output |
- |
(byte[] ByteArray, int CodePage) |
| StringToHex |
string |
Output |
- |
(string SourceString, bool ToUpperCase); convert SourceString to HEX format |
| HexToString |
string |
Output |
- |
(string SourceString); convert SourceString from HEX format |
| ByteToHex |
string |
Output |
- |
(byte[] SourceByte); convert SourceByte (byte array) to HEX format |
| IPStringToLong |
long |
Output |
- |
(string IP); IP string to IP long int |
| IPLongToString |
string |
Output |
- |
(long IP); IP long int to IP string |
| CharToASCII |
int |
Output |
- |
(string SourceString, int CharPosition, int CodePage) |
| RightString |
string |
Output |
- |
(string SourceString, int RightPosition); equivalent of Right$(string, int) in VB6 |
| Class: Validate +; ASPSimply.CSTool.Validate mcVal = new ASPSimply.CSTool.Validate() |
| Method |
Type |
In/Out |
Default |
Description / Arguments |
| IsValidInteger |
bool |
Output |
- |
(string Data); valid string "123" |
| IsNegativeInteger |
bool |
Output |
- |
(string Data); valid string "-123" |
| IsValidDecimal |
bool |
Output |
- |
(string Data); valid "1.1", ".1", "1." |
| IsNegativeDecimal |
bool |
Output |
- |
(string Data); valid "-12.3", "-.3", "-12." |
| IsValidWordChar |
bool |
Output |
- |
(string Data); valid [a-zA-Z0-9] |
| IsValidUserName |
bool |
Output |
- |
(string Data); valid [a-zA-Z0-9_.-] |
| IsValidEmail |
bool |
Output |
- |
(string Data); validate E-mail address |
| IsValidIPAddress |
bool |
Output |
- |
(string Data); valid "255.0.123.321" |
| IsValidCCNumber |
bool |
Output |
- |
(long CCNumber); validate Credit Card Number (CCNumber) | |
|
 |
|
 |
Notice: Most methods have their Overloads by using some default arguments, i.e. by default:
ConfigTag = "appSettings"; ConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, or "Web.Config" for ASP.Net; Crypto.UseBase64 = true by default instead of UseHEX since Base64 works fine even for binary file.
* KeyPassword and CryptoKey recommended 6-20 long Word Characters [a-zA-Z0-9]. In the Trial, KeyPassword = "TooManySecrets", CryptoKey = "MoreSecrets" by using their default values only.
# The AppConfig class is pretty like the default settings in VB6, but no longer using the Windows Registry.
+ All methods in class Validate uses the RegExp, except IsValidCCNumber.
Some C# Sample Code: shows how to use the DLL in C#, self-explained, similar code in J# and VB.Net. For more codes, check out the downloaded ZIP. For ASP.Net, the code is very similar, just avoid the WinRegistry Class (see
ASP.Net Crypto). For VBNet, have a look of the
VB.Net Porject Tools.
// initialize 3 classes at Module level
ASPSimply.CSTool.RegisterKey mcRK = new ASPSimply.CSTool.RegisterKey();
ASPSimply.CSTool.AppConfig mcAC = new ASPSimply.CSTool.AppConfig();
ASPSimply.CSTool.Crypto mcCry = new ASPSimply.CSTool.Crypto();
private void GenerateRegisterKey()
{
mcRK.CryptoKey = textBox4.Text;
mcRK.IsKeyValueEncrypted = checkBox2.Checked;
textBox1.Text = mcRK.GenerateRegisterKey(textBox2.Text.Trim(), Convert.ToInt32(textBox3.Text));
textBox1.Focus();
}
private void IsRegisterKeyValid()
{
mcRK.CryptoKey = textBox4.Text;
mcRK.IsKeyValueEncrypted = checkBox2.Checked;
MessageBox.Show(mcRK.IsRegisterKeyValid(textBox1.Text, textBox2.Text).ToString());
}
private void EncryptStringFile()
{
mcCry.CryptoKey = textBox5.Text;
mcCry.SourceString = textBox6.Text; // encrypt string
mcCry.UseBase64 = checkBox7.Checked; // default true, Base64 is OK for binary file
if (!checkBox7.Checked) // only one of them
mcCry.UseHEX = checkBox3.Checked;
textBox7.Text = mcCry.EncryptString();
textBox7.Focus();
if (checkBox4.Checked) // encrypt file
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.Title = "Open file to encrypt ...";
OFD.Filter = "All files (*.*)|*.*";
OFD.ShowReadOnly = true;
if (OFD.ShowDialog() == DialogResult.OK) {
mcCry.SourceFile = OFD.FileName;
OFD = null;
SaveFileDialog SFD = new SaveFileDialog();
SFD.Title = "Save encrypted file as ...";
SFD.Filter = "All files (*.*)|*.*";
if (SFD.ShowDialog() == DialogResult.OK)
{
mcCry.TargetFile = SFD.FileName;
SFD = null;
mcCry.EncryptFile();
}
}
}
}
private void DecryptStringFile()
{
mcCry.CryptoKey = textBox5.Text;
mcCry.SourceString = textBox7.Text; // decrypt string
mcCry.UseBase64 = checkBox7.Checked; // default true, Base64 is OK for binary file
if (!checkBox7.Checked) // only one of them
mcCry.UseHEX = checkBox3.Checked;
textBox6.Text = mcCry.DecryptString();
textBox6.Focus();
if (checkBox4.Checked) // decrypt file
{
OpenFileDialog OFD = new OpenFileDialog();
OFD.Title = "Open file to decrypt ...";
OFD.Filter = "All files (*.*)|*.*";
OFD.ShowReadOnly = true;
if (OFD.ShowDialog() == DialogResult.OK)
{
mcCry.SourceFile = OFD.FileName;
OFD = null;
SaveFileDialog SFD = new SaveFileDialog();
SFD.Title = "Save decrypted file as ...";
SFD.Filter = "All files (*.*)|*.*";
if (SFD.ShowDialog() == DialogResult.OK)
{
mcCry.TargetFile = SFD.FileName;
SFD = null;
mcCry.DecryptFile();
}
}
}
}
private void SaveSetting()
{
mcAC.CryptoKey = textBox8.Text;
mcAC.IsConfigKeyEncrypted = checkBox6.Checked;
mcAC.IsConfigValueEncrypted = checkBox5.Checked;
mcAC.IsCaseSensitive = false; // default
mcAC.SaveSetting(textBox9.Text, textBox10.Text, textBox12.Text, textBox11.Text);
}
private void GetSetting()
{
mcAC.CryptoKey = textBox8.Text;
mcAC.IsConfigKeyEncrypted = checkBox6.Checked;
mcAC.IsConfigValueEncrypted = checkBox5.Checked;
// mcAC.IsCaseSensitive = false; // default
textBox11.Text = mcAC.GetSetting(textBox9.Text, textBox10.Text, textBox12.Text, "");
}
The Trial vs. Registered Version: The Trial using the downloaded ASPSimply.CSTool.DLL, the KeyPassword and CryptoKey will use the default values only ("TooManySecrets" and "MoreSecrets", user can't change them), which can still generate lots different Register Keys and en/decrypt string/file, but no security.
To remove it, please
Register this Product, or
simply at PayPal 
, or
iKobo 
, or
BoC 
.
Thank you for your support !
See Also:
VB.Net Porject Tools (VB.Net, DLL)
VB6 Porject Security Tool (VB6, DLL)
Protected Files Backup (VB.Net, EXE)
Encrypt and Decrypt String (ASP.Net, Online Demo)
The Tips & Notes:
A general rule, use the same property sets to retrieve/delete/verify data as used when set/create/generate them.
Remember the KeyPassword and CryptoKey as well as others.
If not sure, use the default values or Overloads, then test a few times using the CSTool_Test.EXE.
Don't try to use any feature of Windows Registry when working on WebForm (ASP.Net).
When using Trial DLL, the [Key Password] and [Crypto Key] in the EXE are useless since only default values are used.
Make sure to add the standard App/Web.Config in your project, and it contains XML element <configuration> and <appSettings> though this tool will check and create them (VS won't create the App.Config for C#).
The Version & History:
V 1.0.4 (Oct. 2004): Enhanced Unicode encoding for Chinese and East Asian Characters.
V 1.0.0.3 (Feb. 2004): Set .UseBase64 = true as default instead of .UseHEX. Enabled inline compiled comments by the ASPSimply.CSTool.XML.
V 1.0.0.2 (Feb. 2004): Addon property UseBase64 in class Crypto, and Base64 methods in Converting class.
V 1.0.0.1 (Dec. 2003): Fix a bug when dealing with Web.Config for ASP.Net.
V 1.0.0.0 (Dec. 2003): Something for debugging.