加密..................

//介绍:                                                    //
//    这是一个非常有趣的例子!                              //
//    在Ezone International公司里Ting是Dicky的女友,Viisen是//
//他们的上级.Viisen不允许公司内部员工谈恋爱,上有政策,下有对//
//策,Ting和Dicky利用空闲时间通过邮件的方式与对方通讯,Viisen//
//下发任务给Dicky的方式也是通过发送邮件.为了保存邮件的安全 //
//性,邮件采用RC2(对称加密)加密方式进行发送,RC2的密钥经过RSA//
//(非对称加密)的加密.Ting,Dicky和Viisen各自有一对公钥和密钥//
//.Ting和Dicky的通讯内容对于Viisen是不可见的,Dicky和Viisen //
//的通讯内容对于Ting是不可见的.                            //
/

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace EzoneSecuritySystem
{
     class EzoneSecurityDemo
     {
           //创建三个员工对象,分别是 Dicky, Ting, Viisen
           public static EzonePerson Dicky=new EzonePerson("Dicky");
           public static EzonePerson Ting=new EzonePerson("Ting");
           public static EzonePerson Viisen=new EzonePerson("Viisen");

           [STAThread]
           public static void Main(string[] args)
           {
                 Console.WriteLine("==================== 场景一: Dicky给自己发送邮件消息 ====================");
                 Scene_1();
                 Console.WriteLine("=========================================================================");
                 Console.Write("按任意键继续......");
                 Console.In.ReadLine();

                 Console.WriteLine("==================== 场景二: Dicky给Ting发送邮件消息 ====================");
                 Scene_2();
                 Console.WriteLine("=========================================================================");
                 Console.Write("按任意键继续......");
                 Console.In.ReadLine();

                 Console.WriteLine("================ 场景三: Dicky给Ting ,Viisen发送邮件消息 ================");
                 Scene_3();
                 Console.WriteLine("=========================================================================");
                 Console.Write("按任意键继续......");
                 Console.In.ReadLine();

           }

           /// <summary>
           /// Dicky给自己发送邮件消息
           /// </summary>
           public static void Scene_1()
           {
                 Console.WriteLine("Dicky用自己的公钥加密信息");
                 EzoneMessage DickyMessage=Dicky.EncryptMessage("我今天努力了吗?");
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Dicky用自己的私钥解密消息");
                 Dicky.DecryptMessage(DickyMessage);
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Viisen试图用自己的私钥解密消息");
                 Viisen.DecryptMessage(DickyMessage);
                 Console.WriteLine(System.Environment.NewLine);
           }
           /// <summary>
           /// Dicky给Ting发送邮件消息
           /// </summary>
           public static void Scene_2()
           {
                 Console.WriteLine("Dicky获得Ting的公钥");
                 Dicky.GetPublicKey(Ting);
                 Console.WriteLine(System.Environment.NewLine);
                 
                 Console.WriteLine("Dicky用Ting的公钥加密信息");
                 EzoneMessage TingMessage=Dicky.EncryptMessage("今晚一起吃饭好吗?");
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Ting用自己的私钥解密信息");
                 Ting.DecryptMessage(TingMessage);
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Viisen试图用自己的私钥解密消息");
                 Viisen.DecryptMessage(TingMessage);
                 Console.WriteLine(System.Environment.NewLine);
           }

           /// <summary>
           /// Dicky给Ting,Viisen发送邮件消息
           /// </summary>
           public static void Scene_3()
           {
                 Console.WriteLine("Dicky获得Ting的公钥");
                 Dicky.GetPublicKey(Ting);
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Dicky用Ting的公钥加密信息");
                 EzoneMessage TingMessage=Dicky.EncryptMessage("I love you!");
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Dicky获得Viisen的公钥");
                 Dicky.GetPublicKey(Viisen);
                 Console.WriteLine(System.Environment.NewLine);
                 
                 Console.WriteLine("Dicky用Viisen的公钥加密信息");
                 EzoneMessage ViisenMessage=Dicky.EncryptMessage("2003年度的财务总结报告放在你的办公桌上!");
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Ting用自己的私钥解密信息");
                 Ting.DecryptMessage(TingMessage);
                 Console.WriteLine(System.Environment.NewLine);

                 Console.WriteLine("Viisen用自己的私钥解密信息");
                 Viisen.DecryptMessage(ViisenMessage);
                 Console.WriteLine(System.Environment.NewLine);

           }
     }

 

     //邮件对象
     class EzoneMessage
     {
           public byte[] MessageBody;//邮件内容(内容通过RC2(对称加密)加密过的)
           public byte[] RC2Key;     //RC2的密钥(通过RSA(非对称加密)加密过的)
           public byte[] RC2IV;      //RC2的初始化向量
     }
     //员工对象
     class EzonePerson
     {
           //RSA(非对称加密对象)
           private RSACryptoServiceProvider EzoneRSA;
           //RC2(对称机密对象)
           private RC2CryptoServiceProvider EzoneRC2;
           //员工姓名
           private string Name;

           //Person构造方法
           public EzonePerson(string Name)
           {
                 //初始化成员对象
                 this.EzoneRSA=new RSACryptoServiceProvider();
                 this.EzoneRC2=new RC2CryptoServiceProvider();
                 this.Name=Name;
           }

           //发送公钥
           public RSAParameters SendPublicKey()
           {
                 //RSA的公钥和密钥对象
                 RSAParameters result=new RSAParameters();
                 //导出EzoneRSA的公钥(false 表示不导出私钥)
                 result=this.EzoneRSA.ExportParameters(false);
                 return result;
           }

           //获得公钥
           public void GetPublicKey(EzonePerson Obj)
           {
                 //导入 EzonePerson对象 的公钥
                 this.EzoneRSA.ImportParameters(Obj.SendPublicKey());
           }

           //加密邮件
           public EzoneMessage EncryptMessage(string text)
           {
                 EzoneMessage MessageObj=new EzoneMessage();
                 //将消息从字符串的形式转换成字节数组的形式
                 byte[] MessageBytes=System.Text.Encoding.UTF8.GetBytes(text);
                 //随机创建RC2的密钥
                 this.EzoneRC2.GenerateKey();
                 //随机创建RC2的初始化向量
                 this.EzoneRC2.GenerateIV();
                 //用RSA加密RC2的密钥,并赋值给消息对象的RC2的密钥匙(false 表示不用OAEP进行填充,只有WinXp以上版本的*作系统才支持)
                 MessageObj.RC2Key=this.EzoneRSA.Encrypt(this.EzoneRC2.Key,false);
                 //给消息对象的RC2的向量赋值
                 MessageObj.RC2IV=this.EzoneRC2.IV;

                 //创建一个加密对象
                 ICryptoTransform MyEncryptor=this.EzoneRC2.CreateEncryptor();
                 //创建一个内存流
                 MemoryStream EzoneMemoryStream=new MemoryStream();
                 //在内存流的基础上创建一个加密流对象
                 CryptoStream MyEncryptoStream=new CryptoStream(EzoneMemoryStream,MyEncryptor,CryptoStreamMode.Write);

                 //向加密流中写入邮件内容
                 MyEncryptoStream.Write(MessageBytes,0,MessageBytes.Length);
                 //刷新加密流的缓冲区
                 MyEncryptoStream.FlushFinalBlock();
                 //给消息对象的MessageBody(消息主体)赋值(经过RC2加密过的字节数组)
                 MessageObj.MessageBody=EzoneMemoryStream.ToArray();
                 EzoneMemoryStream.Close();
                 MyEncryptoStream.Close();
                 //返回EzoneMessage对象
                 return MessageObj;
           }
           //解密邮件
           public void DecryptMessage(EzoneMessage obj)
           {
                 this.EzoneRC2.IV=obj.RC2IV;
                 try
                 {
                       //用RSA解密RC2的密钥(false 表示不用OAEP进行填充,只有WinXp以上版本的*作系统才支持)
                       this.EzoneRC2.Key=this.EzoneRSA.Decrypt(obj.RC2Key,false);
                 }
                 catch(CryptographicException e)
                 {
                       Console.WriteLine("解密失败: "+e.Message);
                       return;
                 }
                 //创建一个解密对象
                 ICryptoTransform MyDecryptor=this.EzoneRC2.CreateDecryptor();
                 //创建一个内存流,用obj的邮件消息初始化内存流!
                 MemoryStream EzoneMemoryStream=new MemoryStream(obj.MessageBody);
                 //在内存流的基础上创建一个解密流对象
                 CryptoStream MyDecryptoStream=new CryptoStream(EzoneMemoryStream,MyDecryptor,CryptoStreamMode.Read);
                 //存储解密后的邮件内容
                 byte[] MessageText=new byte[obj.MessageBody.Length];
                 //从解密流解密数据,并把解密过的数据写入到MessageText字节数组中
                 MyDecryptoStream.Read(MessageText,0,MessageText.Length);

                 EzoneMemoryStream.Close();
                 MyDecryptoStream.Close();

                 Console.WriteLine("解密成功:"+System.Text.Encoding.UTF8.GetString(MessageText));
           }
     }
}


