频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

加密与解密

来源: 作者: 出处:巧巧读书 2006-09-20 进入讨论组

  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.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
' Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
' FileAccess.ReadWrite)
'存储流,
Dim outStream As New MemoryStream() '(arrInByte, True) '(arrInByte, True)


'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.
While rdlen < totlen
len = fin.Read(bin, 0, 1024)
outStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len)
' Console.WriteLine("{0} bytes processed", rdlen)

End While

outStream.Seek(0, SeekOrigin.Begin)

Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
Dim encStream As New CryptoStream(outStream, _
rijn.CreateDecryptor(rijnKey, rijnIV), CryptoStreamMode.Read)

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

'Read from the input file, then encrypt and write to the output file.


Dim returnValue As String
returnValue = New StreamReader(encStream, New System.Text.UnicodeEncoding()).ReadToEnd()


encStream.Close()
outStream.Close()
fin.Close()
Return returnValue

End Function





'加密
'in: inText 待加密原始文本
'in: rijnKey 32位密钥
'in: rijnIV 16位初始化向量

'out: return 加密后的文本(为空则加密失败)

Public Function Crypt(ByVal inText As String, _
Optional ByVal rijnKey() As Byte = Nothing, _
Optional ByVal rijnIV() As Byte = Nothing) As String



'Create the file streams to handle the input and output files.
' Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
D来自:http://www.qqread.com/dotnet/g233373.html 更多文章 更多内容请看加密与解密技术常用软件加密宝典数据加密技术专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章