Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is my first post. I am trying to do file encryption and decryption using DES algorithm in ASP.Net with C#. I have successfully encrypted the file and uploaded it. When I download, the encrypted file will be available. Now, I need to decrypt the file by selecting the file through upload control. Can any one please help me?

The encryption code is below:
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);

        System.IO.Stream myStream;
        Int32 fileLen;
        StringBuilder displayString = new StringBuilder();
        fileLen = FileUpload1.PostedFile.ContentLength;

        Byte[] Input = new Byte[fileLen];

        // Initialize the stream to read the uploaded file.
        myStream = FileUpload1.FileContent;

        // Read the file into the byte array.
        myStream.Read(Input, 0, fileLen);

        // Copy the byte array to a string.
        for (int loop1 = 0; loop1 < fileLen; loop1++)
        {
            displayString.Append(Input[loop1].ToString());
        }

        //TextBox1.Text = displayString.ToString();
        bytes = ASCIIEncoding.ASCII.GetBytes(dktxt.Text);
        bytes1 = ASCIIEncoding.ASCII.GetBytes(ck);
        
        string originalString = displayString.ToString();

        // DES algorithm to encrypt the given file
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes1), CryptoStreamMode.Write);
        StreamWriter writer = new StreamWriter(cryptoStream);
        writer.Write(originalString);
        writer.Flush();
        cryptoStream.FlushFinalBlock();
        writer.Flush();

        // store the encrypted data of a file
        cryptedString = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
Posted
Updated 24-Apr-13 5:32am
v2

Following link can solve your problem please see that

[
See this link[^]
]
 
Share this answer
 
Comments
Member 9985378 24-Apr-13 11:48am    
i have tried that too but it needs to store and get the files from default location. i need to get the file from file upload control, then decrypt the file with des and finally store it to the downloads location
i tried that too. but it needs to store and get the files from default location.
 
Share this answer
 
Comments
Richard C Bishop 24-Apr-13 11:33am    
Be careful not to post questions or comments as solutions to your own thread.

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