2222222222222
(亿众国际-008)[原创]利用DotNET密码系统保证数据安全

/
//Author: stardicky                                        //
//E-mail: stardicky@hotmail.com                            //
//QQNumber: 9531511                                        //
//CompanyName: Ezone International                         //
//Class: HBS-0308                                          //
//title: 利用DotNET密码系统保证数据安全                    //
/
//注:利用DotNET密码系统之一的DES对称加密算法保证数据安全   //
/

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace EzoneInternationalSecurityCryptography
{
     class EzoneSecurityCryptographyDemo
     {

           [STAThread]
           public static void Main(string[] args)
           {
                 //加密数据(从内存到文件)
                 EzoneEncryptorDemo();
                 //解密数据(从文件到内存)
                 EzoneDecryptorDemo();
           }
                 
           /// <summary>
           /// 加密
           /// </summary>
           public static void EzoneEncryptorDemo()
           {
                 //创建一个文件对象,文件的模式是创建新文件,文件的访问权限是可写!
                 FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Create,FileAccess.Write);
                 Console.WriteLine("请输入你想要进行加密的字符串:");
                 //输入你想要进行加密的字符串
                 string YourInput=Console.ReadLine();
                 //将字符串转换成字节
                 byte[] YourInputStorage=System.Text.Encoding.UTF8.GetBytes(YourInput);
                 //创建一个DES算法的加密类
                 DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider();
                 //从DES算法的加密类对象的CreateEncryptor方法,创建一个加密转换接口对象
                 //第一个参数的含义是:对称算法的机密密钥(长度为64位,也就是8个字节)
                 //                                可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateKey();
                 //第二个参数的含义是:对称算法的初始化向量(长度为64位,也就是8个字节)
                 //                    可以人工输入,也可以随机生成方法是:MyServiceProvider.GenerateIV();
                 ICryptoTransform MyTransform=MyServiceProvider.CreateEncryptor(new byte[]{100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});
                 //CryptoStream对象的作用是将数据流连接到加密转换的流
                 CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Write);
                 //将字节数组中的数据写入到加密流中
                 MyCryptoStream.Write(YourInputStorage,0,YourInputStorage.Length);
                 //关闭加密流对象
                 MyCryptoStream.Close();

           }

           /// <summary>
           /// 解密
           /// </summary>
           public static void EzoneDecryptorDemo()
           {
                 FileStream fs=new FileStream("EzoneDemo.txt",FileMode.Open,FileAccess.Read);
                 DESCryptoServiceProvider MyServiceProvider=new DESCryptoServiceProvider();
                 //从DES算法的加密类对象的CreateEncryptor方法,创建一个解密转换接口对象
                 //[对称算法的机密密钥]必须是加密时候的[对称算法的机密密钥]
                 //[对称算法的初始化向量]必须是加密时候的[对称算法的初始化向量]
                 //如果不一样,则会抛出一个异常。
                 ICryptoTransform MyTransform=MyServiceProvider.CreateDecryptor(new byte[]{100,110,120,130,100,110,120,130},new byte[]{100,110,120,130,100,110,120,130});
                 CryptoStream MyCryptoStream=new CryptoStream(fs,MyTransform,CryptoStreamMode.Read);
                 byte[] YourInputStorage=new byte[1000];
                 int len=MyCryptoStream.Read(YourInputStorage,0,YourInputStorage.Length);
                 Console.WriteLine("你刚才输入的字符串是:");
                 Console.WriteLine(System.Text.Encoding.UTF8.GetString(YourInputStorage,0,len));
           }

     }
}

