Click here to Skip to main content
16,019,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

Lab9.3 Working on File Encryption - got this to work
Lab9.4 Working on File Decryption - got this to work

The first lines in the book look like this:

VB
// Read the command-line parameters
             //example c:\myfile\abc.txt The file has some data to be encrypted
              string inputFilePath = args[0];

              //example c:\myfile\abcEncrpt.txt This file will be used to save the encrypted format of the input file
              string OutputFilePath = args[1];

              //provide any password
              string filePassword = args[2];


When I tried to compile, I got this error:

Index was outside the bounds of the array.


I could have also changed it to something like this:

C#
string filePassword;
            Console.WriteLine("Enter a Password to decrypy the file");
            filePassword = Console.ReadLine();




So I changed the code to the following and got it to work:

C#
string inputFilePath = "crptTest.txt";

            string OutputFilePath = "crypTestEncrpted.txt";

            string filePassword = "Password";


To improved in this I could hyave done something like:

C#
string filePassword;
            Console.WriteLine("Enter a Password to decrypt the file");
            filePassword = Console.ReadLine();


So here's my question how should I have done it using the args that was defined:

static void Main(string[] args)


I found a cool example on MSDN, but did not work. And I think it is because a similar reason. here is the link:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes.aspx[^]

or just google Rfc2898DeriveBytes MSDN.


I feel as though I am missing something, even though I got both labs to work, and had fun modifying the code so that the password was not hard coded.

Any feedback on this would be greatly appreciated.

Thanks in advance.
Posted

Try putting a quote (") around your file names if they have space in them e.g:

Encrypt.exe "c:\input.ext" "d:\output.ext" "password"


Index out of bounds is probably to do with the args
 
Share this answer
 
Comments
censurl 1-Aug-12 13:16pm    
I came back to this a weeks ago, and by chance I gave it a go and got it working. If only I could have seen then the way I see it now. This answer would have been just what I needed.
Thanks
Mehdi Gholam 1-Aug-12 15:36pm    
:)
Sounds like there were not enough arguments given to use args[0], args[1] and args[2].

For this to work, you have to call the executable with at least 3 command line arguments.

You could implement a test prior to using the arguments. That test would check if enough arguments existed. If it failed, it would quit the program after giving an error message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900