8.對字符串進行加密
輸入: abc
輸出: cvJ5W08AdsA=
Code
textBox1.ReadOnly = false;
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
textBox2.Text = Convert.ToBase64String(MStream.ToArray());
textBox3.Text = "";
textBox3.ReadOnly = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
9.對字符串進行解密 (與上面例子配合使用)
輸入: cvJ5W08AdsA=
輸出: abc
Code
textBox3.ReadOnly = false;
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Convert.FromBase64String(textBox2.Text.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
textBox3.Text = Encoding.Unicode.GetString(MStream.ToArray());
textBox1.Text = "";
textBox1.ReadOnly = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
10.區別 0, 空字符串, Null, Empty和 Nothing
(1).對於聲明後未賦值的數值類型變量,它們的默認值為0;
(2).對於聲明後未賦值的字符串變量,則缺省值為空字符串"";
(3).Null關鍵字說明變量不包含有效數據,它是將Null值顯式地賦值給變量的結果,也可能是包含Null的表達式之間進行運算的結果。
(4).Empty關鍵字表示未初始化的變量的缺省值。
(5).Nothing關鍵字用於將對象變量從實際對象中分離開來。
補充說明: 一些常用的字符串處理技術如首字母轉化為大寫 , 字符串比較, 添加子串等操作比較簡單, 此處略