相关文章
对该文的评论
CSDN 网友 ( 2004-06-11)
To rexsp:
http://www.csdn.net/develop/article/26/26096.shtm
nieyong ( 2004-05-17)
那到不一定。可以使用MemoryStream,把加密的信息读取到内存中去,然后把密文从内存中提取出来,解密出明文。比如下面一段代码:
//加密
public string DesEncrypt(string strText, string strEncrKey) 

byte[] byKey=null; 
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
try 

byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); 
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); 
MemoryStream ms = new MemoryStream(); 
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; 
cs.Write(inputByteArray, 0, inputByteArray.Length); 
cs.FlushFinalBlock(); 
return Convert.ToBase64String(ms.ToArray()); 

catch(System.Exception error) 

MessageBox.Show(error.Message); 
return "error:" +error.Message+"/r"; 


//解密函数 
public string DesDecrypt(string strText,string sDecrKey) 

byte[] byKey = null; 
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
byte[] inputByteArray = new Byte[strText.Length]; 
try 

byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); 
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
inputByteArray = Convert.FromBase64String(strText); 
MemoryStream ms = new MemoryStream(); 
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
cs.Write(inputByteArray, 0, inputByteArray.Length); 
cs.FlushFinalBlock(); 
System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
return encoding.GetString(ms.ToArray()); 

