I can insert any byte-array into a compression-algorithm like Lempel-Ziv and i will get a lossless compressed byte-array back. I can decompress this again.
But is there an algorithm that can decompress any random byte-array?
I tried it using gzip, but it returned an error System.IO.InvalidDataException: 'The archive entry was compressed using an unsupported compression method.'
What would be the outcome?
Will it be something random, but with repeating patterns?
Let me know what you think!
Oh and btw, here is the code that i used:
public class Compresser
{
public static string CompressString(string text)
{
byte[] byteArray = Encoding.UTF8.GetBytes(text);
using (MemoryStream memoryStream = new MemoryStream())
{
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
gzipStream.Write(byteArray, 0, byteArray.Length);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
public static string DecompressString(string compressedText)
{
byte[] byteArray = Convert.FromBase64String(compressedText);
using (MemoryStream memoryStream = new MemoryStream(byteArray))
{
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
using (MemoryStream decompressedStream = new MemoryStream())
{
gzipStream.CopyTo(decompressedStream);
byte[] decompressedBytes = decompressedStream.ToArray();
return Encoding.UTF8.GetString(decompressedBytes);
}
}
}
}
}
public class Base64RandomFiller
{
private static Random random = new Random();
public static string GenerateRandomBase64String(int length)
{
byte[] randomBytes = new byte[(length * 3) / 4];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes);
}
return Convert.ToBase64String(randomBytes);
}
}
string randomBase64String = Base64RandomFiller.GenerateRandomBase64String(10);
Console.WriteLine("Random String: " + randomBase64String);
Thread.Sleep(1000);
string decompressedRandomString = Compresser.DecompressString(randomBase64String);
Console.WriteLine("Decompressed Random String: " + decompressedRandomString);