C# DES

news/2024/7/4 15:29:24
using System;
//这个是使用DES的基础
using System.Security.Cryptography;
//这个是处理文字编码的前提
using System.Text;
//以“流”的形式处理文字,也是微软DES算法要求的
using System.IO;
/// <summary>
/// DES加密方法
/// </summary>
/// <param name="strPlain">明文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>密文</returns>
public string DESEncrypt(string strPlain,string strDESKey,string strDESIV)
{
//把密钥转换成字节数组
byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey);
//把向量转换成字节数组
byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV);
//声明1个新的DES对象
DESCryptoServiceProvider desEncrypt=new DESCryptoServiceProvider();
//开辟一块内存流
MemoryStream msEncrypt=new MemoryStream();
//把内存流对象包装成加密流对象
CryptoStream csEncrypt=new CryptoStream(msEncrypt,desEncrypt.CreateEncryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Write);
//把加密流对象包装成写入流对象
StreamWriter swEncrypt=new StreamWriter(csEncrypt);
//写入流对象写入明文
swEncrypt.WriteLine(strPlain);
//写入流关闭
swEncrypt.Close();
//加密流关闭
csEncrypt.Close();
//把内存流转换成字节数组,内存流现在已经是密文了
byte[] bytesCipher=msEncrypt.ToArray();
//内存流关闭
msEncrypt.Close();
//把密文字节数组转换为字符串,并返回
return UnicodeEncoding.Unicode.GetString(bytesCipher);
}
/// <summary>
/// DES解密方法
/// </summary>
/// <param name="strCipher">密文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>明文</returns>
public string DESDecrypt(string strCipher,string strDESKey,string strDESIV)
{
//把密钥转换成字节数组
byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey);
//把向量转换成字节数组
byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV);
//把密文转换成字节数组
byte[] bytesCipher=UnicodeEncoding.Unicode.GetBytes(strCipher);
//声明1个新的DES对象
DESCryptoServiceProvider desDecrypt=new DESCryptoServiceProvider();
//开辟一块内存流,并存放密文字节数组
MemoryStream msDecrypt=new MemoryStream(bytesCipher);
//把内存流对象包装成解密流对象
CryptoStream csDecrypt=new CryptoStream(msDecrypt,desDecrypt.CreateDecryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Read);
//把解密流对象包装成读出流对象
StreamReader srDecrypt=new StreamReader(csDecrypt);
//明文=读出流的读出内容
string strPlainText=srDecrypt.ReadLine();
//读出流关闭
srDecrypt.Close();
//解密流关闭
csDecrypt.Close();
//内存流关闭
msDecrypt.Close();
//返回明文
return strPlainText;
}

转载于:https://www.cnblogs.com/satng/archive/2009/07/11/2138922.html


http://www.niftyadmin.cn/n/3297880.html

相关文章

raft算法与paxos算法相比有什么优势,使用场景有什么差异?

raft利用日志连续性对Paxos做了大量很好的简化&#xff0c;但是其中有一点有很强的误导性&#xff0c;就是新任leader对于旧日志的处理&#xff0c;他的原文描述是“Raft uses a simpler approach where it guarantees that all the committed entries from previous terms are…

前端词汇大全

A absolute 绝对的active 活动的&#xff0c;激活的&#xff0c;<a>标记的一个伪类align 对齐alpha 透明度&#xff0c;半透明anchor 锚记<a>标记是这个单词的缩写arrow 箭头auto 自动Bbackground 背景border 边框banner 页面上的一个横条both 二者都是clear 属性的…

ActiveReports for .NET 简单使用

一. ActiveReports for .net的主要功能介绍 支持ASP.NET-ActiveReports在WebForms应用程序中通过使用ASP.NET 服务器控件来使用。这个控件支持分布式web报表&#xff0c;可使用HTML&#xff0c;ActiveX&#xff0c;.NET 和PDF浏览器。 完全代码集成-使用Microsoft Visual S…

基于ASP.NET的开源Blog程序总结

转自&#xff1a;http://www.livelog.org/article.aspx?id2 .Text http://www.telligentsystems.com/Solutions/Forums/ 多用户 ASP.NET 1.1 SQL Server 2000 开源 更新情况&#xff1a; 英文原版已经并入Community Server。博客园&#xff08;http://cnblogs.com &#xff0…

大数据下的用户行为分析

1. Consumer behaviour is the study of when&#xff0c;why&#xff0c;how and where people do or dont buy a product。 用户行为一般指用户通过中间资源&#xff0c;购买、使用和评价某种产品的记录。同时辅以用户、资源、产品自身及环境的信息。 用户行为记录一般可以表…

C#概念整理

一、C#中多继承的问题C# 不支持对类的多继承&#xff0c;每个类只能继承一个类&#xff1b;但支持多继承于接口&#xff0c;即每个类可以多继承于多个接口&#xff1b;二、C# 中结构体和类的区别c 中结构体和类的区别主要在于默认的可见程度不同。结构体的默认是public&#xf…

Stack栈和Heap堆的区别[转]

面试题常考题Stack—栈&#xff0c;Heap—堆。 堆存储&#xff1a; heapstorage 堆存储分配&#xff1a; heapstorage allocation 堆存储管理&#xff1a; heap storage management栈编址&#xff1a; stack addressing 栈变换&#xff1a;stack transformation 栈存储器&am…

Hadoop项目实战-用户行为分析之分析与设计

Hadoop项目实战&#xff0d;用户行为分析之分析与设计 http://www.cnblogs.com/smartloli/p/4569882.html 1.概述 本课程的视频教程地址&#xff1a;《用户行为分析之分析与设计》 下面开始本教程的学习&#xff0c;本教程以用户行为分析案例为基础&#xff0c;带着大家对项目的…