当前位置:编程学习 > VB >>

vb.net文本加密与解密代码

使用VB.NET进行文本的加密保存,VB.NET中自带的源代码不会用,看不懂,有人解释下吗?

Imports System.IO
Imports System.Security.Cryptography

 

        TextBox1.Font = New System.Drawing.Font("Symbol", CType(9, Byte))

        Dim fStream As FileStream = File.Open("encrypted.txt", FileMode.OpenOrCreate)
        Dim RijndaelAlg As Rijndael = Rijndael.Create
        Dim cStream As New CryptoStream(fStream, _
                                       RijndaelAlg.CreateEncryptor(RijndaelAlg.Key, RijndaelAlg.IV), _
                                       CryptoStreamMode.Write)
        Dim sWriter As New StreamWriter(cStream)

        sWriter.WriteLine("Text to encrypt")
        sWriter.Close()
        cStream.Close()
        fStream.Close()
还有解密的:

        TextBox1.Font = New System.Drawing.Font("宋体", CType(9, Byte))
        Dim fStream As FileStream = File.Open("encrypted.txt", FileMode.OpenOrCreate)
        Dim RijndaelAlg As Rijndael = Rijndael.Create
        Dim cStream As New CryptoStream(fStream, _
                                        RijndaelAlg.CreateDecryptor(RijndaelAlg.Key, RijndaelAlg.IV), _
                                        CryptoStreamMode.Read)

        Dim sReader As New StreamReader(cStream)
        Dim plainText As String = sReader.ReadLine()

        sReader.Close()
        cStream.Close()
        fStream.Close()

答案:.NET将原来独立的API和SDK合并到一个框架中,这对于程序开发人员非常有利。它将CryptoAPI改编进.NET的System.Security.Cryptography名字空间,使密码服务摆脱了SDK平台的神秘性,变成了简单的.NET名字空间的使用。由于随着整个框架组件一起共享,密码服务更容易实现了,现在仅仅需要学习System.Security.Cryptography名字空间的功能和用于解决特定方案的类。 

  加密和解密的算法

  System.Security.Cryptography名字空间包含了实现安全方案的类,例如加密和解密数据、管理密钥、验证数据的完整性并确保数据没有被篡改等等。本文重点讨论加密和解密。

  加密和解密的算法分为对称(symmetric)算法和不对称(asymmetric)算法。对称算法在加密和解密数据时使用相同的密钥和初始化矢量,典型的有DES、 TripleDES和Rijndael算法,它适用于不需要传递密钥的情况,主要用于本地文档或数据的加密。不对称算法有两个不同的密钥,分别是公共密钥和私有密钥,公共密钥在网络中传递,用于加密数据,而私有密钥用于解密数据。不对称算法主要有RSA、DSA等,主要用于网络数据的加密。

  加密和解密本地文档

  下面的例子是加密和解密本地文本,使用的是Rijndael对称算法。

  对称算法在数据流通过时对它进行加密。因此首先需要建立一个正常的流(例如I/O流)。文章使用FileStream类将文本文件读入字节数组,也使用该类作为输出机制。

  接下来定义相应的对象变量。在定义SymmetricAlgorithm抽象类的对象变量时我们可以指定任何一种对称加密算法提供程序。代码使用的是Rijndael算法,但是很容易改为DES或者TripleDES算法。.NET使用强大的随机密钥设置了提供程序的实例,选择自己的密钥是比较危险的,接受计算机产生的密钥是一个更好的选择,文中的代码使用的是计算机产生的密钥。

  下一步,算法实例提供了一个对象来执行实际数据传输。每种算法都有CreateEncryptor和CreateDecryptor两个方法,它们返回实现ICryptoTransform接口的对象。

  最后,现在使用BinaryReader的ReadBytes方法读取源文件,它会返回一个字节数组。BinaryReader读取源文件的输入流,在作为CryptoStream.Write方法的参数时调用ReadBytes方法。指定的CryptoStream实例被告知它应该操作的下层流,该对象将执行数据传递,无论流的目的是读或者写。

  下面是加密和解密一个文本文件的源程序片断:

  namespace com.billdawson.crypto

  {

  class TextFileCrypt

  {

  public static void Main(string[] args)

  {

  string file = args[0];

  string tempfile = Path.GetTempFileName();

  //打开指定的文件

  FileStream fsIn = File.Open(file,FileMode.Open,

  FileAccess.Read);

  FileStream fsOut = File.Open(tempfile, FileMode.Open,

  FileAccess.Write);

  //定义对称算法对象实例和接口

  SymmetricAlgorithm symm = new RijndaelManaged();

  ICryptoTransform transform = symm.CreateEncryptor();

  CryptoStream cstream = new CryptoStream(fsOut,transform,

  ryptoStreamMode.Write);

  BinaryReader br = new BinaryReader(fsIn);

  // 读取源文件到cryptostream

  cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);

  cstream.FlushFinalBlock();

  cstream.Close();

  fsIn.Close();

  fsOut.Close();

  Console.WriteLine("created encrypted file {0}", tempfile);

  Console.WriteLine("will now decrypt and show contents");

  // 反向操作--解密刚才加密的临时文件

  fsIn = File.Open(tempfile,FileMode.Open,FileAccess.Read);

  transform = symm.CreateDecryptor();

  cstream = new CryptoStream(fsIn,transform,

  CryptoStreamMode.Read);

  StreamReader sr = new StreamReader(cstream);

  Console.WriteLine("decrypted file text: " sr.ReadToEnd());

  fsIn.Close();

  }

  }

  }

上一个:vb 根据文本框的数字读取文本
下一个:如何用VB制作个多功能时钟

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,