Stuck at Binary

sunarto.gouw

Member
Joined
May 22, 2013
Messages
12
Programming Experience
Beginner
Hey guys. i'm asking for your help.

i have a assignment to complete a program related to video encrypting using serpent algorithm.

i really don't know where to start.

but my friend told me to start from reading the video file in binary, but i don't have idea how to do it.

i've surf in the web anywhere but i still don't find anything.

please someone help me.

i'm using Visual studio 2010

thanks
 
I've never heard of this "serpent algorithm". Do you understand how it works? That would be the place to start. There's no point trying to use it if you don't have a reasonable understanding of it to begin with.

As for reading the data, you can read an entire file into a Byte array by calling File.ReadAllBytes. If the file is large though, that will lead to a high memory footprint for your app and an OutOfMemoryException if the file is too big. For all but fairly small files, it's preferable to read and process only a small chunk at a time. That may or may not be possible in your case, depending on how that algorithm works. Here's some sample code that will read a file in blocks of up to 4KB:
Private Sub ProcessFile(filePath As String)
    'Use a 4KB buffer.
    Const BUFFER_SIZE As Integer = 4 * 1024
    Dim buffer(BUFFER_SIZE - 1) As Byte

    Using sourceFile As FileStream = File.OpenRead(filePath)
        'Read the first block.
        Dim byteCount As Integer = sourceFile.Read(buffer, 0, BUFFER_SIZE)

        Do While byteCount > 0
            'Use data block here, e.g.
            For i = 0 To byteCount - 1
                ProcessByte(buffer(i))
            Next

            'Read the next block.
            byteCount = sourceFile.Read(buffer, 0, BUFFER_SIZE)
        Loop
    End Using

End Sub

Private Sub ProcessByte(data As Byte)
    '...
End Sub
 
I've never heard of this "serpent algorithm". Do you understand how it works? That would be the place to start. There's no point trying to use it if you don't have a reasonable understanding of it to begin with.

As for reading the data, you can read an entire file into a Byte array by calling File.ReadAllBytes. If the file is large though, that will lead to a high memory footprint for your app and an OutOfMemoryException if the file is too big. For all but fairly small files, it's preferable to read and process only a small chunk at a time. That may or may not be possible in your case, depending on how that algorithm works. Here's some sample code that will read a file in blocks of up to 4KB:
Private Sub ProcessFile(filePath As String)
    'Use a 4KB buffer.
    Const BUFFER_SIZE As Integer = 4 * 1024
    Dim buffer(BUFFER_SIZE - 1) As Byte

    Using sourceFile As FileStream = File.OpenRead(filePath)
        'Read the first block.
        Dim byteCount As Integer = sourceFile.Read(buffer, 0, BUFFER_SIZE)

        Do While byteCount > 0
            'Use data block here, e.g.
            For i = 0 To byteCount - 1
                ProcessByte(buffer(i))
            Next

            'Read the next block.
            byteCount = sourceFile.Read(buffer, 0, BUFFER_SIZE)
        Loop
    End Using

End Sub

Private Sub ProcessByte(data As Byte)
    '...
End Sub

thanks.

here is the pseudo code:

p = 0;
l = 0
repeat
while size<128 do
read(p,l);
bufferp[p++] := p;
bufferl[l++] := l;
size := size + length(l);
{size >= 128 or all of pi and li have been read}



{combine the content in bufferl}
merged := bufferl[0];
for i:=1 to length(bufferl)-1 do
merged:= mergearray(merged, bufferl);


while size>=128 do
n := 1;
encrypt/decrypt the block n in merged array;
n := n + 1;
size := size - 128;
{Bufferl data that not encrypted are as many as 'size'}


restore all of the content to bufferl except the data that not encrypted/decrypted
sizetowrite = length(bufferl);
if size>0 then
bufferl[p++] := data that not encrypted
bufferp[l++] := Empty buffer

for i:=0 to sizetowrite do
write(bufferp);
write(bufferl);
p := p ? 1;
l := l -1;


until all of pi and li are read
 
Read file in binary

hi guys, i need your help

i want to read a file (video .mpeg) in binary and display it in msgbox.

please help me.

i put my .mpeg file in D:\

thanks guys
 
