Hey there my beloved Community,
i'm currently trying to convert any file into a textfile consisting of it's own bits and bytes (e.g. any exe file to a string of 100010010010010010101...) and then reconverting them to a working file.
the conversion to a string seems to work, but somehow i don't get working files out of my already converted ones...
so i decided to ask you pros about it and present my methods, maybe there's something i didn't quiet get right.
this is the conversion to a string
public static string ConvStep1(string path)
{
byte[] fileBytes = File.ReadAllBytes(path);
string filenamewithoutextension = Path.GetFileNameWithoutExtension(path);
string fileextension = Transform(Path.GetExtension(path).Replace(".", "")).ToLower();
SaveToTextFile(ToBinaryString(fileBytes), filenamewithoutextension +"."+ fileextension + ".cry", path);
File.Delete(path);
return filenamewithoutextension +"."+ fileextension + ".cry";
}
it uses this method for conversion
public static string ToBinaryString(byte[] array)
{
var s = new StringBuilder();
foreach (byte b in array)
s.Append(Convert.ToString(b, 2));
return s.ToString();
}
and it uses this method to write the file:
private static void SaveToTextFile(string data, string FileName, string sourcefile)
{
StreamWriter sw = new StreamWriter(FileName);
sw.Write(data);
sw.Close();
sw.Dispose();
File.Delete(sourcefile);
}
and this is my re-conversion whith the bad outcome
just ignore the transform command as is only changes the extension of the file
public static string ConvStep2(string path)
{
string withcryptedext = Path.GetFileNameWithoutExtension(path);
string filenamewithoutextension = Path.GetFileNameWithoutExtension(withcryptedext);
string fileextension = Transform(Path.GetExtension(withcryptedext).Replace(".", ""));
StreamReader sr = new StreamReader(path);
string a = "";
while (!sr.EndOfStream)
{
a += sr.ReadLine();
}
sr.Close();
sr.Dispose();
SaveToExecFile(a, filenamewithoutextension +"."+ fileextension, path);
return filenamewithoutextension +"."+ fileextension;
}
as you can see, it reads the file line by line and then executes this method
private static void SaveToExecFile(string data, string FileName, string sourcefile)
{
File.WriteAllBytes(FileName,FromBinaryString(data));
File.Delete(sourcefile);
}
public static byte[] FromBinaryString(string s)
{
int count = s.Length / 8;
var b = new byte[count];
for (int i = 0; i < count; i++)
b[i] = Convert.ToByte(s.Substring(i * 8, 8), 2);
return b;
}
as i already said, the output file doesn't work for some reason and consists only of gibberish.
what am i doing wrong?
thank you in advance for your help!