Results 1 to 1 of 1
  1. #1
    topblast's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Location
    Far from around you Programmer: C++ | VB | C# | JAVA
    Posts
    3,607
    Reputation
    149
    Thanks
    5,052
    My Mood
    Cool

    [RELEASE||HELP]Compression and Encryption of Byte Arrays

    Credit Topblast(making Class) _
    Who ever fix DecryptDecompress

    [php]Imports System
    Imports Microsoft.CSharp
    Imports System.Collections.Generic
    Imports System.IO.Compression
    Imports System.IO
    Imports System.Collections
    Imports System.Text
    Imports System.Security.Cryptography

    Public Class Utility
    Public KEY As Byte()
    Public IV As Byte()
    Sub New(ByVal iKEY As Byte(), ByVal iIV As Byte())
    KEY = iKEY
    IV = iIV
    End Sub

    Sub New()
    KEY = GetKey()
    IV = GetIV()
    End Sub

    Private Function GetKey() As Byte()
    '32 length byte array'
    Dim key As Byte() = {54, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 35, 1}
    Return key
    End Function

    Private Function GetIV() As Byte()
    '16 length byte array'
    Dim IV As Byte() = {45, 65, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 89, 23, 53, 0}
    Return IV
    End Function

    Public Function Compress(ByVal Data As Byte()) As Byte()
    Dim ms As New MemoryStream()
    Using Compressor As GZipStream = New GZipStream(ms, CompressionMode.Compress)

    ' Copy the source file into the compression stream.'
    Compressor.Write(Data, 0, Data.Length)
    Compressor.Flush()
    Compressor.Close()
    Compressor.Dispose()
    End Using
    Dim val = ms.ToArray
    ms.Flush()
    ms.Close()
    ms.Dispose()
    Return val
    End Function

    Public Function Compress(ByVal Str As String) As String
    Dim Data As Byte() = Encoding.ASCII.GetBytes(Str)
    Dim ms As New MemoryStream()
    Using Compressor As GZipStream = New GZipStream(ms, CompressionMode.Compress)

    ' Copy the source file into the compression stream.'
    Compressor.Write(Data, 0, Data.Length)
    Compressor.Flush()
    Compressor.Close()
    Compressor.Dispose()
    End Using
    Dim val = ms.ToArray
    ms.Flush()
    ms.Close()
    ms.Dispose()
    Return Encoding.ASCII.GetString(val)
    End Function

    Public Function Decompress(ByVal Data As Byte()) As Byte()
    Const BUFFER_SIZE As Int32 = 256
    Dim tempArray((BUFFER_SIZE)) As Byte
    Dim tempList As New List(Of Byte())
    Dim NewByte(BUFFER_SIZE) As Byte
    Dim count As Int32 = 0, length As Int32 = 0

    Dim ms As New MemoryStream(Data)
    Using ds As GZipStream = New GZipStream(ms, CompressionMode.Decompress)
    Dim rex = ds.Read(tempArray, 0, BUFFER_SIZE)
    While ((rex) > 0)
    count = rex
    If count = BUFFER_SIZE Then
    tempList.Add(tempArray)
    tempArray = NewByte
    Else
    Dim temp(count) As Byte
    Array.Copy(tempArray, 0, temp, 0, count)
    tempList.Add(temp)
    End If
    length += count
    rex = ds.Read(tempArray, 0, BUFFER_SIZE)
    End While
    Dim retVal(length) As Byte

    count = 0
    For Each temp As Byte() In tempList
    Array.Copy(temp, 0, retVal, count, temp.Length)
    count += temp.Length
    Next
    ms.Flush()
    ms.Close()
    ms.Dispose()
    Return retVal
    End Using
    End Function

    Public Function Decompress(ByVal str As String) As String
    Return Encoding.ASCII.GetString(Decompress(Encoding.ASCII .GetBytes(str)))
    End Function

    Public Function EncryptCompress(ByVal Data As Byte()) As Byte()
    Dim Data1 = Encrypt(Data)
    Return Me.Compress(Data1)
    End Function

    Public Function EncryptCompress(ByVal str As String) As String
    Dim Data1 = Encrypt(str)
    Return Me.Compress(Data1)
    End Function

    Public Function DecryptDecompress(ByVal Data As Byte()) As Byte()
    Dim DECOM = Me.Decompress(Data)
    Return Decrypt(DECOM)
    End Function

    Public Function DecryptDecompress(ByVal str As String) As String
    Dim DECOM = Me.Decompress(str)
    Return Decrypt(DECOM)
    End Function

    Public Function Encrypt(ByVal Data As Byte()) As Byte()
    Dim ms As New MemoryStream()
    Dim RijndaelAlg As Rijndael = Rijndael.Create
    Dim cStream As New CryptoStream(ms, RijndaelAlg.CreateEncryptor(Me.KEY, Me.IV), CryptoStreamMode.Write)
    cStream.Write(Data, 0, Data.Length)
    cStream.Flush()
    cStream.Close()
    Return ms.ToArray()
    End Function

    Public Function Encrypt(ByVal str As String) As String
    Return Encoding.ASCII.GetString(Encrypt(Encoding.ASCII.Ge tBytes(str)))
    End Function

    Public Function Decrypt(ByVal Data As Byte()) As Byte()
    Dim ms As New MemoryStream()
    Dim RijndaelAlg As Rijndael = Rijndael.Create
    Dim cStream As New CryptoStream(ms, RijndaelAlg.CreateDecryptor(Me.KEY, Me.IV), CryptoStreamMode.Write)
    cStream.Write(Data, 0, Data.Length)
    cStream.Flush()
    cStream.Close()
    Return ms.ToArray()
    End Function

    Public Function Decrypt(ByVal str As String) As String
    Return Encoding.ASCII.GetString(Decrypt(Encoding.ASCII.Ge tBytes(str)))
    End Function

    End Class
    [/php]


    When I EncryptCompress. All go well
    after i DecryptDecompress .. HELL ON EARTH
    "Length of data to Decrypt is invalid"

    but when i Encrypt and Decrypt without compression it works Fine

    and for some reason Decompress(str) is not working

    can someone help me.
    Last edited by topblast; 12-24-2010 at 01:58 AM.
    I just like programming, that is all.

    Current Stuff:

    • GPU Programmer (Cuda)
    • Client/Server (Cloud Server)
    • Mobile App Development