What exactly do you mean by "display it in a msgbox"? Do you mean that you want to display the 1s and 0s? Do you mean that you want to display the Bytes in hexadecimal format? Do you mean something else? You need to be clear. Also, consider how much text the contents of a video file is going to generate. Is it really going to be practical to display the entire contents in one go? Do you want to do it in blocks, because you didn;t say so.
 
yes, i want to display the file in 1's and 0's

i want to put it in 128-bit partition block (for the serpent algorithm).

but first i need to proof that my source code is right and it can be readable in binary. that's why i want to change my video file into binary (ex: 0000110101101010101) and display it into messagebox or anything that could be fit in.

thanks for your reply, jmcilhinney
 
It seems Serpent is an AES variant, operating on 128-bit blocks. It's a rather advanced algorithm, surely you had some lessons in cryptography before being assigned this project?

Before trying a full video file, I would start with a single block + 1, so for practical purposes here, 5 bytes. Start by making sure you can encrypt and decrypt those 5 bytes before moving on to a full file.
 
I agree with Herman that you should start small and increase the size of your data over multiple tests. That said, here's a code snippet that will read a file in blocks of up to 16 bytes and display each block in binary format:
Using sourceStream = IO.File.OpenRead("file path here")
    'Read the data in 16-byte blocks.
    Const BUFFER_SIZE As Integer = 16
    Dim buffer(BUFFER_SIZE - 1) As Byte

    'Read the first block.
    Dim byteCount = sourceStream.Read(buffer, 0, BUFFER_SIZE)

    Do Until byteCount = 0
        'Convert each Byte to a binary String.
        'NOTE: Take is required because the buffer may not have been completely filled on the last read.
        Dim binaryBytes = buffer.Take(byteCount).Select(Function(b) Convert.ToString(b, 2))

        'Display the binary data with a space between each pair of bytes to improve readability.
        MessageBox.Show(String.Join(" ", binaryBytes))

        'Read the next block.
        byteCount = sourceStream.Read(buffer, 0, BUFFER_SIZE)
    Loop
End Using
 
I agree with Herman that you should start small and increase the size of your data over multiple tests. That said, here's a code snippet that will read a file in blocks of up to 16 bytes and display each block in binary format:
Using sourceStream = IO.File.OpenRead("file path here")
    'Read the data in 16-byte blocks.
    Const BUFFER_SIZE As Integer = 16
    Dim buffer(BUFFER_SIZE - 1) As Byte

    'Read the first block.
    Dim byteCount = sourceStream.Read(buffer, 0, BUFFER_SIZE)

    Do Until byteCount = 0
        'Convert each Byte to a binary String.
        'NOTE: Take is required because the buffer may not have been completely filled on the last read.
        Dim binaryBytes = buffer.Take(byteCount).Select(Function(b) Convert.ToString(b, 2))

        'Display the binary data with a space between each pair of bytes to improve readability.

        MessageBox.Show(String.Join(" ", binaryBytes))

        'Read the next block.
        byteCount = sourceStream.Read(buffer, 0, BUFFER_SIZE)
    Loop
End Using


thanks jmcilhinney!!! its working!!! now will continue to work on my project.. you're the best!!!!

if i have more question, i'll ask you guys. thanks!!!
 
here we are again bro. i want to ask something. i'm trying to build array of byte. but i caught up some error.

here is my code

Private Structure MemBlock
Dim Data(0 To 3) As Byte
End Structure

but i got an error, "Arrays declared as structure members cannot be declared with an initial size."

how do i fix this?
 
Unlike in a class, member variables in structures cannot be initialised where they are declared. In the code below, all four fields are valid in the class but not in the structure:
Public Class Class1

    Public Integer1 As Integer = 0
    Public String1 As String = ""
    Public Object1 As New Object
    Public Array1(0) As Object

End Class


Public Structure Structure1

    Public Integer1 As Integer = 0
    Public String1 As String = ""
    Public Object1 As New Object
    Public Array1(0) As Object

End Structure
So, either use a class or expose the array via a property that can initialise the array in the getter or both, e.g.
Public Class MemoryBlock

    Private _data As Byte()

    Public ReadOnly Property Data() As Byte()
        Get
            If _data Is Nothing Then
                _data = New Byte(3) {}
            End If

            Return _data
        End Get
    End Property

End Class
That's better anyway because it means that a new array can't be assigned back to replace the existing one.
 
Back
Top