Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用PGP加密SFTP流_C#_Sftp_Pgp_Ssh.net - Fatal编程技术网

C# 如何使用PGP加密SFTP流

C# 如何使用PGP加密SFTP流,c#,sftp,pgp,ssh.net,C#,Sftp,Pgp,Ssh.net,我正在使用PgpCore&SSH.NET对流进行加密,并通过带有加密流的sftp在远程服务器上创建一个文件 我有以下尝试,但是,虽然我可以成功列出远程目录中的文件,但我无法写入远程文件。我在pgp.EncryptStream行得到错误没有这样的文件 using (var client = new SftpClient(server, 22, username, password)) { client.Connect(); Console.WriteLine("Conne

我正在使用PgpCore&SSH.NET对流进行加密,并通过带有加密流的sftp在远程服务器上创建一个文件

我有以下尝试,但是,虽然我可以成功列出远程目录中的文件,但我无法写入远程文件。我在
pgp.EncryptStream
行得到错误
没有这样的文件

using (var client = new SftpClient(server, 22, username, password))
{
    client.Connect();
    Console.WriteLine("Connected to client.");
    var inputStream = new MemoryStream();
    var fileContents = Encoding.UTF8.GetBytes(sb.ToString());
    inputStream.Write(fileContents, 0, fileContents.Length);
    inputStream.Position = 0;

    var outputStream = new MemoryStream();

    var files = client.ListDirectory("/upload/");

    foreach (var file in files)
    {
        string remoteFileName = file.Name;

        Console.WriteLine(remoteFileName);
    }

    foreach (var file in files)
    {
        string remoteFileName = file.FullName;

        Console.WriteLine(remoteFileName);
    }

    using (PGP pgp = new PGP())
    {
        Console.WriteLine("Doing PGP...");
        using (Stream outputFileStream = client.Create("/upload/test.txt.pgp"))
        using (Stream publicKeyStream = new FileStream(@".\PublicKey.asc", FileMode.Open))
        {
            Console.WriteLine("Encrypting Stream...");
            pgp.EncryptStream(inputStream, outputFileStream, publicKeyStream);
            Console.WriteLine("Stream Encrypted.");

            //Console.WriteLine("Uploading File.");
            //client.UploadFile(outputFileStream, string.Format("/upload/{0}", "test.txt.pgp"));
            //Console.WriteLine("Upload Complete.");
        }
    }
}
有什么办法可以做到这一点吗

异常堆栈跟踪:

   at Renci.SshNet.Sftp.SftpSession.RequestWrite(Byte[] handle, UInt64 serverOffset, Byte[] data, Int32 offset, Int32 length, AutoResetEvent wait, Action`1 writeCompleted)
   at Renci.SshNet.Sftp.SftpFileStream.FlushWriteBuffer()
   at Renci.SshNet.Sftp.SftpFileStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Project.Web.Areas.Pims.Controllers.Eod.EodController.EncryptStream(String fileName, StringBuilder sb) in C:\Repos\Solution\Project.Web\Areas\Pims\Controllers\Eod\EodController.cs:line 144
编辑: 对于那些可能认为它有用的人,我设法使用
client.OpenWrite
而不是
client.Create
上传了加密文件,如下所示。我还添加了代码,用我的pgp私钥和密码对加密文件进行签名:

工作解决方案:

using (PGP pgp = new PGP())
{
    using (Stream outputFileStream = client.OpenWrite("/upload/test.txt.pgp"))
    using (Stream publicKeyStream = new FileStream(@".\MSPublicKey.asc", FileMode.Open))
    using (Stream privateKeyStream = new FileStream(@".\PrivateKey.asc", FileMode.Open))
    {
        pgp.EncryptStreamAndSign(inputStream, outputFileStream, publicKeyStream, privateKeyStream, pgpPasspharse, true, true);
    }
}

您可以将文件加密到本地pc,然后将文件上载到远程服务器

   string path= "c:\yourfolder\test.txt.pgp";

   using (Stream outputFileStream = client.Create(path))
    using (Stream publicKeyStream = new FileStream(@".\PublicKey.asc", FileMode.Open))
    {
        Console.WriteLine("Encrypting Stream...");
        pgp.EncryptStream(inputStream, outputFileStream, publicKeyStream);
        uploadfile(path, "/upload/test.txt.pgp");
        Console.WriteLine("Stream Encrypted.");

        
    }
    static void uploadFile(string sourcepath, string destination)
    {
        FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read );
        byte[] bytes= new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        MemoryStream stream = new MemoryStream(bytes);          
        IAsyncResult result = client.BeginUploadFile(ms, destination); 
        //you can also use client.UploadFile()

       
    }
下面的方法可以将加密文件上载到远程服务器

   string path= "c:\yourfolder\test.txt.pgp";

   using (Stream outputFileStream = client.Create(path))
    using (Stream publicKeyStream = new FileStream(@".\PublicKey.asc", FileMode.Open))
    {
        Console.WriteLine("Encrypting Stream...");
        pgp.EncryptStream(inputStream, outputFileStream, publicKeyStream);
        uploadfile(path, "/upload/test.txt.pgp");
        Console.WriteLine("Stream Encrypted.");

        
    }
    static void uploadFile(string sourcepath, string destination)
    {
        FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read );
        byte[] bytes= new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        MemoryStream stream = new MemoryStream(bytes);          
        IAsyncResult result = client.BeginUploadFile(ms, destination); 
        //you can also use client.UploadFile()

       
    }

谢谢你的回答。我试图避免将文件保存到本地驱动器,而是纯粹在内存中创建一个文件,然后加密并发送到服务器。您知道这样做的方法吗?请共享代码pgp.EncryptStream()方法,或者您可以修改它以返回加密流,而不是将其写入文件。然后,您可以使用uploadFile()中的流。EncryptStream是来自PgpCore()的一种方法,我正在使用它来加密数据。您可以共享我的PgpCore dll吗???@MartinPrikryl我已经编辑过,以包括异常堆栈跟踪。上载文件夹确实存在,并且在我列出目录并循环文件时确认了这一点。此代码实际上创建了文件
test.txt.pgp
,但其中没有数据,并且在
pgp.EncryptStream
上发生错误,即上载成功。我已经设法使用
clinet.OpenWrite
(如上面的编辑所示)上传了加密文件。