資料加密標準(英語:Data Encryption Standard,縮寫為 DES)
https://goo.gl/52G6fK
他是一種對稱型的演算法
需要一把安全鑰匙與檢查鑰匙
都為8位元長度
但因為只有八位元
而且可以逆向反組譯
所以是不安全的,以現在技術來說是可以被破解的
目前銀行或ATM Center已經都不採用這種演算法了
接下來就用c#.Net來實作DES加密演算法
先設計一個簡單的UI介面
並且實作兩種字串的長度格式
一種是64位元的,加密後的字串比較精簡
一種是傳統的
假定安全鑰匙跟檢查鑰匙的密碼分別如下:
- public string strKey = "C.W.Kuan";
- public string strCheckCode = "19810313";
先示範第一種Base64的
原始資料輸入一段文字後,按下加密(Base64)後,得到加密後的結果資料如下圖
- private void btnEncrypt64_Click(object sender, EventArgs e)
- {
- txtResult.Text = desEncryptBase64(txtSource.Text, strKey, strCheckCode);
- }
- private string desEncryptBase64(string source, string strkey, string strCheckCode)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] key = Encoding.ASCII.GetBytes(strkey);
- byte[] iv = Encoding.ASCII.GetBytes(strCheckCode);
- byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
- des.Key = key;
- des.IV = iv;
- string encrypt = "";
- using (MemoryStream ms = new MemoryStream())
- using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(dataByteArray, 0, dataByteArray.Length);
- cs.FlushFinalBlock();
- encrypt = Convert.ToBase64String(ms.ToArray());
- }
- return encrypt;
- }
- private void btnDecrypt64_Click(object sender, EventArgs e)
- {
- txtResult.Text = desDecryptBase64(txtSource.Text, strKey, strCheckCode);
- }
- private string desDecryptBase64(string encrypt, string strkey, string strCheckCode)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] key = Encoding.ASCII.GetBytes(strkey);
- byte[] iv = Encoding.ASCII.GetBytes(strCheckCode);
- des.Key = key;
- des.IV = iv;
- byte[] dataByteArray = Convert.FromBase64String(encrypt);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(dataByteArray, 0, dataByteArray.Length);
- cs.FlushFinalBlock();
- return Encoding.UTF8.GetString(ms.ToArray());
- }
- }
- }
- private void btnEncrypt_Click(object sender, EventArgs e)
- {
- txtResult.Text = desEncrypt(txtSource.Text, strKey, strCheckCode);
- }
- private string desEncrypt(string source, string strkey, string strCheckCode)
- {
- StringBuilder sb = new StringBuilder();
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] key = Encoding.ASCII.GetBytes(strkey);
- byte[] iv = Encoding.ASCII.GetBytes(strCheckCode);
- byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
- des.Key = key;
- des.IV = iv;
- string encrypt = "";
- using (MemoryStream ms = new MemoryStream())
- using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(dataByteArray, 0, dataByteArray.Length);
- cs.FlushFinalBlock();
- //輸出資料
- foreach (byte b in ms.ToArray())
- {
- sb.AppendFormat("{0:X2}", b);
- }
- encrypt = sb.ToString();
- }
- return encrypt;
- }
- private void btnDecrypt_Click(object sender, EventArgs e)
- {
- txtResult.Text = desDecrypt(txtSource.Text, strKey, strCheckCode);
- }
- private string desDecrypt(string encrypt, string strkey, string strCheckCode)
- {
- byte[] dataByteArray = new byte[encrypt.Length / 2];
- for (int x = 0; x < encrypt.Length / 2; x++)
- {
- int i = (Convert.ToInt32(encrypt.Substring(x * 2, 2), 16));
- dataByteArray[x] = (byte)i;
- }
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] key = Encoding.ASCII.GetBytes(strkey);
- byte[] iv = Encoding.ASCII.GetBytes(strCheckCode);
- des.Key = key;
- des.IV = iv;
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(dataByteArray, 0, dataByteArray.Length);
- cs.FlushFinalBlock();
- return Encoding.UTF8.GetString(ms.ToArray());
- }
- }
- }
最終結果都正確
提供有興趣的人可以實作看看喔
沒有留言:
張貼留言