Inferno986

Hi,

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




Re: .NET Compact Framework Type is not defined

AndrewBadera

Did you perhaps mean 'PasswordDeriveBytes' Derive, without the 'r'



Re: .NET Compact Framework Type is not defined

Inferno986

Yes, sorry that was a typo.

Any ideas on how to solve this




Re: .NET Compact Framework Type is not defined

AndrewBadera

it still gives you the same error with the typo corrected



Re: .NET Compact Framework Type is not defined

Inferno986

Yep




Re: .NET Compact Framework Type is not defined


Re: .NET Compact Framework Type is not defined

Inferno986

Ok then, lets say that the compact framework doesnt support it.

I still need to find a way that I can execure the SetKey function and accieve the same job but with out using the PasswoDeriveBytes type.

If I comment out all the lines in the SetKey function the code will work, however If I am to encrypt the file with a password it will still successfully decrypt the file regardless of what the password is.

Thanks for the help so far. I am desperate to find a solution.




Re: .NET Compact Framework Type is not defined

AndrewBadera

you're just looking for the byte array of the password + the salt when password is a string, you can use:

Encoding.UTF8.GetBytes(plainText);

where plainText is a string value.

you can get a byte array of the password+salt by simply populating an array of the proper length with the bytes from your password byte array and your salt byte array.

 

if you need to get a Base64 string out of a byte array:

Convert.ToBase64String(hashWithSaltBytes);

where hashWithSaltBytes is your byte array.





Re: .NET Compact Framework Type is not defined

Inferno986

I'm sorry, but I am not really sure on how exactly to impliment it.

Please can you show me what exactly i need to change

thanks again




Re: .NET Compact Framework Type is not defined

AndrewBadera

at first glance I thought PasswordDeriveBytes was simply giving you the byte array of your password+salt. reading a little more closely, I see it's generating a key based on the crypto algorithm. I'm not certain what the alternative would be for this crypto implementation under NETCF.



Re: .NET Compact Framework Type is not defined

Michael Koster

One of the following 3rd party libraries do provide an implementation of the unsupported PasswordDeriveBytes class:

OpenNETCF SDK : www.opennetcf.org
cfAES: http://www.mperfect.net/cfAes

You may consider using one of those implementations.