Excel的加密和解密
生活随笔
收集整理的這篇文章主要介紹了
Excel的加密和解密
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Excel的加密和解密
? ? ? ? ? 在做各種項目的時候,如果涉及到公司內部的數據,或者其他的保密的數據,就需要用到Excel的加密和解密。
? ? ? ? ??其實,就是用到了兩個函數,加密函數和解密函數。
? ? ? ? ??加密函數:
<span style="font-size:24px;">PublicFunction EncryptDes(ByVal SourceStr As String, ByVal myKey As String, ByValmyIV As String) As String '使用的DES對稱加密Dim des As NewSystem.Security.Cryptography.DESCryptoServiceProvider 'DES算法'Dim DES As NewSystem.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法Dim inputByteArray As Byte()inputByteArray =System.Text.Encoding.Default.GetBytes(SourceStr)des.Key = System.Text.Encoding.UTF8.GetBytes(myKey)'myKey DES用8個字符,TripleDES要24個字符des.IV =System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8個字符,TripleDES要24個字符Dim ms As New System.IO.MemoryStreamDim cs As NewSystem.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(),System.Security.Cryptography.CryptoStreamMode.Write)Dim sw As NewSystem.IO.StreamWriter(cs)sw.Write(SourceStr)sw.Flush()cs.FlushFinalBlock()ms.Flush()EncryptDes =Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)End Function</span>? ? ? ? ??解密函數:
<span style="font-size:24px;">PublicFunction DecryptDes(ByVal SourceStr As String, ByVal myKey As String, ByValmyIV As String) As String '使用標準DES對稱解密Dim des As NewSystem.Security.Cryptography.DESCryptoServiceProvider 'DES算法'Dim DES As NewSystem.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法des.Key =System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8個字符,TripleDES要24個字符des.IV =System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8個字符,TripleDES要24個字符Dim buffer As Byte() =Convert.FromBase64String(SourceStr)Dim ms As New System.IO.MemoryStream(buffer)Dim cs As NewSystem.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(),System.Security.Cryptography.CryptoStreamMode.Read)Dim sr As NewSystem.IO.StreamReader(cs)DecryptDes = sr.ReadToEnd()End Function</span>? ? ? ? ??具體如何使用呢,就是非常的簡單,在程序中需要導出Excel的時候,添加兩句代碼進行加密,在導入Excel的時候,同樣的添加兩句代碼進行解密即可。
總結
以上是生活随笔為你收集整理的Excel的加密和解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML中的列表和表格
- 下一篇: 插入排序法