Example #1
0
        private static void generateScriptFromCube3File(String FileName, String scriptFile)
        {
            Encoding    encoding = Encoding.ASCII;
            String      decodedModel;
            BitFromByte bfbObject;

            PaddedBufferedBlockCipher cipher;

            if (FileName != null || FileName.Length > 0)
            {
                try
                {
                    using (var inFile = File.OpenRead(FileName))
                        using (var binaryReader = new BinaryReader(inFile))
                        {
                            inputCubeFile = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
                            bool copyInputFile = true;
                            if (!RawCubeFile())
                            {
                                extractor = new CubeExtractor(inputCubeFile);

                                String rawCube3Filename = extractor.GetCubeFilename();
                                if (rawCube3Filename != null)
                                {
                                    copyInputFile = false;
                                    dataModel     = new byte[extractor.ModelFiles[rawCube3Filename].Length];
                                    Array.Copy(extractor.ModelFiles[rawCube3Filename], dataModel, extractor.ModelFiles[rawCube3Filename].Length);
                                }
                            }

                            if (copyInputFile)
                            {
                                dataModel = new byte[inputCubeFile.Length];
                                Array.Copy(inputCubeFile, dataModel, inputCubeFile.Length);
                            }


                            try
                            {
                                ZeroBytePadding padding = new ZeroBytePadding();
                                cipher = new PaddedBufferedBlockCipher(engine, padding);

                                // make sure buffer is a multiple of Blowfish Block size.
                                int leftover = dataModel.Length % cipher.GetBlockSize();
                                if (leftover != 0)
                                {
                                    Array.Resize(ref dataModel, dataModel.Length + (cipher.GetBlockSize() - leftover));
                                }

                                // create the key parameter
                                Byte[]       keyBytes = encoding.GetBytes(key);
                                KeyParameter param    = new KeyParameter(encoding.GetBytes(key));

                                // initalize the cipher
                                cipher.Init(false, new KeyParameter(keyBytes));

                                byte[] decodedBytes = new byte[cipher.GetOutputSize(dataModel.Length)];

                                int decodedLength = cipher.ProcessBytes(dataModel, 0, dataModel.Length, decodedBytes, 0);
                                if (decodedLength % 8 == 0)
                                {
                                    cipher.DoFinal(decodedBytes, decodedLength);
                                }

                                decodedModel = encoding.GetString(decodedBytes);

                                bfbObject = new BitFromByte(encoding, decodedBytes);

                                CubeScript cscript = new CubeScript(bfbObject);

                                System.IO.StreamWriter file = new System.IO.StreamWriter(scriptFile, false);

                                foreach (String line in cscript.CubeScriptLines)
                                {
                                    file.WriteLine(line);
                                }

                                file.Close();
                            }
                            catch (IOException)
                            {
                                Console.WriteLine($"Unable to process CUBE3 File [{FileName}]\n\n" +
                                                  $"Was this really a CUBE3 file?");
                                Environment.Exit(-1);
                            }
                        }
                }
                catch (SecurityException ex)
                {
                    Console.WriteLine($"Security error\n\nError message: {ex.Message}\n\n" +
                                      $"Details:\n\n{ex.StackTrace}");
                    Environment.Exit(ex.HResult);
                }
            }
            else
            {
                throw new SecurityException("No File specified!");
            }
        }
Example #2
0
        private static void generateBFBFromCube3File(String FileName, String bfbFileName, Boolean xmlMode)
        {
            Encoding    encoding = Encoding.ASCII;
            String      decodedModel;
            BitFromByte bfbObject;

            PaddedBufferedBlockCipher cipher;

            if (FileName != null || FileName.Length > 0)
            {
                try
                {
                    using (var inFile = File.OpenRead(FileName))
                        using (var binaryReader = new BinaryReader(inFile))
                        {
                            inputCube3File = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
                            bool copyInputFile = true;
                            if (!RawCubeFile())
                            {
                                extractor = new CubeExtractor(inputCube3File);

                                String rawCube3Filename = extractor.GetCubeFilename();
                                if (rawCube3Filename != null)
                                {
                                    copyInputFile = false;
                                    dataModel     = new byte[extractor.ModelFiles[rawCube3Filename].Length];
                                    Array.Copy(extractor.ModelFiles[rawCube3Filename], dataModel, extractor.ModelFiles[rawCube3Filename].Length);
                                }
                            }

                            if (copyInputFile)
                            {
                                dataModel = new byte[inputCube3File.Length];
                                Array.Copy(inputCube3File, dataModel, inputCube3File.Length);
                            }

                            try
                            {
                                ZeroBytePadding padding = new ZeroBytePadding();
                                cipher = new PaddedBufferedBlockCipher(engine, padding);

                                // make sure buffer is a multiple of Blowfish Block size.
                                int leftover = dataModel.Length % cipher.GetBlockSize();
                                if (leftover != 0)
                                {
                                    Array.Resize(ref dataModel, dataModel.Length + (cipher.GetBlockSize() - leftover));
                                }

                                // create the key parameter
                                Byte[]       keyBytes = encoding.GetBytes(key);
                                KeyParameter param    = new KeyParameter(encoding.GetBytes(key));

                                // initalize the cipher
                                cipher.Init(false, new KeyParameter(keyBytes));

                                byte[] decodedBytes = new byte[cipher.GetOutputSize(dataModel.Length)];

                                int decodedLength = cipher.ProcessBytes(dataModel, 0, dataModel.Length, decodedBytes, 0);
                                if (decodedLength % 8 == 0)
                                {
                                    cipher.DoFinal(decodedBytes, decodedLength);
                                }

                                decodedModel = encoding.GetString(decodedBytes);

                                if (xmlMode)
                                {
                                    int endXmlIndex = decodedModel.LastIndexOf('>');

                                    //if (endXmlIndex < decodedModel.Length - 1)
                                    //{
                                    //    decodedModel = decodedModel.Substring(0, endXmlIndex + 1);
                                    //}
                                }

                                string[] seperator         = new string[] { "\r\n" };
                                string[] decodedModelArray = decodedModel.Split(seperator, StringSplitOptions.RemoveEmptyEntries);

                                List <String> bfbStringList;
                                if (xmlMode)
                                {
                                    bfbStringList = decodedModelArray.ToList();
                                }
                                else
                                {
                                    bfbObject     = new BitFromByte(encoding, decodedBytes);
                                    bfbStringList = bfbObject.BfbLines;
                                }

                                System.IO.StreamWriter file = new System.IO.StreamWriter(bfbFileName, false);

                                int numBFBLines = 0;
                                foreach (string bfbLine in bfbStringList)
                                {
                                    numBFBLines++;
                                    if (numBFBLines < bfbStringList.Count)
                                    {
                                        file.WriteLine(bfbLine);
                                    }
                                    else
                                    {
                                        file.Write(bfbLine);
                                    }
                                }

                                file.Close();
                            }
                            catch (IOException)
                            {
                                Console.WriteLine($"Unable to process CUBE3 File [{FileName}]\n\n" +
                                                  $"Was this really a CUBE3 file?");
                                Environment.Exit(-1);
                            }
                        }
                }
                catch (SecurityException ex)
                {
                    Console.WriteLine($"Security error\n\nError message: {ex.Message}\n\n" +
                                      $"Details:\n\n{ex.StackTrace}");
                    Environment.Exit(ex.HResult);
                }
            }
            else
            {
                throw new SecurityException("No File specified!");
            }
        }