I have a create a new project (Control Library). And I am using this code. However it returns the following error:
Type 'PasswordDeriverBytes' is not defined regarding the SetKey function. (highlighted in bold) What am I missing out. I originally took this code from a windows form (for a desktop not PPC).
I really need to get this fixed.
Thanks in advance
Imports System.Security.Cryptography
Imports System.IO
Public Class PDACrypt
Public Sub New()
MyBase.New()
InitializeComponent()
clientRC2CryptoServiceProvider = New RC2CryptoServiceProvider()
End Sub
Public Shared clientRC2CryptoServiceProvider As RC2CryptoServiceProvider
Private Function SetEncKey(ByVal pwd As String) As Byte()
Dim drive As Long
Dim rnd As Random
rnd = New Random
Dim byts(7) As Byte '8 bytes
rnd.NextBytes(byts)
SetKey(pwd, byts)
Return byts
End Function
Private Sub SetDecKey(ByVal pwd As String, ByVal salt As Byte())
Dim drive As Long
SetKey(pwd, salt)
End Sub
Private Sub SetKey(ByVal pwd As String, ByVal salt As Byte())
Dim deriver As PasswordDeriveBytes
deriver = New PasswordDeriveBytes(pwd, salt)
clientRC2CryptoServiceProvider.Key = deriver.GetBytes(clientRC2CryptoServiceProvider.LegalKeySizes(0).MaxSize \ 8)
clientRC2CryptoServiceProvider.IV = deriver.GetBytes(clientRC2CryptoServiceProvider.BlockSize \ 8)
End Sub
Public Function EncryptFileRC2(ByVal pwd As String, ByVal fin As String, ByVal feout As String)
Dim fileStream As FileStream
Dim lngFileLength As Double = 0
fileStream = File.OpenWrite(feout)
Dim writeStream As BinaryWriter
writeStream = New BinaryWriter(fileStream)
Dim myByte As Byte
Dim salt() As Byte
salt = SetEncKey(pwd)
Try
Dim i As Integer
For i = 0 To salt.Length - 1
writeStream.Write(salt(i))
Next
writeStream.Flush()
Catch
End Try
Dim encryptor As ICryptoTransform
encryptor = clientRC2CryptoServiceProvider.CreateEncryptor()
Dim encStream As CryptoStream
encStream = New CryptoStream(fileStream, encryptor, CryptoStreamMode.Write)
Dim fI As New FileInfo(fin)
Dim lngLoopCounter As Double = 0
lngFileLength = fI.Length
Dim readStream As BinaryReader
readStream = New BinaryReader(File.OpenRead(fin))
Try
Do
'pbStatus.Value = (100 / lngFileLength) * lngLoopCounter
lngLoopCounter = lngLoopCounter + 1
myByte = readStream.ReadByte()
encStream.WriteByte(myByte)
Loop
Catch
encStream.FlushFinalBlock()
encStream.Flush()
End Try
encStream.Close()
fileStream.Close()
readStream.Close()
End Function
Public Function DecryptFileRC2(ByVal pwd As String, ByVal fin As String, ByVal fdout As String)
Dim fileStream As FileStream
fileStream = File.OpenRead(fin)
Dim salt(7) As Byte
Try
Dim i As Integer
For i = 0 To 7
salt(i) = fileStream.ReadByte()
Next
SetDecKey(pwd, salt)
Catch
fileStream.Close()
' Return
End Try
Dim encryptor As ICryptoTransform
encryptor = clientRC2CryptoServiceProvider.CreateDecryptor()
Dim decStream As CryptoStream
decStream = New CryptoStream(fileStream, encryptor, CryptoStreamMode.Read)
Dim readStream As BinaryReader
readStream = New BinaryReader(decStream)
Dim lngFileLength As Double = 0
Dim fI As New FileInfo(fin)
Dim lngLoopCounter As Double = 0
lngFileLength = fI.Length
Dim writeStream As BinaryWriter
writeStream = New BinaryWriter(File.OpenWrite(fdout))
Dim myByte As Byte
Try
Do
'pbStatus.Value = (100 / lngFileLength) * lngLoopCounter
lngLoopCounter = lngLoopCounter + 1
myByte = readStream.ReadByte()
writeStream.Write(myByte)
Loop
Catch
writeStream.Flush()
End Try
writeStream.Close()
fileStream.Close()
readStream.Close()
End Function
End Class