分段加密处理
'获取key
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\test\PublicKey.xml")
Dim publickey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(publickey)
'读取和写入文件文件
Dim read As new IO.FileStream("d:\test\test.txt", IO.FileMode.Open)
Dim maxLength As Integer = oRSA.KeySize/8-11
Dim data(maxlength-1) As Byte
Dim write As new IO.FileStream("d:\test\test加密.txt", IO.FileMode.Create)
Dim dataSize As Integer = read.Read(data, 0, data.Length)
Do While dataSize = maxLength
Dim AOutput As Byte() = oRSA.Encrypt(data ,False)
write.Write(AOutput, 0, AOutput.Length)
write.Flush()
dataSize = read.Read(data, 0, data.Length)
If dataSize < maxLength
'最后一次的情况
Dim lsData(dataSize-1) As Byte
Array.Copy(data, 0, lsData, 0, dataSize)
Dim lsAOutput As Byte() = oRSA.Encrypt(lsData,False)
write.Write(lsAOutput, 0, lsAOutput.Length)
write.Flush()
End If
Loop
read.Close ()
write.Close()
分段解密处理
'获取key
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\test\PrivateKey.xml")
Dim PrivateKey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(PrivateKey)
'读取和写入文件文件
Dim read As new IO.FileStream("d:\test\test加密.txt", IO.FileMode.Open)
Dim data(oRSA.KeySize/8-1) As Byte
Dim write As new IO.FileStream("d:\test\test解密.txt", IO.FileMode.Create)
Do While read.Read(data, 0, data.Length) > 0
Dim AInput As Byte() = oRSA.Decrypt(data ,False)
write.Write(AInput, 0, AInput.Length)
write.Flush()
Loop
read.Close ()
write.Close()