//Open file and store it path... private void Button2_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { textBox1.Text = ofd.SafeFileName; fileName = ofd.FileName; Debug.WriteLine($"FilePath: {filepath} "); try { bff = new BootloaderFile(fileName); string result = BitConverter.ToString(bff.IV); Debug.WriteLine($"{ bff.ToFancyString()} IV: {result} "); richTextBox1.Text = bff.ToFancyString(); isClickedButton2 = true; if (isClickedButton3 == true) { button1.Enabled = true; } } catch (Exception ex) { Restart_Params(RESTART_FILE_PARAMS); UpdateStatus(true, ex.Message); button1.Enabled = false; } } }
private static void RunOptions(Options opts) { var inputFilePath = opts.InputFilePath; var keyFilePath = opts.KeyFilePath; var manufacturerName = opts.ManufacturerName; var version = opts.Version; var outputFilePath = opts.OutputFilePath; uint startAddr = 0; uint endAddr = 0; // check ManufacturerName length if (manufacturerName.Length > 31) { throw new ConstraintException("Manufacturer name is too long (max: 31)"); } // parse startAddr and endAddr try { startAddr = Convert.ToUInt32(opts.StartAddr, 16); } catch (FormatException) { startAddr = Convert.ToUInt32(opts.StartAddr); } try { endAddr = Convert.ToUInt32(opts.EndAddr, 16); } catch (FormatException) { endAddr = Convert.ToUInt32(opts.EndAddr); } // read firmware binary var rawFirmwareBinaryList = new List <byte>(); using (var binaryReader = new BinaryReader(new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { while (true) { byte tempByte; try { tempByte = binaryReader.ReadByte(); } catch (EndOfStreamException) { break; } rawFirmwareBinaryList.Add(tempByte); } } while (rawFirmwareBinaryList.Count % 16 != 0) { rawFirmwareBinaryList.Add(0); } // calculate CRC32 var addrCrc = Crc32Algorithm.Compute(BitConverter.GetBytes(startAddr).Concat(BitConverter.GetBytes(endAddr)).ToArray()); var dataCrc = Crc32Algorithm.Compute(rawFirmwareBinaryList.ToArray()); var firstBlock = BitConverter.GetBytes(addrCrc) .Concat(BitConverter.GetBytes(startAddr)) .Concat(BitConverter.GetBytes(endAddr)) .Concat(BitConverter.GetBytes(dataCrc)); // read key byte[] key; using (var binaryReader = new BinaryReader(new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) { key = binaryReader.ReadBytes(32); } // create BootloaderFile var file = new BootloaderFile(); // encrypt var encryptedBinary = Utilities.Encrypt( firstBlock.Concat(rawFirmwareBinaryList).ToArray(), key, ref file ); file.Data = encryptedBinary.Skip(16).ToArray(); file.ManufacturerName = manufacturerName; file.FirstBlock = encryptedBinary.Take(16).ToArray(); // parse version var splitVersion = version.Split("."); for (var i = 0; i < 4; i++) { file.FirmwareVersion[i] = ushort.Parse(splitVersion[i]); } file.WriteBootloaderFile(outputFilePath); Console.WriteLine(file.ToFancyString()); Console.Write("IV: "); }