Posted By

kyrathaba on 01/24/11


Tagged

Encryption decryption TripleDES file-in file-out


Versions (?)

File-in File-out TripleDES encryption using password


 / Published in: C#
 

  1. namespace Kyrathasoft.Cryptography.TripleDES.FileInFileOutUsingPassword {
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Windows.Forms;
  7.  
  8. class clsKyrCrypt {
  9.  
  10. // Encrypt a file into another file using a password
  11.  
  12. public static void Encrypt(string fileIn,
  13. string fileOut, string Password) {
  14.  
  15. // First we are going to open the file streams
  16.  
  17. FileStream fsIn = new FileStream(fileIn,
  18. FileMode.Open, FileAccess.Read);
  19. FileStream fsOut = new FileStream(fileOut,
  20. FileMode.OpenOrCreate, FileAccess.Write);
  21.  
  22. // Then we are going to derive a Key and an IV from the
  23. // Password and create an algorithm
  24.  
  25. PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  26. new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
  27. 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  28.  
  29. Rijndael alg = Rijndael.Create();
  30. alg.Key = pdb.GetBytes(32);
  31. alg.IV = pdb.GetBytes(16);
  32.  
  33. // Now create a crypto stream through which we are going
  34. // to be pumping data.
  35. // Our fileOut is going to be receiving the encrypted bytes.
  36.  
  37. CryptoStream cs = new CryptoStream(fsOut,
  38. alg.CreateEncryptor(), CryptoStreamMode.Write);
  39.  
  40. // Now will will initialize a buffer and will be processing
  41. // the input file in chunks.
  42. // This is done to avoid reading the whole file (which can
  43. // be huge) into memory.
  44.  
  45. int bufferLen = 4096;
  46. byte[] buffer = new byte[bufferLen];
  47. int bytesRead;
  48.  
  49. do {
  50. // read a chunk of data from the input file
  51. bytesRead = fsIn.Read(buffer, 0, bufferLen);
  52. // encrypt it
  53. cs.Write(buffer, 0, bytesRead);
  54. } while (bytesRead != 0);
  55.  
  56. // close everything
  57. // this will also close the unrelying fsOut stream
  58.  
  59. cs.Close();
  60. fsIn.Close();
  61. }
  62.  
  63.  
  64.  
  65. // Decrypt a file into another file using a password
  66.  
  67. public static void Decrypt(string fileIn,
  68. string fileOut, string Password) {
  69.  
  70. // First we are going to open the file streams
  71. FileStream fsIn = new FileStream(fileIn,
  72. FileMode.Open, FileAccess.Read);
  73. FileStream fsOut = new FileStream(fileOut,
  74. FileMode.OpenOrCreate, FileAccess.Write);
  75.  
  76. // Then we are going to derive a Key and an IV from
  77. // the Password and create an algorithm
  78.  
  79. PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
  80. new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
  81. 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
  82. Rijndael alg = Rijndael.Create();
  83.  
  84. alg.Key = pdb.GetBytes(32);
  85. alg.IV = pdb.GetBytes(16);
  86.  
  87. // Now create a crypto stream through which we are going
  88. // to be pumping data.
  89. // Our fileOut is going to be receiving the Decrypted bytes.
  90.  
  91. CryptoStream cs = new CryptoStream(fsOut,
  92. alg.CreateDecryptor(), CryptoStreamMode.Write);
  93.  
  94. // Now will will initialize a buffer and will be
  95. // processing the input file in chunks.
  96. // This is done to avoid reading the whole file (which can be
  97. // huge) into memory.
  98.  
  99. int bufferLen = 4096;
  100. byte[] buffer = new byte[bufferLen];
  101. int bytesRead;
  102.  
  103. do {
  104. // read a chunk of data from the input file
  105. bytesRead = fsIn.Read(buffer, 0, bufferLen);
  106. // Decrypt it
  107. cs.Write(buffer, 0, bytesRead);
  108. } while (bytesRead != 0);
  109.  
  110. // close everything
  111. cs.Close(); // this will also close the unrelying fsOut stream
  112. fsIn.Close();
  113. }
  114.  
  115. }
  116. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: 32U on March 18, 2011

There is an error in this code, but cool snippet. The error is in } while (bytesRead != 0). If the chars in the fsIn is greater then the bufferLen, it will truncate.

I used: } while (fsIn.Position < fsIn.Length); and that seemed to fix it. Thanks for the code though. Very useful.

Posted By: 32U on March 18, 2011

I take it back--I think your code is ok...i think the problem is on my end somehow. Sorry.

You need to login to post a comment.