如何从URL加密数据 [英] How to encrypt data from URL

查看:91
本文介绍了如何从URL加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





任何人都可以帮我如何加密来自URL的数据,我已经写了下面的代码并在main函数中调用加密文件但是我无法读取输入文件,而...



我尝试过:



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");

推荐答案

EncryptFile 中,您使用 FileStream 来阅读要加密的数据。通过一些修改,您可以使用来自Web响应的 Stream



c# - 从网址转换为流 - 堆栈溢出 [ ^ ]:

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[^]:
private static Stream GetStreamFromUrl(string url)
{
    byte[] imageData = null;

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

    return new MemoryStream(imageData);
}





您可以使用该方法从URL获取Stream,然后使用该方法代替您使用的FileStream之前。



You can use that method to get a Stream from an URL and then use that instead of the FileStream you used before.


您不能只从IP地址访问文件 - 您需要使用FTP访问它,并为您尝试访问的计算机提供有效的FTP凭据,或访问远程计算机上的共享文件夹,再次提供相应的凭据(这将适用于您显示的IP地址,因为它在同一个局域网上,但赢了;适用于互联网访问的计算机。)



从这开始: FtpWebRequest类(System.Net) [ ^ ] - 底部有一个全面的例子。
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.


这篇关于如何从URL加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