您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#加解密之DES算法怎么實現的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#加解密之DES算法怎么實現文章都會有所收獲,下面我們一起來看看吧。
使用DES加密方式加解密文本數據
開發工具:Visual Studio 2013
.NET Framework版本:4.5
public class DesUtil { /// <summary> /// DES加密 /// </summary> /// <param name="desModel"></param> /// <returns></returns> public static byte[] Encrypt(DesModel desModel) { //使用8位密鑰 byte[] key8 = new byte[8]; //如果我們的密鑰不是8為,則自動補全到8位 byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length)); //復制密鑰 Array.Copy(byteKey, key8, key8.Length); //使用8位向量 byte[] iv8 = new byte[8]; //如果我們的向量不是8為,則自動補全到8位 byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length)); //復制向量 Array.Copy(byteIv, iv8, iv8.Length); // 創建加密對象 DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); desProvider.Mode = desModel.Mode; desProvider.Padding = desModel.Padding; desProvider.Key = key8; desProvider.IV = iv8; byte[] result = null; try { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream EncryptStream = new CryptoStream(ms, desProvider.CreateEncryptor(), CryptoStreamMode.Write)) { EncryptStream.Write(desModel.Data, 0, desModel.Data.Length); EncryptStream.FlushFinalBlock(); result = ms.ToArray(); } } } catch { } return result; } /// <summary> /// DES解密 /// </summary> /// <param name="desModel"></param> /// <returns></returns> public static byte[] Decrypt(DesModel desModel) { //使用8位密鑰 byte[] key8 = new byte[8]; //如果我們的密鑰不是8為,則自動補全到8位 byte[] byteKey = Encoding.UTF8.GetBytes(desModel.Key.PadRight(key8.Length)); //復制密鑰 Array.Copy(byteKey, key8, key8.Length); //使用8位向量 byte[] iv8 = new byte[8]; //如果我們的向量不是8為,則自動補全到8位 byte[] byteIv = Encoding.UTF8.GetBytes(desModel.IV.PadRight(iv8.Length)); //復制向量 Array.Copy(byteIv, iv8, iv8.Length); // 創建解密對象 DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider(); desProvider.Mode = desModel.Mode; desProvider.Padding = desModel.Padding; desProvider.Key = key8; desProvider.IV = iv8; byte[] result = null; try { using (MemoryStream ms = new MemoryStream(desModel.Data)) { using (CryptoStream DecryptStream = new CryptoStream(ms, desProvider.CreateDecryptor(), CryptoStreamMode.Read)) { using (MemoryStream msResult = new MemoryStream()) { byte[] temp = new byte[1024 * 1024]; int len = 0; while ((len = DecryptStream.Read(temp, 0, temp.Length)) > 0) { msResult.Write(temp, 0, len); } result = msResult.ToArray(); } } } } catch { } return result; } /// <summary> /// DES加密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Encrypt(string data, string key, string iv = "") { byte[] bytes = Encoding.UTF8.GetBytes(data); byte[] result = Encrypt(new DesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Convert.ToBase64String(result); } /// <summary> /// DES解密字符串 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Decrypt(string data, string key, string iv = "") { byte[] bytes = Convert.FromBase64String(data); byte[] result = Decrypt(new DesModel { Data = bytes, Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }); if (result == null) { return ""; } return Encoding.UTF8.GetString(result); } public class DesModel { /// <summary> /// 需要加密/解密的數據 /// </summary> public byte[] Data { get; set; } /// <summary> /// 密鑰 /// </summary> public string Key { get; set; } /// <summary> /// 向量 /// </summary> public string IV { get; set; } /// <summary> /// 加密模式 /// </summary> public CipherMode Mode { get; set; } /// <summary> /// 填充模式 /// </summary> public PaddingMode Padding { get; set; } } }
private void btn_Des_Encrypt_Click(object sender, EventArgs e) { string result = DesUtil.Encrypt(textBox1.Text, "12345678900987654321"); textBox2.Text = result; } private void btn_Des_Decrypt_Click(object sender, EventArgs e) { string result = DesUtil.Decrypt(textBox2.Text, "12345678900987654321"); textBox1.Text = result; }
關于“C#加解密之DES算法怎么實現”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#加解密之DES算法怎么實現”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。