Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Any one can help me how to encrypt data from taking URL, i have written below code and calling encrypted file in main function but i unable read input file while ...

What I have tried:

Class AES
  {        
    public void EncryptFile(string inputFile, string  outputfile, string hoskeys)
       {
      try
      {
          //if(!File.Exists(outputfile))
          //    File.Create(outputfile);
          // Create the password key
        byte[]  saltValueBytes = Encoding.ASCII.GetBytes("This is my salt");
    Rfc2898DeriveBytes passwordKey = new Rfc2898DeriveBytes(hoskeys, saltValueBytes);
        // Create the algorithm and specify the key and IV
        RijndaelManaged alg = new RijndaelManaged();
        alg.Key = passwordKey.GetBytes(alg.KeySize/8);
        alg.IV = passwordKey.GetBytes(alg.BlockSize/8);
        // Read the unencrypted file into fileData
        FileStream inFile = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        byte[]  fileData = new byte[inFile.Length];
        inFile.Read(fileData, 0, (int)inFile.Length);
        // Create the ICryptoTransform and CryptoStream object
        ICryptoTransform encryptor = alg.CreateEncryptor();
        FileStream outFile = new FileStream( outputfile, FileMode.OpenOrCreate, FileAccess.Write);
        CryptoStream encryptStream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);        
        // Write the contents to the CryptoStream
        encryptStream.Write(fileData, 0, fileData.Length);
        // Close the file handles
        encryptStream.Close();
        inFile.Close();
        outFile.Close();
        } 
       catch(Exception ex)
        {
            //MessageBox.Show("Encryption failed!", "Error");
            Console.WriteLine("");
        }
    }

//calling in main function

  Encrypt_and_decrypt.AES e = new AES();

   e.EncryptFile("http://192.168.....","E:\\folder","hkeys");
Posted
Updated 12-Dec-20 3:20am
Comments
F-ES Sitecore 10-May-18 5:14am    
File stream is for accessing files on a file system, it doesn't understand http so you can't access files over http. If you want to read the file over http you'll need something like

https://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx

or

https://social.msdn.microsoft.com/Forums/en-US/b489fe1d-a237-48ac-b58a-f901e1b03bde/file-download-how-to-use-httpwebrequestrespone?forum=vssmartdevicesvbcs

In EncryptFile, you use a FileStream to read the data to encrypt. With some modifications, you can use a Stream that comes from a web response instead.

c# - Convert to Stream from a Url - Stack Overflow[^]:
C#
private static Stream GetStreamFromUrl(string url)
{
    byte[] imageData = null;

    using (var wc = new System.Net.WebClient())
        imageData = wc.DownloadData(url);

    return new MemoryStream(imageData);
}


You can use that method to get a Stream from an URL and then use that instead of the FileStream you used before.
 
Share this answer
 
You can't just access a file from an IP address - you would need to use FTP to access it, and provide valid FTP credentials for the machine you are trying to access, or access a shared folder on a remote machine, again supplying the appropriate credentials (which would work for the IP address you show as it's on the same LAN, but won;t work for internet accessed machines.)

Start with this: FtpWebRequest Class (System.Net)[^] - there is a comprehensive example at the bottom.
 
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