首页|操作系统|软件开发|网页制作|媒体动画|数据库|ASP教程|ASP.NET教程|PHP教程|JSP教程|XML教程|建站资料|软件学院|行业资讯|平面设计|网络安全|晒IT论坛|IT人才
  位置: 晒IT >> ASP.NET教程 >> Asp.Net提高教程 >> 正文
 
 
加密与解密
加密与解密
 
 
Imports System.IO
Imports System.Security.Cryptography

'数据加/解密 类
Public Class CryData
'加密密钥,初始化向量
Public ReadOnly cryKey As Byte() = {9, 4, 2, 8, 5, 1, 4, 9, 7, 6, 9, 5, 1, 13, 7, 5, 14, 9, 10, 15, 0, 1, 14, 5, 9, 4, 3, 8, 2, 10}
Public ReadOnly cryIV As Byte() = {7, 1, 8, 8, 2, 8, 7, 1, 4, 5, 6, 3, 5, 6, 7}


' 文件加密
Public Sub EncryptData(ByVal inName As String, ByVal outName As String, _
Optional ByVal rijnKey() As Byte = Nothing, _
Optional ByVal rijnIV() As Byte = Nothing)

If rijnKey Is Nothing Then
rijnKey = cryKey


End If
If rijnIV Is Nothing Then
rijnIV = cryIV

End If
ReDim Preserve rijnKey(31)
ReDim Preserve rijnIV(15)

'Create the file streams to handle the input and output files.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
fout.SetLength(0)

'Create variables to help with read and write.
Dim bin(1024) As Byte 'This is intermediate storage for the encryption.
Dim rdlen As Long = 0 'This is the total number of bytes written.
Dim totlen As Long = fin.Length 'Total length of the input file.
Dim len As Integer 'This is the number of bytes to be written at a time.
'Creates the default implementation, which is RijndaelManaged.
Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
Dim encStream As New CryptoStream(fout, _
rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write)

' Console.WriteLine("Encrypting...")

'Read from the input file, then encrypt and write to the output file.
While rdlen < totlen
len = fin.Read(bin, 0, 1024)
encStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len)
' Console.WriteLine("{0} bytes processed", rdlen)
End While


'fout.Seek(0, SeekOrigin.Begin)
'Dim returnValue As String
'returnValue = New StreamReader(fout).ReadToEnd()

encStream.Close()
fout.Close()
fin.Close()

End Sub

'文件解密

Public Sub DecryptData(ByVal inName As String, ByVal outName As String, _
Optional ByVal rijnKey() As Byte = Nothing, _
Optional ByVal rijnIV() As Byte = Nothing)

If rijnKey Is Nothing Then
rijnKey = cryKey

End If

If rijnIV Is Nothing Then
rijnIV = cryIV

End If

ReDim Preserve rijnKey(31)
ReDim Preserve rijnIV(15)

'Create the file streams to handle the input and output files.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.ReadWrite)
fout.SetLength(0)

'Create variables to help with read and write.
Dim bin(1024) As Byte 'This is intermediate storage for the encryption.
Dim rdlen As Long = 0 'This is the total number of bytes written.
Dim totlen As Long = fin.Length 'Total length of the input file.
Dim len As Integer 'This is the number of bytes to be written at a time.
'Creates the default implementation, which is RijndaelManaged.
Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
Dim encStream As New CryptoStream(fout, _
rijn.CreateDecryptor(rijnKey, rijnIV), CryptoStreamMode.Write)

' Console.WriteLine("Encrypting...")

'Read from the input file, then encrypt and write to the output file.
While rdlen < totlen
len = fin.Read(bin, 0, 1024)
encStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len)
' Console.WriteLine("{0} bytes processed", rdlen)

End While

encStream.Close()
fout.Close()
fin.Close()

End Sub
'文件解密

Public Function DecryptData(ByVal inName As String, _
Optional ByVal rijnKey() As Byte = Nothing, _
Optional ByVal rijnIV() As Byte = Nothing) As String

If rijnKey Is Nothing Then
rijnKey = cryKey

End If

If rijnIV Is Nothing Then
rijnIV = cryIV

End If

ReDim Preserve rijnKey(31)
ReDim Preserve rijnIV(15)

'Create the file streams to handle the input and output files.
[1] [2] [3]  下一页
  • 上一篇: 使用的DES对称加密
  • 下一篇: WEB系统中加密\解密URL传输参数.
  •  告诉好友  打印此文 关闭窗口 返回顶部
     
    热点文章
     
     
    推荐文章
     
     
    相关文章

    | 设为首页 | 加入收藏 | 联系我们 | 友情链接 | 诚聘英才 |
    Copyright© 2008 ShaiIT.Com .All Rights Reserved
    下载alexa工具,提升您的网站排名