Quantcast

Maximum PC

It is currently Tue Sep 16, 2014 7:45 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: File Encryption / Decryption
PostPosted: Tue Oct 07, 2008 1:00 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Okay, so I am playing with this in preparation to really make some good use of the idea of encrypting/decrypting text files etc.

I did some googling on top of this, and it appears that people claim this does not work in .NET 2.0 (it does, I did it). However, I am not using DES I tried TripleDES (After finding out that AES isn't supported in 2.0 version of System.Security.Cryptography :( ) I do plan to find or implement AES later but it isn't necessary for proof of concept right now.

My issue is that the last character of my text file is missing from the final decryption. Does anybody have any ideas about this? Do you have a better method?

Also, something that isn't mission critical right now, can anybody suggest a change (or changes) that might make this work with more than just text files?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 2:46 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
http://msdn.microsoft.com/en-us/library ... ndael.aspx

Rijndael is AES with more block sizes (it was the submission name for AES). While it might not be exact, it should do the job close enough

Anyways, I've mainly played with hashes but I can try and look and see if I have any code that might help.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 4:53 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Any idea why I am losing the last character of the original text file after decryption?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 5:20 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I found it, but I want to see if anybody else spots it in the article.

There is a funny point to mention, the bug is NOT in the code here, from the full code listing in the article. It is in a section in the article. This code doesn't have the bug:
Code:
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;

namespace CSEncryptDecrypt
{
   class Class1
   {
      //  Call this function to remove the key from memory after use for security
      [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
      public static extern bool ZeroMemory(IntPtr Destination, int Length);
      
      // Function to Generate a 64 bits Key.
      static string GenerateKey()
      {
         // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
         DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

         // Use the Automatically generated key for Encryption.
         return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
      }

      static void EncryptFile(string sInputFilename,
         string sOutputFilename,
         string sKey)
      {
         FileStream fsInput = new FileStream(sInputFilename,
            FileMode.Open,
            FileAccess.Read);

         FileStream fsEncrypted = new FileStream(sOutputFilename,
            FileMode.Create,
            FileAccess.Write);
         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
         ICryptoTransform desencrypt = DES.CreateEncryptor();
         CryptoStream cryptostream = new CryptoStream(fsEncrypted,
            desencrypt,
            CryptoStreamMode.Write);

         byte[] bytearrayinput = new byte[fsInput.Length];
         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
         cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
         cryptostream.Close();
         fsInput.Close();
         fsEncrypted.Close();
      }

      static void DecryptFile(string sInputFilename,
         string sOutputFilename,
         string sKey)
      {
         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
         //A 64 bit key and IV is required for this provider.
         //Set secret key For DES algorithm.
         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
         //Set initialization vector.
         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

         //Create a file stream to read the encrypted file back.
         FileStream fsread = new FileStream(sInputFilename,
            FileMode.Open,
            FileAccess.Read);
         //Create a DES decryptor from the DES instance.
         ICryptoTransform desdecrypt = DES.CreateDecryptor();
         //Create crypto stream set to read and do a
         //DES decryption transform on incoming bytes.
         CryptoStream cryptostreamDecr = new CryptoStream(fsread,
            desdecrypt,
            CryptoStreamMode.Read);
         //Print the contents of the decrypted file.
         StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
         fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
         fsDecrypted.Flush();
         fsDecrypted.Close();
      }

      static void Main()
      {
         // Must be 64 bits, 8 bytes.
         // Distribute this key to the user who will decrypt this file.
         string sSecretKey;
         
         // Get the Key for the file to Encrypt.
         sSecretKey = GenerateKey();

         // For additional security Pin the key.
         GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
         
         // Encrypt the file.       
         EncryptFile(@"C:\MyData.txt",
            @"C:\Encrypted.txt",
            sSecretKey);

         // Decrypt the file.
         DecryptFile(@"C:\Encrypted.txt",
            @"C:\Decrypted.txt",
            sSecretKey);

         // Remove the Key from memory.
         ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
         gch.Free();
      }
   }
}


Top
  Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 1:50 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Well, I've not done any .Net programming yet, but looking at the article, it's in the encryption part when they're reading the source file. Looks like they're setting the temporary array they're reading the file into to the file length - 1. Possibly due to thinking about starting from 0 vs 1. That sometimes gets me as well. Or thinking about excluding the EOF character (although that might be stripped automatically by the fsInput.Read).


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2008 5:05 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Winner!

Yeah, I read the article, and the little blocks where they describe the code was correctly Length-1 but the code at the end was wrong! Needless to say, I "saved time" by not re-writting and I just copied the full code.

Yeah, it didn't work. I was very WTF about it.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group