Image of my problem
http://i31.tinypic.com/2v3ivyq.png
Left = Actual notepad.exe opened in notepad
Right = what i get after i read/write.
Goal - Be able to store notepad.exe between to splits and then when the application that is houses the bytes is run, it reads those bytes and saves them.
I have the reading/writing sussed but it looks to me like the encoding is the problem.
Builder -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Polyamory
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string byteString(byte[] myBytes)
{
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(myBytes);
}
private void buildButton_Click(object sender, EventArgs e)
{
try
{
string sPath = "";
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.FileName = "built.exe";
saveFile.Filter = "Executable Files (*.exe)|*.exe";
if (saveFile.ShowDialog() == DialogResult.OK)
sPath = saveFile.FileName;
else
return;
File.Copy(Application.StartupPath + "/Stub.exe", sPath, true);
byte[] myFile = File.ReadAllBytes("C:\\notepad.exe");
File.SetAttributes(sPath, FileAttributes.Normal);
string mySplit = "<@@ENCEXE@@>";
string myEndSplit = "</@@ENCEXE@@/>";
string sUrl = mySplit + byteString(myFile) + myEndSplit;
FileStream myStream = new FileStream(sPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
BinaryWriter myWriter = new BinaryWriter(myStream);
myStream.Position = myStream.Length + 1;
myWriter.Write(sUrl);
myStream.Close();
myStream.Dispose();
myWriter.Close();
MessageBox.Show("Built.");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}
Stub to house the bytes.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;
namespace Polyamorous
{
class Program
{
static string GetBetween(string myFile, string strStart, string strEnd)
{
int iPos, iEnd, lenStart = strStart.Length, startPos = 0;
iPos = myFile.IndexOf(strStart, startPos);
iEnd = myFile.IndexOf(strEnd, iPos + lenStart);
if(iPos != -1 && iEnd != -1)
{
return myFile.Substring(iPos + lenStart, iEnd - (iPos + lenStart));
}
return "error";
}
static byte[] stringByte(string myString)
{
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetBytes(myString);
}
static string byteString(byte[] myBytes)
{
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(myBytes);
}
static void Main()
{
try
{
byte[] myFile = File.ReadAllBytes(Application.ExecutablePath);
string sData = GetBetween(byteString(myFile), "<@@ENCEXE@@>", "</@@ENCEXE@@/>");
File.WriteAllBytes("wtf.exe", stringByte(sData));
}
catch
{
Environment.Exit(0);
}
}
}
}
Hope you guys have some insight into my problem :)