分享
 
 
 

加密与解密

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有