title: How to get file type from encrypted file?
body: <p>How do I get the <strong>file type</strong> using <code>C#</code> from an encrypted file (i.e. <code>file.enc</code>)?</p>
  
  <p><strong>Encryption Method:</strong> Shift Cipher Z<sub>256</sub></p>
  
  <p><strong>Shift Cipher Encryption:</strong></p>
  
  <p>Y<sub>i</sub> = (X<sub>i</sub> + k) % 256</p>
  
  <p>X<sub>i</sub> = (Y<sub>i</sub> - k) % 256</p>
  
  <p><strong>Where:</strong></p>
  
  <p>X<sub>i</sub>, i = 1 : n, is the input in plain bytes.</p>
  
  <p>Y<sub>i</sub>, i = 1 : n, is the output cipher bytes.</p>
  
  <p>k is the shift key which is a secret byte between 1 and 255.</p>
  
  <p>If I have to decrypt the file first, how could I decrypt it without using exhaustive search to find the <strong>shift key</strong>?</p>
  
  <p>I'm not talking about getting <code>.enc</code> as I can already easily do that. I'm not able to determine how the file was before encryption such as <code>.doc</code>, <code>.xls</code>, <code>.pdf</code>, <code>.jpg</code>, or <code>.wav</code> file types.</p>
  
  <p>What I have tried:</p>
  
  <pre><code>byte[] byteArray = File.ReadAllBytes(openFileDialog1.FileName);
  
  // Mean
  double mean = 0;
  
  for (int i = 0; i &lt; byteArray.Length; i++)
  {
      mean += byteArray[i];
  }
  
  mean = mean / byteArray.Length;
  txtMean.Text = mean.ToString("#.000");
  
  // Median
  byteArray.ToList().Sort();
  int median = byteArray[(int)Math.Floor((decimal)(byteArray.Length / 2))];
  txtMedian.Text = median.ToString();
  
  // Mode
  var groups = byteArray.GroupBy(g =&gt; g);
  int maxCount = groups.Max(g =&gt; g.Count());
  int mode = groups.First(g =&gt; g.Count() == maxCount).Key;
  txtMode.Text = mode.ToString();
  
  // Standard Deviation
  double standardDeviation = byteArray.Select(value =&gt; (value - mean) * (value - mean)).Sum();
  standardDeviation = Math.Sqrt(standardDeviation / byteArray.Length);
  txtStandardDeviation.Text = standardDeviation.ToString("#.000");
  
  // Entropy (I don't know how to get this part.)
  int entropy = 0;
  txtEntropy.Text = entropy.ToString();
  </code></pre>
  
  <p>So, from this you can see I take the file, read all bytes of the file and find the <code>mean</code>, <code>median</code>, <code>mode</code>, <code>standard deviation</code>, and <code>entropy</code> values.</p>
  
  <p>By the way, I don't know how to find the entropy value for the file, is there some formula for this or maybe a C# built-in method? I have searched, but found nothing.</p>
  
  <p>I thought using the <code>mode</code> value would be able to determine the file type, but rather it only determines <code>.pdf</code> files as <code>.pdf</code> files have a mode of 48.</p>
  
  <p><code>.doc</code>, <code>.xls</code>, <code>.docx</code>, <code>.xlsx</code>, <code>.jpg</code>, and <code>.wav</code> files all give me a mode of 0.</p>
  
  <p>I have also tried reading the bytes using the following page(s):</p>
  
  <p>ASCII Character Codes Chart 1 - <a href="https://msdn.microsoft.com/en-us/library/60ecse8t(v=vs.80).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/60ecse8t(v=vs.80).aspx</a>
  ASCII Character Codes Chart 2 - <a href="https://msdn.microsoft.com/en-us/library/9hxt0028(v=vs.80).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/9hxt0028(v=vs.80).aspx</a></p>
  
  <p>using this code:</p>
  
  <pre><code>string str = Encoding.ASCII.GetString(byteArray).Substring(0, 256);
  </code></pre>
  
  <p>but it just returns gibberish in which I am unable to determine the difference in file types.</p>