滴滴PGP解密验证

Didisoft PGP Decrypt and Verify

我正在尝试使用 Didisoft pgp 进行解密,并且 运行 遇到了一些问题。我可以很好地生成私有密钥和 public 密钥。我还可以使用 SignAndEncryptFile 函数加密文件,它 returns .pgp 文件就好了。问题是当我尝试使用 DecryptAndVerifyFile 函数时。 returns异常私钥错误,我检查了很多次,我的public和私钥和我用来加密文件的是一样的,我用的密码都是一样,因为我只是在测试它。我做错了什么? :(

只有当我尝试使用public密钥和私钥对其进行加密和解密时才会出现这种情况。我之前设法进行了加密和解密,但它只使用私钥。

Public Function GetValue(ByVal FileData As String, ByVal email As String, ByVal password As String) As String
    Dim idKey As String = Guid.NewGuid().ToString()
    Dim ks As KeyStore = New KeyStore()
    Dim keySize As Integer = 1024
    'Dim password As String = "12345"

    Dim FilePathKey As String
    Dim FilePathPublicKey As String
    Dim keys As KeyPairInformation() = ks.GetKeys()

    ' Generate DH/DSS OpenPGP key
    ks.GenerateElgamalKeyPair(keySize, email, password)
    'idKey = ks(0).KeyId
    FilePathKey = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\", "Coba\" + idKey + ".key")
    ks.ExportPrivateKey(FilePathKey, ks(0).KeyId)

    Dim sql As New MySqlConnection()
    Dim adapter As New MySqlDataAdapter
    sql.ConnectionString = "server=localhost;userid=root;database=test"
    Dim query As String = "Select public_key from public_key LIMIT 1"
    Dim cmd As MySqlCommand

    Dim dt As New DataSet()
    cmd = New MySqlCommand(query, sql)
    sql.Open()

    adapter = New MySqlDataAdapter(cmd)
    adapter.Fill(dt)

    sql.Close()
    FilePathPublicKey = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\PublicKey\", dt.Tables(0).Rows(0).Item(0).ToString + ".key")

    If File.Exists(FileData) Then
        'Encrypt Data
        Dim pgp As New PGPLib()
        Dim idData As String = Guid.NewGuid().ToString()
        Dim asciiArmor As Boolean = True
        Dim withIntegrityCheck As Boolean = True
        Dim encryptedOutputFile As String = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\Coba\", idData + ".pgp")
        'Dim encryptedOutputFile As String = Path.Combine(Directory.GetCurrentDirectory(), "Tes\" + idData + ".pgp")
        'C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\Data\
        Dim encryptedOutputFileTes As New FileInfo(encryptedOutputFile)

        cmd = New MySqlCommand("INSERT INTO `tes`(`tesid`) VALUES ('" + idKey + "')", sql)
        sql.Open()
        cmd.ExecuteNonQuery()
        sql.Close()

        pgp.SignAndEncryptFile(FileData, FilePathKey, password, FilePathPublicKey, encryptedOutputFile, asciiArmor, withIntegrityCheck)
        'pgp.SignAndEncryptFile(FileData, FilePathKey, password, FilePathPublicKey, encryptedOutputFile, asciiArmor)
        'pgp.EncryptFilePBE(FileData, FilePathKey, password, encryptedOutputFile, asciiArmor, withIntegrityCheck)

        Dim FullFile As New FullFile(idData + ".pgp", My.Computer.FileSystem.ReadAllText(encryptedOutputFile))

        cmd = New MySqlCommand("INSERT INTO `enkripsi_data`(`data`, `key`) VALUES ('" + idData + "','" + idKey + "')", sql)
        sql.Open()
        cmd.ExecuteNonQuery()
        sql.Close()
        Dim serializer As New JavaScriptSerializer
        serializer.MaxJsonLength = Int32.MaxValue
        Dim fullFileJSON = serializer.Serialize(FullFile)

        Return fullFileJSON
    Else
        'File Tidak ada
        Return "0"
    End If
    'Dim FilePath = HttpContext.Current.Server.MapPath("~/" + idKey + ".asc")
End Function

Public Function GetValue(ByVal FileData As String, ByVal password As String) As String
    Dim sql As New MySqlConnection()
    Dim adapter As New MySqlDataAdapter
    Dim FileName As String
    FileName = Path.GetFileNameWithoutExtension(FileData)
    Dim pgp As New PGPLib()

    sql.ConnectionString = "server=localhost;userid=root;database=test"

    Dim dt As New DataTable()
    Dim cmd As New MySqlCommand("SELECT `key` FROM `enkripsi_data` WHERE `data`='" + FileName + "'", sql)
    sql.Open()

    adapter = New MySqlDataAdapter(cmd)
    adapter.Fill(dt)
    sql.Close()
    Dim KeyName As String
    KeyName = dt.Rows(0)(0).ToString

    Dim FilePathKey As String
    FilePathKey = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\", "Coba\" + KeyName + ".key")

    Dim decryptedOutputFile As String = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\", "Coba\Decrypted" + DateTime.Now.ToString(" dd-MM-yyyy HH.mm.ss") + ".bat")
    Dim query As String = "Select public_key from public_key LIMIT 1"
    Dim ds As New DataSet
    cmd = New MySqlCommand(query, sql)
    sql.Open()

    adapter = New MySqlDataAdapter(cmd)
    adapter.Fill(ds)

    sql.Close()
    Dim FilePathPublicKey As New String(Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\PublicKey\", ds.Tables(0).Rows(0).Item(0).ToString + ".key"))
    If File.Exists(FileData) Then
        Dim originalFileName As SignatureCheckResult
        Try
            'pgp.DecryptAndVerifyFile(FileData, FilePathKey, password, FilePathPublicKey, decryptedOutputFile)
            'originalFileName = pgp.DecryptFile(FileData, FilePathKey, password, decryptedOutputFile)
            originalFileName = pgp.DecryptAndVerifyFile(FileData, FilePathKey, password, FilePathPublicKey, decryptedOutputFile)
            '==================================================

            Dim fullFile As New FullFile(originalFileName, My.Computer.FileSystem.ReadAllText(decryptedOutputFile))

            Dim serializer As New JavaScriptSerializer
            serializer.MaxJsonLength = Int32.MaxValue
            Dim fullFileJSON = serializer.Serialize(fullFile)


            Return fullFileJSON
        Catch e As PGPException
            If TypeOf e Is NonPGPDataException Then
                Return "The input file is not an OpenPGP archive or is corrupted"
            ElseIf TypeOf e Is WrongPublicKeyException Then
                Return "The supplied public key is not an OpenPGP public key or is corrupted"
            ElseIf TypeOf e Is WrongPrivateKeyException Then
                ' The message cannot be decrypted with this private key 
                ' or the supplied private key is not an OpenPGP private key or is corrupted
                Return e.Message
            ElseIf TypeOf e Is WrongPasswordException Then
                Return "The password for the private key is incorrect"
            ElseIf TypeOf e Is FileIsPBEEncryptedException Then
                Return "The input file is password encrypted."
                Return "You have to use DecryptAndVeifyFilePBE or DecryptAndVerifyStreamPBE"
            ElseIf TypeOf e Is IntegrityCheckException Then
                Return "The encrypted data is corrupted"
            Else
                Return e.Message
            End If
        End Try

    Else
        'File gak ada
        Return "0"
    End If
End Function

这是问题所在:

PGPLib.SignAndEncryptFile(dataFIle, signingPrivateKey, signingPrivateKeyPassword, encryptingPublicKey, desitinationFile)

PGPLib.DecryptAndVerifyFile(dataFIle, decryptingPrivateKey, decryptingPrivateKeyPassword, signatureVerifyingPublicKey, desitinationFile)

您在两种情况下都使用相同的 publicKey 和相同的私钥 - 但 PGP 加密(以及实现它的库)期望在解密时使用与在中使用的加密密钥相对应的私钥签名并加密,

验证密钥为SignAndEncrypt中使用的私钥对应的public密钥。

本质上这是你应该做的:

Dim FilePathPublicKey As New String(Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\PublicKey\", ds.Tables(0).Rows(0).Item(0).ToString + ".key"))

-> FilePathPublicKey 必须是验证来自

的public 密钥的签名
ks.GenerateElgamalKeyPair(keySize, email, password)

...

FilePathKey = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\", "Coba\" + KeyName + ".key")

-> FilePathKey mys是加密用的key对应的私钥(FilePathPublicKey私钥 = Path.Combine("C:\Users\user\Desktop\Kuliah\Semester 8\Project\Tes\TES\WindowsApp1\bin\Debug\PublicKey\", dt.Tables( 0).Rows(0).Item(0).ToString + ".key")

originalFileName = pgp.DecryptAndVerifyFile(FileData, FilePathKey, password, FilePathPublicKey, decryptedOutputFile)