如何使代码更安全连接到FTP服务器?

时间:2017-09-26 07:08:45

标签: c# ftp

目前,我正在使用控制台应用程序将文件上传到FTP服务器。就目前而言,我想根据我的知识,我觉得我的编程不够安全,可能会被黑客入侵。这完全是我对进一步过程的编码

这是配置类

class configuration
{
    public string consolePath { get; set; }
    public string remoteFtpPath { get; set; }
    public string localDestinationPath { get; set; }
    public string userName { get; set; }
    public string password { get; set; }

    public configuration()
    {
        //Set information from App.config to class
        consolePath = ConfigurationManager.AppSettings.Get("consolePath").ToString();
        remoteFtpPath = ConfigurationManager.AppSettings.Get("remoteFtpPath").ToString();
        localDestinationPath = ConfigurationManager.AppSettings.Get("localDestinationPath").ToString();
        userName = "geek";
        password = "geek";
    }
}

这是主要过程     static void Main(string [] args)         {

        configuration config = new configuration();

        // Hide the console gui
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = config.consolePath;
        start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

        // Loop each file in specific folder
        string[] fileArray = Directory.GetFiles(config.localDestinationPath, "*.txt");

        foreach (string file in fileArray)
        {
            config.localDestinationPath = file;
            uploadEmployeeAttendance(config);
        }

    }

    static void uploadEmployeeAttendance(configuration config)
    {    
            // Get the local file name
            String filename = Path.GetFileName(config.localDestinationPath);

            // Open a request using the full URI
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(config.remoteFtpPath + "/" + filename);

            // Configure the connection request
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(config.userName, config.password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = true;

            // Create a stream from the file
            FileStream stream = File.OpenRead(config.localDestinationPath);
            byte[] buffer = new byte[stream.Length];

            // Read the file into the a local stream
            stream.Read(buffer, 0, buffer.Length);

            // Close the local stream
            stream.Close();

            // Create a stream to the FTP server
            Stream reqStream = request.GetRequestStream();

            // Write the local stream to the FTP stream
            // 2 bytes at a time
            int offset = 0;
            int chunk = (buffer.Length > 2048) ? 2048 : buffer.Length;
            while (offset < buffer.Length)
            {
                reqStream.Write(buffer, offset, chunk);
                offset += chunk;
                chunk = (buffer.Length - offset < chunk) ? (buffer.Length - offset) : chunk;
            }

            // Close the stream to the FTP server
            reqStream.Close();
    }

我想知道是否可以在此程序中对用户名和密码进行加密和解密部分

0 个答案:

没有答案