d28905 2016-04-25 09:56 采纳率: 33.3%
浏览 1979

C#上传到ftp文件加密问题

上传功能:private void Upload(string filename) //上传功能
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; //uri登录方式
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
// 缓冲区大小设置成2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
//打开一个文件流来读入上传的文件
FileStream fs = fileInf.OpenRead();

        try
        {

            // 把要上传的文件写入流


            Stream strm=reqFTP.GetRequestStream();



            //从文件流中读取数据,一次读2kb大小的数据 
            contentLen = fs.Read(buff, 0, buffLength);

            // Till Stream content ends
            while (contentLen != 0)
            {
                //把文件的内容从文件流写到FTP上传流中 
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            //关闭文件流和请求流 
            strm.Close();
            fs.Close();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message, "上传出错");
        }
    }

加密功能:
public static String Encrypt(String Key, String str)
{
byte[] bKey = Encoding.UTF8.GetBytes(Key.Substring(0, 8));
byte[] bIV = IV;
byte[] bStr = Encoding.UTF8.GetBytes(str);
try
{
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return string.Empty;
}
}
我调用加密函数显示cannot 'string' to 'System.IO.Stream',求大神指点

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