.NET編程 — TripleDES加解密範例

來源:互聯網
上載者:User

這個範例是以VB.NET進行TripleDES加解密範例

 Imports System  
 Imports System.IO  
 Imports System.Security.Cryptography  
  
 Public Class Form_Main  
  
     Dim byteKey As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}  
     Dim byteIV As Byte() = {8, 7, 6, 5, 4, 3, 2, 1}  
     Dim byteEncrypt As Byte() = Nothing 
     Dim strPlainText As String = String.Empty  
  
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
  
  
     End Sub 
  
     Private Shared Function TripleDESEncrypt(ByVal strInput As String, ByVal byteKey As Byte(), ByVal byteIV As Byte()) As Byte()  
         Dim tdes As TripleDES = Nothing 
         Dim ict As ICryptoTransform = Nothing 
         Dim ms As MemoryStream = Nothing 
         Dim cs As CryptoStream = Nothing 
         Dim sw As StreamWriter = Nothing 
         Dim byteResult As Byte() = Nothing 
  
         Try 
             tdes = TripleDES.Create()  
             tdes.Key = byteKey  
             tdes.IV = byteIV  
  
             ict = tdes.CreateEncryptor()  
  
             ms = New MemoryStream()  
  
             cs = New CryptoStream(ms, ict, CryptoStreamMode.Write)  
  
             sw = New StreamWriter(cs)  
  
             sw.Write(strInput)  
             sw.Close()  
  
             cs.Close()  
  
             byteResult = ms.ToArray()  
             ms.Close()  
  
             Return byteResult  
         Catch e As Exception  
             Throw e  
         Finally 
             If sw IsNot Nothing Then 
                 sw.Close()  
             End If 
             If cs IsNot Nothing Then 
                 cs.Close()  
             End If 
             If ms IsNot Nothing Then 
                 ms.Close()  
             End If 
         End Try 
     End Function 
  
     Private Shared Function TripleDESDecrypt(ByVal byteInput As Byte(), ByVal byteKey As Byte(), ByVal byteIV As Byte()) As String 
         Dim tdes As TripleDES = Nothing 
         Dim ict As ICryptoTransform = Nothing 
         Dim ms As MemoryStream = Nothing 
         Dim cs As CryptoStream = Nothing 
         Dim sr As StreamReader = Nothing 
         Dim strResult As String = [String].Empty  
  
         Try 
             tdes = TripleDES.Create()  
             tdes.Key = byteKey  
             tdes.IV = byteIV  
  
             ict = tdes.CreateDecryptor()  
  
             ms = New MemoryStream(byteInput)  
  
             cs = New CryptoStream(ms, ict, CryptoStreamMode.Read)  
  
             sr = New StreamReader(cs)  
  
             strResult = sr.ReadToEnd()  
             sr.Close()  
  
             cs.Close()  
  
             ms.Close()  
  
             Return strResult  
         Catch e As Exception  
             Throw e  
         Finally 
             If sr IsNot Nothing Then 
                 sr.Close()  
             End If 
             If cs IsNot Nothing Then 
                 cs.Close()  
             End If 
             If ms IsNot Nothing Then 
                 ms.Close()  
             End If 
         End Try 
     End Function 
  
     Private Sub Button_Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start.Click  
         strPlainText = String.Empty  
  
         '呼叫加密方法   
         byteEncrypt = TripleDESEncrypt(TextBox1.Text, byteKey, byteIV)  
  
         '加密結果   
         TextBox2.Text = Convert.ToBase64String(byteEncrypt)  
  
         '解密結果  
         TextBox3.Text = TripleDESDecrypt(byteEncrypt, byteKey, byteIV)  
  
  
     End Sub 
 End Class 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.