locked
how to read the first x numbers of bytes of a file and view in label1.text RRS feed

  • Question

  • how to read the first x numbers of bytes of a file and view in label1.text
    Thursday, April 11, 2019 8:50 AM

All replies

  • Hi,

    I think it should only be decrypted first, then intercept the string.

    Imports System.IO
    Imports System.Security.Cryptography
    Imports System.Text
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim t = MD5Encrypt("1234567890", "qwertyui")
            MsgBox(MD5Decrypt(t, "qwertyui", 5))
        End Sub
        Public Shared Function MD5Encrypt(ByVal pToEncrypt As String, ByVal sKey As String) As String
            Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
            Dim inputByteArray As Byte() = Encoding.[Default].GetBytes(pToEncrypt)
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
            Dim ms As MemoryStream = New MemoryStream()
            Dim cs As CryptoStream = New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim ret As StringBuilder = New StringBuilder()
    
            For Each b As Byte In ms.ToArray()
                ret.AppendFormat("{0:X2}", b)
            Next
    
            ret.ToString()
            Return ret.ToString()
        End Function
    
        Public Shared Function MD5Decrypt(ByVal pToDecrypt As String, ByVal sKey As String, ByVal t As Integer) As String
            Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
            Dim inputByteArray As Byte() = New Byte(pToDecrypt.Length / 2 - 1) {}
    
            For x As Integer = 0 To pToDecrypt.Length / 2 - 1
                Dim i As Integer = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16))
                inputByteArray(x) = CByte(i)
            Next
    
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
            Dim ms As MemoryStream = New MemoryStream()
            Dim cs As CryptoStream = New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim ret As StringBuilder = New StringBuilder()
            Return System.Text.Encoding.Default.GetString(ms.ToArray()).Substring(0, t)
        End Function
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 6:11 AM
  • how to read the first x numbers of bytes of a file and view in label1.text

     I guess that would depend on what you want shown in the label.  Do you need the Byte values shown, or are the bytes suppose to be read and displayed as a string like a RIFF file identifier or something?  Just curios,  is this for a common file type?

     Example of reading the first 4 bytes of a Wave audio file which is a RIFF file type and the first 4 bytes can be read as a string "RIFF", or just the byte values.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim bts() As Byte = IO.File.ReadAllBytes("C:\Test\Audio Files\Laser Gun.wav").Take(4).ToArray 'get the first 4 bytes of Wave audio file, a RIFF file
    
            Label1.Text = System.Text.Encoding.ASCII.GetString(bts) 'encodes the 4 bytes as an ASCII string. For a Wave audio file this will be "RIFF"
    
            Label2.Text = String.Join(", ", bts) 'display the 4 byte values
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Friday, April 12, 2019 11:11 PM
    Friday, April 12, 2019 11:10 PM