I get the error " Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'." and
" Value of type 'Byte' cannot be converted to '1-dimensional array of Byte' "
on lines 7 and 34 respectively of the code below .
Private Function Encrypt(ByVal textToEncrypt As String, ByVal FilePath As String) As String
Dim rijndaelCipher As New RijndaelManaged()
rijndaelCipher.Mode = CipherMode.CBC
rijndaelCipher.Padding = PaddingMode.PKCS7
rijndaelCipher.KeySize = &H80
rijndaelCipher.BlockSize = &H80 Dim pwdBytes As Byte() = GetFileBytes(FilePath)
Dim keyBytes As Byte() = New Byte(15) {}
Dim len As Integer = pwdBytes.Length
If len > keyBytes.Length Then
len = keyBytes.Length
End If
Array.Copy(pwdBytes, keyBytes, len)
rijndaelCipher.Key = keyBytes
rijndaelCipher.IV = keyBytes
Dim transform As ICryptoTransform = rijndaelCipher.CreateEncryptor()
Dim plainText As Byte() = Encoding.UTF8.GetBytes(textToEncrypt)
Return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length))
End Function
Private Function GetFileBytes(ByVal filePath As [String]) As [Byte]
Dim buffer As Byte()
Dim fileStream As New IO.FileStream(filePath, FileMode.Open, FileAccess.Read)
Try
Dim length As Integer = CInt(fileStream.Length)
buffer = New Byte(length - 1) {}
Dim count As Integer
Dim sum As Integer = 0
While (count = fileStream.Read(buffer, sum, length - sum)) > 0
sum += count
End While
Finally
fileStream.Close()
End Try Return buffer
End Function
Public Class Tester
Public Shared Sub Main
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo("test.txt")
Console.WriteLine("Size: " & fileDetail.Length & " byte(s)")
End Sub
End Class
Public Function GetFileBytes(ByVal path As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte
lngFileNum = FreeFile
If LenB(Dir(path)) Then ''// Does file exist?
Open path For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise 53
End If
GetFileBytes = bytRtnVal
Erase bytRtnVal
End Function
Programming to simplify, dont look for hard way
Marked as answer by Amy Peng - MSFT on Dec 24, 2012 12:14 AM
TCIL
None
0 Points
12 Posts
Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 14, 2012 10:45 AM|LINK
I get the error " Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'." and
" Value of type 'Byte' cannot be converted to '1-dimensional array of Byte' "
on lines 7 and 34 respectively of the code below .
Private Function Encrypt(ByVal textToEncrypt As String, ByVal FilePath As String) As String
Dim rijndaelCipher As New RijndaelManaged()
rijndaelCipher.Mode = CipherMode.CBC
rijndaelCipher.Padding = PaddingMode.PKCS7
rijndaelCipher.KeySize = &H80
rijndaelCipher.BlockSize = &H80
Dim pwdBytes As Byte() = GetFileBytes(FilePath)
Dim keyBytes As Byte() = New Byte(15) {}
Dim len As Integer = pwdBytes.Length
If len > keyBytes.Length Then
len = keyBytes.Length
End If
Array.Copy(pwdBytes, keyBytes, len)
rijndaelCipher.Key = keyBytes
rijndaelCipher.IV = keyBytes
Dim transform As ICryptoTransform = rijndaelCipher.CreateEncryptor()
Dim plainText As Byte() = Encoding.UTF8.GetBytes(textToEncrypt)
Return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length))
End Function
Private Function GetFileBytes(ByVal filePath As [String]) As [Byte]
Dim buffer As Byte()
Dim fileStream As New IO.FileStream(filePath, FileMode.Open, FileAccess.Read)
Try
Dim length As Integer = CInt(fileStream.Length)
buffer = New Byte(length - 1) {}
Dim count As Integer
Dim sum As Integer = 0
While (count = fileStream.Read(buffer, sum, length - sum)) > 0
sum += count
End While
Finally
fileStream.Close()
End Try
Return buffer
End Function
Any fix possible? Please help!
senthilwaits
Contributor
3832 Points
651 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 14, 2012 10:50 AM|LINK
Senthil Kumar Sundaram
TCIL
None
0 Points
12 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 14, 2012 11:01 AM|LINK
This change gives errors on these lines:
1. Dim len As Integer = pwdBytes.Length
error: 'Length' is not a member of 'Byte'.
2. Array.Copy(pwdBytes, keyBytes, len)
Error: Value of type 'Byte' cannot be converted to 'System.Array'.
senthilwaits
Contributor
3832 Points
651 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 14, 2012 11:12 AM|LINK
Dont go with my previous suggestion. Please change your function "GetFileBytes" to return a byte array.
Senthil Kumar Sundaram
RameshRajend...
Star
7983 Points
2099 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 14, 2012 11:17 AM|LINK
Check this sample code
Public Class Tester Public Shared Sub Main Dim fileDetail As IO.FileInfo fileDetail = My.Computer.FileSystem.GetFileInfo("test.txt") Console.WriteLine("Size: " & fileDetail.Length & " byte(s)") End Sub End ClassTCIL
None
0 Points
12 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 15, 2012 07:30 AM|LINK
Is there any way I can avoid using the GETFILEBYTES function? I guess that might solve my problem...
oned_gk
All-Star
31247 Points
6384 Posts
Re: Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'
Dec 15, 2012 08:24 AM|LINK
Public Function GetFileBytes(ByVal path As String) As Byte() Dim lngFileNum As Long Dim bytRtnVal() As Byte lngFileNum = FreeFile If LenB(Dir(path)) Then ''// Does file exist? Open path For Binary Access Read As lngFileNum ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte Get lngFileNum, , bytRtnVal Close lngFileNum Else Err.Raise 53 End If GetFileBytes = bytRtnVal Erase bytRtnVal End Function