catch(System.Exception error) 

MessageBox.Show(error.Message); 
return "error:"+error.Message+"/r"; 


//或者下面的另一个程序代码:
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

public class EncryptStringDES {

    public static void Main(String[] args) {
        if (args.Length < 1) {
            Console.WriteLine("Usage: des_demo  ", args[0]);
            return;
        }

        // 使用UTF8函数加密输入参数
        UTF8Encoding utf8Encoding = new UTF8Encoding();
        byte[] inputByteArray = utf8Encoding.GetBytes(args[0].ToCharArray());

        // 方式一:调用默认的DES实现方法DES_CSP.
        DES des = DES.Create();
        // 方式二:直接使用DES_CSP()实现DES的实体
        //DES_CSP DES = new DES_CSP();

        // 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV)
        Byte[] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
        Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
        des.Key = key;
        des.IV = IV;

        // 建立加密流
          SymmetricStreamEncryptor sse = des.CreateEncryptor();

        // 使用CryptoMemoryStream方法获取加密过程的输出
        CryptoMemoryStream cms = new CryptoMemoryStream();

        // 将SymmetricStreamEncryptor流中的加密数据输出到CryptoMemoryStream中
        sse.SetSink(cms);

        // 加密完毕,将结果输出到控制台
        sse.Write(inputByteArray);
        sse.CloseStream();

        // 获取加密数据
        byte[] encryptedData = cms.Data;

        // 输出加密后结果
        Console.WriteLine("加密结果:");
        for (int i = 0; i < encryptedData.Length; i++) {
            Console.Write("{0:X2} ",encryptedData[i]);
        }
        Console.WriteLine();

        //上面演示了如何进行加密,下面演示如何进行解密
        SymmetricStreamDecryptor ssd = des.CreateDecryptor();
        cms = new CryptoMemoryStream();
        ssd.SetSink(cms);
        ssd.Write(encryptedData);
        ssd.CloseStream();

        byte[] decryptedData = cms.Data;
        char[] decryptedCharArray = utf8Encoding.GetChars(decryptedData);
        Console.WriteLine("解密后数据:");
        Console.Write(decryptedCharArray);
        Console.WriteLine();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值