Base64 Library of Babel – C# Code

Home Forums Code, Bugs, Suggestions, Questions Base64 Library of Babel – C# Code

This topic contains 6 replies, has 2 voices, and was last updated by  KailGale 3 months, 1 week ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #6014 Reply

    MMC

    I see a lot of people asking for the code behind the site. Whilst I am not able to provide that, I am able to provide code for a variation of this site written in C#.

    The below code uses 13 coodinates to return a unique permutation of Base64 encoded data. The number of permuatations accessible is:

    4846571103979087938519772293817400974036377530889529986563438506934527898215687918876665574247879221649973281064002360003246175309802208251259722030882898066992206176275913680678380341273818456673639922888445574614624735641282793357397087854564453968627292664494154644541893793064637948155288606294982665455002184231224643080920849815218593081258249079432339983100081787109375

    Yes, that’s not a mistake.

    How to use it?

    Create an instance of the class, fill out the properties and call GetParagraph(). That’s it. By itself, there is nothing interesting, however, if you drive the inputs randomly and append them together then you can create files of arbirary sizes. Just take the bytes of the Base64 encoded stream and try to decode them with any encoding scheme (ASCII, MP3, JPG, etc).

    The interesting thing is that the solution space is laid out sequentially and is also composible. That means you can define aspects such as headers, or critical portions, to reduce your search space.

    Obviously, this is best suited for Big Data roles, but you could strike it lucky on a home PC.

    Will you see pictures of Aliens? Hear music from the future?

    Try it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Numerics;
    
    namespace LibraryOfBabel.Babel
    {
        public class BabelLibrary
        {
            #region Constants
    
            private BigInteger maxSolarSystems = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxPlanets = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxCountries = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxCities = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxStreets = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxBuildings = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxWings = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxRooms = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxWalls = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxShelfs = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxVolumes = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxPages = BigInteger.Parse("79228162514264337593543950335");
            private BigInteger maxParagraphs = BigInteger.Parse("79228162514264337593543950335");
    
            #endregion
    
            #region Properties
    
            public BigInteger SolarSystem { get; set; }
            public BigInteger Planet { get; set; }
            public BigInteger Country { get; set; }
            public BigInteger City { get; set; }
            public BigInteger Street { get; set; }
            public BigInteger Building { get; set; }
            public BigInteger Wing { get; set; }
            public BigInteger Room { get; set; }
            public BigInteger Wall { get; set; }
            public BigInteger Shelf { get; set; }
            public BigInteger Volume { get; set; }
            public BigInteger Page { get; set; }
            public BigInteger Paragraph { get; set; }
    
            #endregion
    
            #region Get Paragraph
    
            public string GetParagraph()
            {
                byte[] location = GetLocation();
                return GetPermutation(location);
            }
    
            #endregion
    
            #region Get Location
    
            private byte[] GetLocation()
            {
                BigInteger pageUnits = Page * maxParagraphs;
                BigInteger volumeUnits = Volume * maxPages * maxParagraphs;
                BigInteger ShelfUnits = Shelf * maxVolumes * maxPages * maxParagraphs;
                BigInteger wallUnits = Wall * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger roomUnits = Room * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger wingUnits = Wing * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger buildingUnits = Building * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger streetUnits = Street * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger cityUnits = City * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger countryUnits = Country * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger planetUnits = Planet * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger solarsystemUnits = SolarSystem * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
    
                BigInteger bint = Paragraph + pageUnits + volumeUnits + ShelfUnits + wallUnits + roomUnits + wingUnits + buildingUnits + streetUnits + cityUnits + countryUnits + planetUnits + solarsystemUnits;
                return bint.ToByteArray();
            }
    
            #endregion
    
            #region Convert to Base64
    
            private string GetPermutation(byte[] bigIntArray)
            {
                return System.Convert.ToBase64String(bigIntArray);
            }
    
            #endregion
        }
    }
    
    #6016 Reply

    Jonathan Basile
    Keymaster

    Thank you for sharing, MMC!

    #6025 Reply

    MMC

    No problem. Its fun to play with this type of program again. I’ve updated that original program given above, to include two additional dimensions (Clusters, Galaxies), created a class to read in a file, encode it as coords which then can be saved. There is also a decoder which reverses the process. This part was mainly to demonstrate that such algorithms cannot be used for compression as there is a one-to-one mapping between the address space and the content. That is, the address is the content, just in a different base.

    I’ve tested this using an 60MB mp4 and it encodes the file to around 198MB.

    Whilst it is of absolutely no use as traditional compression, it is still an open question whether or not this could be used as a data repository to a deep learning network. Its structure lends well to training such algorithms. Rather than requiring endless databases of data, the neural network could be trained to compose data at runtime that has some relation to the inputs.

    That has a lot of useful applications, especially in shrinking data intensive applications.

    public class BabelEncoder
        {
            #region Babel Encoder
    
            public void Encode(string inputFileName, string outputFileName)
            {
                using (FileStream inputFS = new FileStream(inputFileName, FileMode.Open))
                {
                    int currentInputByte = 0;
                    int currentOutputByte = 0;
                    BabelLibrary bl = new BabelLibrary();
    
                    while(currentInputByte < inputFS.Length)
                    {
                        byte[] inputFile = new byte[8192];
                        inputFS.Seek(currentInputByte, SeekOrigin.Begin);
                        inputFS.Read(inputFile, 0, inputFile.Length);
                        Location location = bl.BabelEncode(inputFile);
                        currentInputByte += inputFile.Length;
    
                        string output = null;
                        
                        if(currentInputByte < inputFS.Length)
                            output = location.Cluster.ToString() + ":" + location.Galaxy.ToString() + ":" + location.SolarSystem.ToString() + ":" + location.Planet.ToString() + ":" + location.Country.ToString() + ":" + location.City.ToString() + ":" + location.Street.ToString() + ":" + location.Building.ToString() + ":" + location.Wing.ToString() + ":" + location.Room.ToString() + ":" + location.Wall.ToString() + ":" + location.Shelf.ToString() + ":" + location.Volume.ToString() + ":" + location.Page.ToString() + ":" + location.Paragraph.ToString() + ",";
                        else
                            output = location.Cluster.ToString() + ":" + location.Galaxy.ToString() + ":" + location.SolarSystem.ToString() + ":" + location.Planet.ToString() + ":" + location.Country.ToString() + ":" + location.City.ToString() + ":" + location.Street.ToString() + ":" + location.Building.ToString() + ":" + location.Wing.ToString() + ":" + location.Room.ToString() + ":" + location.Wall.ToString() + ":" + location.Shelf.ToString() + ":" + location.Volume.ToString() + ":" + location.Page.ToString() + ":" + location.Paragraph.ToString();
    
                        using (FileStream outputFS = new FileStream(outputFileName, FileMode.Append))
                        {
                            byte[] outputFile = System.Text.Encoding.ASCII.GetBytes(output);
                            inputFS.Seek(currentOutputByte, SeekOrigin.Begin);
                            outputFS.Write(outputFile, 0, outputFile.Length);
                            currentOutputByte += outputFile.Length;
                        }
                    }
                }
            }
    
            #endregion
    
            #region Babel Decoder
    
            public void Decode(string inputFileName, string outputFileName)
            {
                List<int> babelBlocks = new List<int>();
                long eof = 0;
                int currentByte = 0;
                int blockcount = 0;
    
                //Scan for comma separators as we don't know the size of the babelblocks
                using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
                {
                    while (currentByte < reader.Length)
                    {
                        byte[] temp = null;
    
                        if (reader.Length - currentByte >= 8191)
                            temp = new byte[8192];
                        else
                            temp = new byte[reader.Length - currentByte];
    
                        reader.Seek(currentByte, SeekOrigin.Begin);
                        reader.Read(temp, 0, temp.Length);
                        currentByte += temp.Length;
                        
    
                        for (int i = 0; i < temp.Length; i++)
                        {
                            if (temp[i] == 44)
                                babelBlocks.Add(i + blockcount);
                        }
    
                        blockcount += temp.Length;
                    }
                    eof = reader.Length - 1;
                }
    
                //Grab each BabelBlock
                using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
                {
                    currentByte = 0;
                    int index = 0;
                    int currentOutputByte = 0;
    
                    while (currentByte < reader.Length)
                    {
                        byte[] temp;
    
                        if(index < babelBlocks.Count())
                            temp = new byte[babelBlocks.ElementAt(index) - currentByte];
                        else
                            temp = new byte[reader.Length - 1 - currentByte];
    
                        reader.Seek(currentByte, SeekOrigin.Begin);
                        reader.Read(temp, 0, temp.Length);
    
                        currentByte += temp.Length + 1;
                        index++;
                        string[] location = (Encoding.ASCII.GetString(temp, 0, temp.Length)).Split(':');
    
                        BabelLibrary bl = new BabelLibrary();
    
                        bl.Cluster = BigInteger.Parse(location[0]);
                        bl.Galaxy = BigInteger.Parse(location[1]);
                        bl.SolarSystem = BigInteger.Parse(location[2]);
                        bl.Planet = BigInteger.Parse(location[3]);
                        bl.Country = BigInteger.Parse(location[4]);
                        bl.City = BigInteger.Parse(location[5]);
                        bl.Street = BigInteger.Parse(location[6]);
                        bl.Building = BigInteger.Parse(location[7]);
                        bl.Wing = BigInteger.Parse(location[8]);
                        bl.Room = BigInteger.Parse(location[9]);
                        bl.Wall = BigInteger.Parse(location[10]);
                        bl.Shelf = BigInteger.Parse(location[11]);
                        bl.Volume = BigInteger.Parse(location[12]);
                        bl.Page = BigInteger.Parse(location[13]);
                        bl.Paragraph = BigInteger.Parse(location[14]);
                        
                        byte[] newFile = bl.BabelDecode();
    
                        using (var stream = new FileStream(outputFileName, FileMode.Append))
                        {
                            stream.Seek(currentOutputByte, SeekOrigin.Begin);
                            stream.Write(newFile, 0, newFile.Length);
                            currentOutputByte += newFile.Length;
                        }
                    }
                }
            }
    
            #endregion
        }
    	
    	#region Location Struct
    
        public struct Location
        {
            public BigInteger Cluster;
            public BigInteger Galaxy;
            public BigInteger SolarSystem;
            public BigInteger Planet;
            public BigInteger Country;
            public BigInteger City;
            public BigInteger Street;
            public BigInteger Building;
            public BigInteger Wing;
            public BigInteger Room;
            public BigInteger Wall;
            public BigInteger Shelf;
            public BigInteger Volume;
            public BigInteger Page;
            public BigInteger Paragraph;
        }
    
        #endregion
    
        public class BabelLibrary
        {
            #region Constants
    
            private BigInteger maxClusters = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxGalaxies = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxSolarSystems = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxPlanets = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxCountries = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxCities = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxStreets = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxBuildings = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxWings = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxRooms = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxWalls = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxShelfs = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxVolumes = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxPages = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxParagraphs = BigInteger.Parse("1000000000000000000000000000000000000000");
    
            #endregion
    
            #region Properties
    
            public BigInteger Cluster { get; set; }
            public BigInteger Galaxy { get; set; }
            public BigInteger SolarSystem { get; set; }
            public BigInteger Planet { get; set; }
            public BigInteger Country { get; set; }
            public BigInteger City { get; set; }
            public BigInteger Street { get; set; }
            public BigInteger Building { get; set; }
            public BigInteger Wing { get; set; }
            public BigInteger Room { get; set; }
            public BigInteger Wall { get; set; }
            public BigInteger Shelf { get; set; }
            public BigInteger Volume { get; set; }
            public BigInteger Page { get; set; }
            public BigInteger Paragraph { get; set; }
    
            #endregion
    
            #region Encode/Decode Methods
    
            public Location BabelEncode(byte[] byteArray)
            {
                string bytes = ConvertToBase64(byteArray);
                return RetrieveCoordsOfB64String(Encoding.ASCII.GetBytes(bytes));
            }
    
            public byte[] BabelDecode()
            {
                byte[] location = RetrieveB64StringFromCoords();
                return ConvertFromBase64(Encoding.ASCII.GetString(location));
            }
    
            #endregion
    
            #region Convert to Base64
    
            private string ConvertToBase64(byte[] bigIntArray)
            {
                return System.Convert.ToBase64String(bigIntArray);
            }
    
            #endregion
    
            #region Convert from Base64
    
            private byte[] ConvertFromBase64(string base64String)
            {
                return System.Convert.FromBase64String(base64String);
            }
    
            #endregion
    
            #region Retrieve Coords (Encode Stage)
    
            private Location RetrieveCoordsOfB64String(byte[] byteArray)
            {
                BigInteger bint = new BigInteger(byteArray.Concat(new byte[] { 0 }).ToArray());  //Append Positive Value
    
                BigInteger runningTotal = bint;
                BigInteger clusterUnits = BigInteger.Parse("0");
                BigInteger galaxyUnits = BigInteger.Parse("0");
                BigInteger solarsystemUnits = BigInteger.Parse("0");
                BigInteger planetUnits = BigInteger.Parse("0");
                BigInteger countryUnits = BigInteger.Parse("0");
                BigInteger cityUnits = BigInteger.Parse("0");
                BigInteger streetUnits = BigInteger.Parse("0");
                BigInteger buildingUnits = BigInteger.Parse("0");
                BigInteger wingUnits = BigInteger.Parse("0");
                BigInteger roomUnits = BigInteger.Parse("0");
                BigInteger wallUnits = BigInteger.Parse("0");
                BigInteger shelfUnits = BigInteger.Parse("0");
                BigInteger volumeUnits = BigInteger.Parse("0");
                BigInteger pageUnits = BigInteger.Parse("0");
                BigInteger paragraphUnits = BigInteger.Parse("0");
    
                if (runningTotal > 0)
                {
                    BigInteger clusterMagnitude = maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    clusterUnits = runningTotal / clusterMagnitude;
                    runningTotal = runningTotal % clusterMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger galaxyMagnitude = maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    galaxyUnits = runningTotal / galaxyMagnitude;
                    runningTotal = runningTotal % galaxyMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger solarSystemMagnitude = maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    solarsystemUnits = runningTotal / solarSystemMagnitude;
                    runningTotal = runningTotal % solarSystemMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger planetMagnitude = maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    planetUnits = runningTotal / planetMagnitude;
                    runningTotal = runningTotal % planetMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger countryMagnitude = maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    countryUnits = runningTotal / countryMagnitude;
                    runningTotal = runningTotal % countryMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger cityMagnitude = maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    cityUnits = runningTotal / cityMagnitude;
                    runningTotal = runningTotal % cityMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger streetMagnitude = maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    streetUnits = runningTotal / streetMagnitude;
                    runningTotal = runningTotal % streetMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger buildingMagnitude = maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    buildingUnits = runningTotal / buildingMagnitude;
                    runningTotal = runningTotal % buildingMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger wingMagnitude = maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    wingUnits = runningTotal / wingMagnitude;
                    runningTotal = runningTotal % wingMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger roomMagnitude = maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    roomUnits = runningTotal / roomMagnitude;
                    runningTotal = runningTotal % roomMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger wallMagnitude = maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    wallUnits = runningTotal / wallMagnitude;
                    runningTotal = runningTotal % wallMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger shelfMagnitude = maxVolumes * maxPages * maxParagraphs;
                    shelfUnits = runningTotal / shelfMagnitude;
                    runningTotal = runningTotal % shelfMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger volumeMagnitude = maxPages * maxParagraphs;
                    volumeUnits = runningTotal / volumeMagnitude;
                    runningTotal = runningTotal % volumeMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger pageMagnitude = maxParagraphs;
                    pageUnits = runningTotal / pageMagnitude;
                    runningTotal = runningTotal % pageMagnitude;
                }
    
                if (runningTotal > 0)
                    paragraphUnits = runningTotal;
    
                Location location = new Location();
                location.Cluster = clusterUnits;
                location.Galaxy = galaxyUnits;
                location.SolarSystem = solarsystemUnits;
                location.Planet = planetUnits;
                location.Country = countryUnits;
                location.City = cityUnits;
                location.Street = streetUnits;
                location.Building = buildingUnits;
                location.Wing = wingUnits;
                location.Room = roomUnits;
                location.Wall = wallUnits;
                location.Shelf = shelfUnits;
                location.Volume = volumeUnits;
                location.Page = pageUnits;
                location.Paragraph = paragraphUnits;
    
                return location;
            }
    
            #endregion
    
            #region Retrieve B64 String (Decode Stage)
    
            private byte[] RetrieveB64StringFromCoords()
            {
                BigInteger pageUnits = Page * maxParagraphs;
                BigInteger volumeUnits = Volume * maxPages * maxParagraphs;
                BigInteger ShelfUnits = Shelf * maxVolumes * maxPages * maxParagraphs;
                BigInteger wallUnits = Wall * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger roomUnits = Room * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger wingUnits = Wing * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger buildingUnits = Building * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger streetUnits = Street * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger cityUnits = City * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger countryUnits = Country * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger planetUnits = Planet * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger solarsystemUnits = SolarSystem * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger galaxyUnits = Galaxy * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger clusterUnits = Cluster * maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
    
                BigInteger bint = Paragraph + pageUnits + volumeUnits + ShelfUnits + wallUnits + roomUnits + wingUnits + buildingUnits + streetUnits + cityUnits + countryUnits + planetUnits + solarsystemUnits + galaxyUnits + clusterUnits;
    
                byte[] bar = bint.ToByteArray();
    
                if (bar.Length == 8193)
                {
                    byte[] newbar = new byte[bar.Length - 1];
                    Array.Copy(bar, newbar, newbar.Length);
                    return newbar;
                }
                else
                    return bar;
            }
    
            #endregion
        }
    #6026 Reply

    MMC

    No problem. Its fun to play with this type of program again. I’ve updated that original program given above, to include two additional dimensions (Clusters, Galaxies), created a class to read in a file, encode it as coords which then can be saved. There is also a decoder which reverses the process. This part was mainly to demonstrate that such algorithms cannot be used for compression as there is a one-to-one mapping between the address space and the content. That is, the address is the content, just in a different base.

    I’ve tested this using an 60MB mp4 and it encodes the file to around 198MB.

    Whilst it is of absolutely no use as traditional compression, it is still an open question whether or not this could be used as a data repository to a deep learning network. Its structure lends well to training such algorithms. Rather than requiring endless databases of data, the neural network could be trained to compose data at runtime that has some relation to the inputs.

    That has a lot of useful applications, especially in shrinking data intensive applications.

    I’ll post the code over the next couple of posts, as I think it is too large for a single post.

    #6027 Reply

    MMC

    Here is the updated Babel Encoder class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Numerics;
    
    namespace LibraryOfBabel.Babel
    {
        #region Location Struct
    
        public struct Location
        {
            public BigInteger Cluster;
            public BigInteger Galaxy;
            public BigInteger SolarSystem;
            public BigInteger Planet;
            public BigInteger Country;
            public BigInteger City;
            public BigInteger Street;
            public BigInteger Building;
            public BigInteger Wing;
            public BigInteger Room;
            public BigInteger Wall;
            public BigInteger Shelf;
            public BigInteger Volume;
            public BigInteger Page;
            public BigInteger Paragraph;
        }
    
        #endregion
    
        public class BabelLibrary
        {
            #region Constants
    
            private BigInteger maxClusters = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxGalaxies = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxSolarSystems = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxPlanets = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxCountries = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxCities = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxStreets = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxBuildings = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxWings = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxRooms = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxWalls = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxShelfs = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxVolumes = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxPages = BigInteger.Parse("1000000000000000000000000000000000000000");
            private BigInteger maxParagraphs = BigInteger.Parse("1000000000000000000000000000000000000000");
    
            #endregion
    
            #region Properties
    
            public BigInteger Cluster { get; set; }
            public BigInteger Galaxy { get; set; }
            public BigInteger SolarSystem { get; set; }
            public BigInteger Planet { get; set; }
            public BigInteger Country { get; set; }
            public BigInteger City { get; set; }
            public BigInteger Street { get; set; }
            public BigInteger Building { get; set; }
            public BigInteger Wing { get; set; }
            public BigInteger Room { get; set; }
            public BigInteger Wall { get; set; }
            public BigInteger Shelf { get; set; }
            public BigInteger Volume { get; set; }
            public BigInteger Page { get; set; }
            public BigInteger Paragraph { get; set; }
    
            #endregion
    
            #region Encode/Decode Methods
    
            public Location BabelEncode(byte[] byteArray)
            {
                string bytes = ConvertToBase64(byteArray);
                return RetrieveCoordsOfB64String(Encoding.ASCII.GetBytes(bytes));
            }
    
            public byte[] BabelDecode()
            {
                byte[] location = RetrieveB64StringFromCoords();
                return ConvertFromBase64(Encoding.ASCII.GetString(location));
            }
    
            #endregion
    
            #region Convert to Base64
    
            private string ConvertToBase64(byte[] bigIntArray)
            {
                return System.Convert.ToBase64String(bigIntArray);
            }
    
            #endregion
    
            #region Convert from Base64
    
            private byte[] ConvertFromBase64(string base64String)
            {
                return System.Convert.FromBase64String(base64String);
            }
    
            #endregion
    
            #region Retrieve Coords (Encode Stage)
    
            private Location RetrieveCoordsOfB64String(byte[] byteArray)
            {
                BigInteger bint = new BigInteger(byteArray.Concat(new byte[] { 0 }).ToArray());  //Append Positive Value
    
                BigInteger runningTotal = bint;
                BigInteger clusterUnits = BigInteger.Parse("0");
                BigInteger galaxyUnits = BigInteger.Parse("0");
                BigInteger solarsystemUnits = BigInteger.Parse("0");
                BigInteger planetUnits = BigInteger.Parse("0");
                BigInteger countryUnits = BigInteger.Parse("0");
                BigInteger cityUnits = BigInteger.Parse("0");
                BigInteger streetUnits = BigInteger.Parse("0");
                BigInteger buildingUnits = BigInteger.Parse("0");
                BigInteger wingUnits = BigInteger.Parse("0");
                BigInteger roomUnits = BigInteger.Parse("0");
                BigInteger wallUnits = BigInteger.Parse("0");
                BigInteger shelfUnits = BigInteger.Parse("0");
                BigInteger volumeUnits = BigInteger.Parse("0");
                BigInteger pageUnits = BigInteger.Parse("0");
                BigInteger paragraphUnits = BigInteger.Parse("0");
    
                if (runningTotal > 0)
                {
                    BigInteger clusterMagnitude = maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    clusterUnits = runningTotal / clusterMagnitude;
                    runningTotal = runningTotal % clusterMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger galaxyMagnitude = maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    galaxyUnits = runningTotal / galaxyMagnitude;
                    runningTotal = runningTotal % galaxyMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger solarSystemMagnitude = maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    solarsystemUnits = runningTotal / solarSystemMagnitude;
                    runningTotal = runningTotal % solarSystemMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger planetMagnitude = maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    planetUnits = runningTotal / planetMagnitude;
                    runningTotal = runningTotal % planetMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger countryMagnitude = maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    countryUnits = runningTotal / countryMagnitude;
                    runningTotal = runningTotal % countryMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger cityMagnitude = maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    cityUnits = runningTotal / cityMagnitude;
                    runningTotal = runningTotal % cityMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger streetMagnitude = maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    streetUnits = runningTotal / streetMagnitude;
                    runningTotal = runningTotal % streetMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger buildingMagnitude = maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    buildingUnits = runningTotal / buildingMagnitude;
                    runningTotal = runningTotal % buildingMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger wingMagnitude = maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    wingUnits = runningTotal / wingMagnitude;
                    runningTotal = runningTotal % wingMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger roomMagnitude = maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    roomUnits = runningTotal / roomMagnitude;
                    runningTotal = runningTotal % roomMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger wallMagnitude = maxShelfs * maxVolumes * maxPages * maxParagraphs;
                    wallUnits = runningTotal / wallMagnitude;
                    runningTotal = runningTotal % wallMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger shelfMagnitude = maxVolumes * maxPages * maxParagraphs;
                    shelfUnits = runningTotal / shelfMagnitude;
                    runningTotal = runningTotal % shelfMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger volumeMagnitude = maxPages * maxParagraphs;
                    volumeUnits = runningTotal / volumeMagnitude;
                    runningTotal = runningTotal % volumeMagnitude;
                }
    
                if (runningTotal > 0)
                {
                    BigInteger pageMagnitude = maxParagraphs;
                    pageUnits = runningTotal / pageMagnitude;
                    runningTotal = runningTotal % pageMagnitude;
                }
    
                if (runningTotal > 0)
                    paragraphUnits = runningTotal;
    
                Location location = new Location();
                location.Cluster = clusterUnits;
                location.Galaxy = galaxyUnits;
                location.SolarSystem = solarsystemUnits;
                location.Planet = planetUnits;
                location.Country = countryUnits;
                location.City = cityUnits;
                location.Street = streetUnits;
                location.Building = buildingUnits;
                location.Wing = wingUnits;
                location.Room = roomUnits;
                location.Wall = wallUnits;
                location.Shelf = shelfUnits;
                location.Volume = volumeUnits;
                location.Page = pageUnits;
                location.Paragraph = paragraphUnits;
    
                return location;
            }
    
            #endregion
    
            #region Retrieve B64 String (Decode Stage)
    
            private byte[] RetrieveB64StringFromCoords()
            {
                BigInteger pageUnits = Page * maxParagraphs;
                BigInteger volumeUnits = Volume * maxPages * maxParagraphs;
                BigInteger ShelfUnits = Shelf * maxVolumes * maxPages * maxParagraphs;
                BigInteger wallUnits = Wall * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger roomUnits = Room * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger wingUnits = Wing * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger buildingUnits = Building * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger streetUnits = Street * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger cityUnits = City * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger countryUnits = Country * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger planetUnits = Planet * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger solarsystemUnits = SolarSystem * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger galaxyUnits = Galaxy * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
                BigInteger clusterUnits = Cluster * maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
    
                BigInteger bint = Paragraph + pageUnits + volumeUnits + ShelfUnits + wallUnits + roomUnits + wingUnits + buildingUnits + streetUnits + cityUnits + countryUnits + planetUnits + solarsystemUnits + galaxyUnits + clusterUnits;
    
                byte[] bar = bint.ToByteArray();
    
                if (bar.Length == 8193)
                {
                    byte[] newbar = new byte[bar.Length - 1];
                    Array.Copy(bar, newbar, newbar.Length);
                    return newbar;
                }
                else
                    return bar;
            }
    
            #endregion
        }
    }
    
    #6030 Reply

    MMC

    I’ve tried posting the code to read/write files but it silently fails. So, I posted it on PasteBin here http://pastebin.com/htwMMr5h

    #29463 Reply

    KailGale

    Hello,
    I am using Visual Studio 2017. Above you say create an instance of the class and fill out the properties. I am fiddling around with making a WPF that has a search function and returns results in a textblock. My first question is, when you say instance of, I take that to mean something like this:

    public void SearchP()
    {
    BabelLibrary myLib = new BabelLibrary();
    myLib.Cluster = 10;
    }
    -Just an example not the actual implementation.
    Is that correct? Second, I am not sure what values to put in the properties. If I do not put anything, it looks like RetrieveB64StringFormCoords() will return nothing or 0 so, do I have to
    specify each property? if I understand it correctly, these properties are the coords you want to look up and will return a base 64 string. Is that correct?

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Base64 Library of Babel – C# Code
Your information: