【问题标题】:Encrypting large file with PGP (BouncyGPG) and sending it over SFTP (JSch) in Kotlin使用 PGP (BouncyGPG) 加密大文件并在 Kotlin 中通过 SFTP (JSch) 发送它
【发布时间】:2021-05-06 17:47:20
【问题描述】:

我一直在尝试使用 PGP 加密大文件(为此我使用 BouncyGPG)并使用 SFTP(使用 JSch)将其发送到远程服务器。我正在使用 PipedOutputStream 和 PipedInputStream 来避免在发送到服务器之前将最终加密文件保存在本地。我认为 BouncyGPG 的行为不端正因为如此。

谁能帮我解决这个问题?

这是我的代码:

    BouncyGPG.registerProvider()

    private val bufferSize: Int = 8 * 1024

    fun sendFileSFTP(myFile: MyFile {
        PipedInputStream().use { input ->
            GlobalScope.launch {
                PipedOutputStream(input).use { output ->
                    encrypt(myFile, output)
                }
            }
            sendSFTP(input, "mysftp-directory/${myFile.filename}")
            log.info("Finished Sending $myFile")
        }
    }

    fun encrypt(myFile: MyFile output: OutputStream) {
        log.info("ENCRYPTING - $myFile")
        val pubkey = "/home/myhome/server.gpg"
        val privkey = "/home/myhome/server.secret.gpg"
        val password = "password"
        val keyring = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withPassword(password))
        keyring.addPublicKey(FileReader(File(pubkey)).readText().toByteArray(charset("US-ASCII")))
        keyring.addSecretKey(FileReader(File(privkey)).readText().toByteArray(charset("US-ASCII")))


        BouncyGPG.encryptToStream()
            .withConfig(keyring)
            .withStrongAlgorithms()
            .toRecipients("contact@myserver.com")
            .andDoNotSign()
            .binaryOutput()
            .andWriteTo(output)

        val filepath = Paths.get(myFile.path, myFile.filename)
        BufferedInputStream(FileInputStream(filepath.toString())).use { input ->
            input.copyTo(output, bufferSize)
        }

        log.info("FINISHED ENCRYPTING - $myFile")
    }

    fun sendSFTP(inputStream: InputStream, dest: String) {
        log.info("Sending $dest")
        var jsch = JSch()
        jsch.setKnownHosts(properties.knownHosts)

        val jschSession: Session = jsch.getSession(properties.sFTPUsername, properties.sFTPHost, properties.sFTPPort)
        jschSession.setPassword(properties.sFTPPassword)
        jschSession.connect()
        val channel = jschSession.openChannel("sftp") as ChannelSftp
        channel.connect()

        channel.put(inputStream, dest)
    }

我假设 BouncyGPG 由于没有关闭而没有唱我的文件(我相信我在某处读到它只在输出关闭后对文件进行签名,但据我所知,kotlin 将关闭已经为我输出了。我错过了什么吗?

谢谢

【问题讨论】:

    标签: kotlin jsch gnupg


    【解决方案1】:

    在与这个问题讨论了一段时间后,我尝试生成了自己的 pgp 密钥(我使用的是客户给我们的测试密钥),你猜怎么着?成功了!

    显然他们有问题! ¯_(ツ)_/¯

    所以,我的代码正在运行!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      相关资源
      最近更新 更多