【问题标题】:Any way to PGP encrypt data stream and save it directly to the FTP without having to save it locally?有什么方法可以 PGP 加密数据流并将其直接保存到 FTP 而无需在本地保存?
【发布时间】:2013-02-16 03:31:22
【问题描述】:

我必须通过 sFTP 将 asc 格式的 PGP 加密文件发送到 FTP 文件夹。有没有办法 PGP 加密流(这是 CSV 格式的流)并将其推送到 sFTP而无需将其保存在本地机器上

下面是我用于PGP加密的函数,它以文件名为参数:

Private Function PGPEncrypt(ByVal FileName As String) As Boolean
        Dim errorHappened As Boolean = False
        Dim encodedFileName As String = String.Format("{0}{1}", FileName, ".asc")
        Dim pgpRecipient As String = System.Configuration.ConfigurationManager.AppSettings.Get("PgpRecipient")
        Dim psi As System.Diagnostics.ProcessStartInfo
        Dim ErrorResult As String = ""
        Dim Result As String = ""
        Try
            psi = New ProcessStartInfo(System.Configuration.ConfigurationManager.AppSettings.Get("PgpPath"))
            psi.CreateNoWindow = True
            psi.UseShellExecute = False
            psi.Arguments = String.Format(" --armor --yes --recipient ""{0}"" --output ""{1}"" --encrypt ""{2}""", _
                                          pgpRecipient, encodedFileName, FileName)
            psi.RedirectStandardInput = True
            psi.RedirectStandardOutput = True
            psi.RedirectStandardError = True
            ProcessPGP = System.Diagnostics.Process.Start(psi)
            ProcessPGP.StandardInput.Write(m_Passphrase)
            Result = ProcessPGP.StandardError.ReadToEnd()
            ProcessPGP.WaitForExit()
        Catch ex As Exception
            errorHappened = True
            Dim ReadError As New StringBuilder()
            ReadError.Append(vbCrLf & "Error Detail:")
            ReadError.Append(vbCrLf & ex.ToString())
            OurEventLog.WriteEntry(ReadError.ToString(), EventLogEntryType.Error)
        End Try
        Return errorHappened
    End Function

同样,主要要求是将 PGP 加密文件保存在本地,然后发送到 FTP,但必须通过流创建 PGP 加密文件。

有什么想法吗?

更新:

FTP 代码:

 ftp.ConnectMode = FTPConnectMode.PASV
                ftp.RemoteHost = Csla.ConfigurationManager.AppSettings("FTPRemoteHost")
                If _blnDiagnostics Then DiagnosticsManager.Publish("STREAM_TO_FTP: CONNECT TO FTP", DiagnosticsManager.EntryType.SuccessAudit)
                ftp.Connect()
                ftp.Login(strUser, strPassword)
                ftp.TransferType = FTPTransferType.BINARY 'ASCII
                ftp.Put(OUTBOUNDMESSAGE, pFilename)

                ftp.Quit()
                ftp = Nothing

OUTBOUNDMESSAGE 是 System.IO.Stream。

【问题讨论】:

  • 这在我看来不像 C#...?

标签: vb.net encryption sftp pgp


【解决方案1】:

您可以将- 传递给--output,而不是将文件名作为命令行选项,这会直接将输出公开给控制台。您可以直接流式传输这些数据。

您的 Arguments 属性将如下所示:

psi.Arguments = String.Format(" --armor --yes --recipient ""{0}"" --output - --encrypt ""{1}""", _
                                      pgpRecipient, FileName)

当您执行该过程时,ProcessPGP.StandardOutput 应该会产生您需要的流。

【讨论】:

  • FTP 组件呢?只需将此加密数据流发送到 FTP 并将其另存为 asc 即可?
  • 由于您没有包含任何有关如何通过 FTP 发送数据的代码,因此我无法确定您会做什么。如果您必须通过 FTP 流式传输现有文件,那么只需使用输出流即可。如果它是一个将文件作为参数传入并自行进行流式处理的库,那么您需要找到一个支持以某种形式获取Stream 的 FTP 库。
  • 为 FTP 添加了代码。我使用以流为参数的 ftp.Put。所以,基本上你的意思是 PGP 加密流并将其作为 asc 保存在 ftp 上?
  • 您可以使用ProcessPGP.StandardOutput.BaseStream 属性代替您添加的代码中的OUTBOUNDMESSAGE 变量。
猜你喜欢
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2022-11-15
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多