HOWTO PGP加密字节数组

时间:2017-02-08 18:20:54

标签: .net encryption pgp

我有这个PGP加密样本,它从文件读取并加密而没有问题。

我正在尝试将代码转换为从字节数组而不是从文件加密。

原始示例代码和我的转换尝试如下。

我转换代码的输出错误。

请问我错过了什么?

示例代码 - 从文件读取并使用PGP进行加密

       installnr readdate   readings
    1    002345 2014-08-17 {0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,2,0}
    2    002345 2014-08-18 {0,0,0,0,0,0,4,1,0,0,0,0,1,1,1,1,0,1,1,1,1,1,0,1}
    3    002345 2014-08-19 {0,1,2,1,0,1,1,1,2,0,1,0,1,0,1,0,1,0,1,0,2,1,1,0}
    4    013459 2014-08-17 {1,0,0,1,0,1,1,1,1,0,1,0,1,0,1,0,0,1,3,1,0,0,1,1}
    5    127465 2014-08-19 {0,1,0,0,1,0,1,1,1,0,0,1,2,0,1,0,0,1,1,0,0,1,1,1}

我转换的代码 - PGP加密字节数组

Public Sub EncryptAndSign(outputStream As Stream, unencryptedFileInfo As FileInfo)
    Using encryptedOut As Stream = ChainEncryptedOut(outputStream)
        Using compressedOut As Stream = ChainCompressedOut(encryptedOut)
            Dim signatureGenerator As PgpSignatureGenerator = InitSignatureGenerator(compressedOut)
            Using literalOut As Stream = ChainLiteralOut(compressedOut, unencryptedFileInfo)
                Using inputFile As FileStream = unencryptedFileInfo.OpenRead()
                    WriteOutputAndSign(compressedOut, literalOut, inputFile, signatureGenerator)
                End Using
            End Using
        End Using
    End Using
End Sub

Private Shared Function ChainLiteralOut(compressedOut As Stream, file As FileInfo) As Stream    
    Dim pgpLiteralDataGenerator As New PgpLiteralDataGenerator()

    Return pgpLiteralDataGenerator.Open(compressedOut, PgpLiteralData.Binary, file)
End Function

Private Shared Sub WriteOutputAndSign(compressedOut As Stream, literalOut As Stream, inputFile As FileStream, signatureGenerator As PgpSignatureGenerator)
    Dim length As Integer = 0

    Dim buf(BufferSize - 1) As Byte

    length = inputFile.Read(buf, 0, buf.Length)
    Do While length > 0
        literalOut.Write(buf, 0, length)

        signatureGenerator.Update(buf, 0, length)

        length = inputFile.Read(buf, 0, buf.Length)
    Loop

    signatureGenerator.Generate().Encode(compressedOut)
End Sub

0 个答案:

没有答案