首页 电脑学院 黑客教程 网站技术 网页特效 免费论文 公文写作 演讲发言 实用文档 职场指南 时尚生活 情感男女 其他资讯

您的位置:首页-> 网站技术-> .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.
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)
Dim arrInByte As Byte() = (New System.Text.UnicodeEncoding()).GetBytes(inText)

Dim len As Long = arrInByte.Length

If (len Mod 32) > 0 Then
Dim oldlen As Long = len

len = oldlen + 32 - oldlen Mod 32

ReDim Preserve arrInByte(len - 1)

End If

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)


Dim outStream As New MemoryStream() '(arrInByte, True) '(arrInByte, True)

Dim rij As SymmetricAlgorithm = SymmetricAlgorithm.Create()

Dim encStream As New CryptoStream(outStream, _
rij.CreateEncryptor(rijnKey, rijnIV), _
CryptoStreamMode.Write)

encStream.Write(arrInByte, 0, len)
outStream.Seek(0, SeekOrigin.Begin)

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

encStream.Close()
outStream.Close()

Return returnValue

End Function

'--------------------------------------------------------------------------------------------------------
'解密

'in: inText 待解密密文
'in: rijnKey 32位解密密钥(须跟加密时的相同)
'in: rijnIV 16位解密初始化向量(须跟加密时的相同)

'out: return 解密后的文本(为空则解密失败)
'解密 过程:把密文换成字节写到内存流,再跟据rijnKey和rijnIV创建解密流
'从解密流中读取所有的数据
'--------------------------------------------------------------------------------------------------------

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



'密文转换成字节

Dim arrInByte As Byte() = (New System.Text.UnicodeEncoding()).GetBytes(inText)

'存储流,
Dim outStream As New MemoryStream() '(arrInByte, True) '(arrInByte, True)

Dim len As Long = arrInByte.Length
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)


Dim rij As SymmetricAlgorithm = SymmetricAlgorithm.Create()

outStream.Write(arrInByte, 0, len)

outStream.Seek(0, SeekOrigin.Begin)

Dim encStream As New CryptoStream(outStream, _
rij.CreateDecryptor(rijnKey, rijnIV), _
CryptoStreamMode.Read)

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


encStream.Close()
outStream.Close()

Return returnValue

End Function
End Class





' 加密
'http://community.csdn.net/Expert/topic/3199/3199494.xml?temp=.982464
Public Shared Function EncryptText(ByVal strText As String) As String
Return Encrypt(strText, "&%#@?./)")
End Function

'解密
Public Shared Function DecryptText(ByVal strText As String) As String
Return Decrypt(strText, "&%#@?./)")
End Function

'加密函数
Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
'byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8))
Dim des As New System.Security.Cryptography.DESCryptoServiceProvider
Dim inputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function

'解密函数
Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
'byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8))

Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function






在百度中查找更多加密与解密的内容
职场宝典   职场故事   职场跳槽   职场文化   职场理财   职场充电   情感天地   职场女性   职场礼仪   职场新人
报告总结   述职报告 工作总结 调查报告 工作汇报 计划方案 个人总结 社会实践 规章制度 调研报告 
  实习报告 考察报告 辞职报告 
演讲发言   竞职演说   就职演说   精彩演说   爱国演讲   英语演讲   十七大演讲   安全生产演讲稿   
节日祝福   重阳节 国庆节 教师节 中秋节 情人节 七夕节 劳动节 妇女节 清明节 愚人节 春节 元旦 圣诞节  儿童节  端午节 母亲节 新婚祝福 生日祝福 
讲话致辞   开业开幕   会议主持   庆典致辞   会议发言   党风廉政   党政报告   贺电慰问   婚丧嫁娶   思想宣传
法律常识   基本常识   法律文书   权益常识   劳动保障   婚姻继承   民事诉讼   刑事诉讼   
党建材料   入党申请   思想学习   党性分析   思想汇报   转正申请   民主生活   党委党建   入团申请   申报材料
求职简历   个人简历   求职自荐   求职谋略   面试技巧   求职英语   自我鉴定   英文简历   简历封面
心得体会   心得体会   经验交流   读后感   
时政热点   和谐社会   先进性教育   新农村建设   十七大   八荣八耻   科学发展观   劳动合同法   
人际沟通   社交技巧   社交礼仪   口才技巧   谈话技巧   演讲技巧   
营销技巧   电话销售   网络销售   推销技巧   促销技巧   销售口才   营销手段   销售技巧   谈判技巧   

“ 加密与解密”来源于网络,版权归作者所有!勿用于商业用途。

.NET技术

ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库开发
其它类

本类阅读TOP10

·数据库的连接串(中文)
·DES(Data Encryption Standard)加密解密整理
·利用Jmail发送和接收邮件(C#)
·.NET Framework 中多语言支持的实现
·动态菜单
·.NET 框架类库(上)
·C#中的事件
·微软.NET战略和ASP.NET简介(1)
·加密与解密
·ASP.NET中的代码分离

广告


关于本站|服务条款|广告服务|客服中心|发布文章|网站留言