Note that there are some explanatory texts on larger screens.

plurals
  1. POFile ecnryption using DES decrypting garbage - C#
    primarykey
    data
    text
    <p>I am trying to write a simple file encrypter in C# using <code>DESCryptoServiceProvider</code>. The file format I am encrypting is <code>.jpg</code>. The encryption appears to look fine, but when I decrypt the encrypted file I get an error message in Windows Photo Viewer.</p> <p>Here is my code:</p> <pre><code>public static void Encrypt(string inputFileName, string outputFileName, string key) { var inputPath = path + "\\" + inputFileName; var outputPath = path + "\\" + outputFileName; try { FileStream fsInput = new FileStream(inputPath, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(outputPath, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); DES.Padding = PaddingMode.None; ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length - 1]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); Console.WriteLine("file encrypted @ {0}", outputFileName); } catch(Exception ex) { Console.WriteLine("catch in encryption: " + ex.Message + "\n\nclosing..."); } } public static void Decrypt(string inputFileName, string outputFileName, string key) { var inputPath = path + "\\" + inputFileName; var outputFileHoler = inputFileName.Substring(0, (outputFileName.IndexOf('.') + 4)); //FIX TO REMOVE REGEX var outputPath = path + "\\" + outputFileHoler; Console.WriteLine(outputPath); //TEMP try { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.IV = ASCIIEncoding.ASCII.GetBytes(key); DES.Padding = PaddingMode.None; FileStream fsread = new FileStream(inputPath, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostream = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(outputPath); fsDecrypted.Write(new StreamReader(cryptostream).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); Console.WriteLine("file decrypted @ {0}", outputPath); } catch (Exception ex) { Console.WriteLine("catch in decryption: " + ex.Message + "\n\nclosing..."); } } </code></pre> <p>What am I doing wrong? </p> <p>PS. completely new to cryptography, please forgive me if it is something simple... </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload