|
|
.NET programming Questions and Answers
|
|
.NET is a common purpose programming platform. It can be used for any workload or any sort of application where general purpose solutions are used. Questions include code examples that enough to understand the problems.
|
|
|
|
How to encrypt and decrypt a string?
The troublesome part about string encryption actually deals with the keys and not the algorithms. You should be worry about where you store your keys, and if you have to, how you exchange them.
|
We will not bother and go deep into the jungle. We will just show you how to decrypt and encrypt a string by using a wrapper object to access the cryptographic service provider (CSP) version of the Data Encryption Standard (DES) algorithm.
|
public string EncryptString(string inputString){
MemoryStream ms = null;
try{
byte[] IV = {12, 43, 67, 27, 17, 21, 57, 35};
string encryptKey = "password"; // 8 characters key
byte[] key = {};
key = Encoding.UTF8.GetBytes(encryptKey);
byte[] byteInput = Encoding.UTF8.GetBytes(inputString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
ms = new MemoryStream();
ICryptoTransform transform = provider.CreateEncryptor(key, IV);
CryptoStream cryptoStream = new CryptoStream(ms, transform, CryptoStreamMode.Write);
cryptoStream.Write(byteInput, 0, byteInput.Length);
cryptoStream.FlushFinalBlock();
}
catch (Exception ex){
Response.Write(ex.Message);
}
return Convert.ToBase64String(ms.ToArray());
}
public string DecryptString(string inputString){
MemoryStream ms = null;
try{
byte[] IV = {12, 43, 67, 27, 17, 21, 57, 35};
string decryptKey = "password"; // 8 characters key
byte[] key = {};
key = Encoding.UTF8.GetBytes(decryptKey);
byte[] byteInput = new byte[inputString.Length];
byteInput = Convert.FromBase64String(inputString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
ms = new MemoryStream();
ICryptoTransform transform = provider.CreateDecryptor(key, IV);
CryptoStream cryptoStream = new CryptoStream(ms, transform, CryptoStreamMode.Write);
cryptoStream.Write(byteInput, 0, byteInput.Length);
cryptoStream.FlushFinalBlock();
}
catch (Exception ex){
Response.Write(ex.Message);
}
Encoding encoding1 = Encoding.UTF8;
return encoding1.GetString(ms.ToArray());
}
|
|
Send email in .NET via a public email account like Yahoo or Gmail
Instead of sending emails from your host, you may suggest of sending your email messages though a public account (Gmail, Yahoo, etc.). How to implement such emailing jobs?
|
Use Gmail Smtp client which allows you to send email messages by using SMTP Protocol.
|
|
The difference between String and string in C#
What is the differences between: string s = "Hello world!"; and String S = "Hello world!";? What are the directions for the use of each?
|
The string is an alias in C# for System.String. So technically, there is no difference.
So the String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in MSIL (Intermediate Language), so there is no difference. Choose what you like and use that.
We can mention the same about (int, System.Int32)
Howevere... :)
string is a reserved word, but String is just a class name. This means that string cannot be used as a variable name by itself.
If for some reason you wanted a variable called string, you would see only the first of these compiles:
|
StringBuilder String = new StringBuilder(); // compiles
StringBuilder string = new StringBuilder(); // does not compile
|
If you really want a variable name called string you can use @ as a prefix:
|
StringBuilder @string = new StringBuilder();
|
|