十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
使用加密方式存储即可实现别人无法查看内容,加密的方式有很多,适用你这里使用的是可逆的算法,推荐你使用DES加密
创新互联专注于延安企业网站建设,响应式网站,商城系统网站开发。延安网站建设公司,为延安等地区提供建站服务。全流程按需策划设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Namespace ZU14
NotInheritable Public Class DES
Private iv As String = "1234的yzo"
Private key As String = "123在yzo"
'/ summary
'/ DES加密偏移量,必须是=8位长的字符串
'/ /summary
Public Property IV() As String
Get
Return iv
End Get
Set
iv = value
End Set
End Property
'/ summary
'/ DES加密的私钥,必须是8位长的字符串
'/ /summary
Public Property Key() As String
Get
Return key
End Get
Set
key = value
End Set
End Property
'/ summary
'/ 对字符串进行DES加密
'/ /summary
'/ param name="sourceString"待加密的字符串/param
'/ returns加密后的BASE64编码的字符串/returns
Public Function Encrypt(sourceString As String) As String
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim ms As New MemoryStream()
Try
Dim inData As Byte() = Encoding.Default.GetBytes(sourceString)
Try
Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(inData, 0, inData.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Return Convert.ToBase64String(ms.ToArray())
Catch
End Try
Finally
ms.Dispose()
End Try
End Function 'Encrypt
'/ summary
'/ 对DES加密后的字符串进行解密
'/ /summary
'/ param name="encryptedString"待解密的字符串/param
'/ returns解密后的字符串/returns
Public Function Decrypt(encryptedString As String) As String
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim ms As New MemoryStream()
Try
Dim inData As Byte() = Convert.FromBase64String(encryptedString)
Try
Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(inData, 0, inData.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Return Encoding.Default.GetString(ms.ToArray())
Catch
End Try
Finally
ms.Dispose()
End Try
End Function 'Decrypt
'/ summary
'/ 对文件内容进行DES加密
'/ /summary
'/ param name="sourceFile"待加密的文件绝对路径/param
'/ param name="destFile"加密后的文件保存的绝对路径/param
Overloads Public Sub EncryptFile(sourceFile As String, destFile As String)
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
End If
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
Try
Try
Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(btFile, 0, btFile.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Catch
Finally
fs.Close()
End Try
Finally
fs.Dispose()
End Try
End Sub 'EncryptFile
'/ summary
'/ 对文件内容进行DES加密,加密后覆盖掉原来的文件
'/ /summary
'/ param name="sourceFile"待加密的文件的绝对路径/param
Overloads Public Sub EncryptFile(sourceFile As String)
EncryptFile(sourceFile, sourceFile)
End Sub 'EncryptFile
'/ summary
'/ 对文件内容进行DES解密
'/ /summary
'/ param name="sourceFile"待解密的文件绝对路径/param
'/ param name="destFile"解密后的文件保存的绝对路径/param
Overloads Public Sub DecryptFile(sourceFile As String, destFile As String)
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
End If
Dim btKey As Byte() = Encoding.Default.GetBytes(key)
Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
Dim des As New DESCryptoServiceProvider()
Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
Try
Try
Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
Try
cs.Write(btFile, 0, btFile.Length)
cs.FlushFinalBlock()
Finally
cs.Dispose()
End Try
Catch
Finally
fs.Close()
End Try
Finally
fs.Dispose()
End Try
End Sub 'DecryptFile
'/ summary
'/ 对文件内容进行DES解密,加密后覆盖掉原来的文件
'/ /summary
'/ param name="sourceFile"待解密的文件的绝对路径/param
Overloads Public Sub DecryptFile(sourceFile As String)
DecryptFile(sourceFile, sourceFile)
End Sub 'DecryptFile
End Class 'DES
End Namespace 'ZU14
对文本文件加密
Dim des As New ZU14.DES()
des.IV = "abcd哈哈笑"
des.Key = "必须八位"
'加密
des.EncryptFile("d:\a.txt", "d:\b.txt")
'解密
des.DecryptFile("d:\b.txt")
"采用DES算法"这个说法不明确,首先是使用多少位的DES进行加密,通常是128位或192位,其次是,要先把主密钥转化成散列,才能供DES进行加密,转化的方法是什么没有明确,通常是md5,所以有的银行卡说是128位md5 3DS就是指用md5转换主密钥散列,用DES进行加密,但是DES本身是64位(包含校验码),2DES是128位,3DES是192位,但是没有2DES的叫法,所以128位、192位统称3DES
要完整的md5+3DS实例,需要100分以上,要不到我的空间中查找相关的文章
下面是完整的类,可以设置任意密码
'DES及md5加密解密----添加引用中添加对system.web的引用。
Imports System.Security.Cryptography
Imports System
Imports System.Text
Imports System.Web
''' summary
''' DES加密类
''' /summary
''' remarks/remarks
Public Class DESEncrypt
Public Sub DESEncrypt()
End Sub
Public Shared Function Encrypt(ByVal Text As String) As String
Return Encrypt(Text, "12345678")
End Function
Public Shared Function Encrypt(ByVal Text As String, ByVal sKey As String) As String
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray As Byte()
inputByteArray = Encoding.Default.GetBytes(Text)
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim ms As New System.IO.MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim ret As New StringBuilder()
Dim b As Byte
For Each b In ms.ToArray()
ret.AppendFormat("{0:X2}", b)
Next
Return ret.ToString()
End Function
Public Shared Function Decrypt(ByVal Text As String) As String
Return Decrypt(Text, "12345678")
End Function
Public Shared Function Decrypt(ByVal Text As String, ByVal sKey As String) As String
Dim des As New DESCryptoServiceProvider()
Dim len As Integer
len = Text.Length / 2
Dim inputByteArray(len - 1) As Byte
Dim x, i As Integer
For x = 0 To len - 1
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16)
inputByteArray(x) = CType(i, Byte)
Next
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8))
Dim ms As New System.IO.MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Encoding.Default.GetString(ms.ToArray())
End Function
End Class
'以下是调用方法
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '加密
Dim str_Encrypt As String = DESEncrypt.Encrypt("你要加密的文本,可以是任意长度", "密码,可以很长,如果省略这个参数就是默认的12345678")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '解密
Dim str_Decrypt As String = DESEncrypt.Decrypt("你要解密的文本, 可以是任意长度", "加密时用到的密码,如果省略这个参数就是默认的12345678")
End Sub