【问题标题】:Didisoft PGP Decrypt and VerifyDidisoft PGP 解密和验证
【发布时间】:2020-06-15 13:42:35
【问题描述】:

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

仅当我尝试使用公钥和私钥对其进行加密和解密时才会发生这种情况。我之前设法进行加密和解密,但它只使用私钥。

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

【问题讨论】:

    标签: vb.net exception encryption pgp


    【解决方案1】:

    问题来了:

    PGPLib.SignAndEncryptFile(dataFIle, signingPrivateKey, signingPrivateKeyPassword, encryptingPublicKey, desitinationFile)
    
    PGPLib.DecryptAndVerifyFile(dataFIle, decryptingPrivateKey, decryptingPrivateKeyPassword, signatureVerifyingPublicKey, desitinationFile)
    

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

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

    本质上这是你应该做的:

    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 必须是签名验证公钥来自

    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 为用于加密的密钥对应的私钥(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)
    

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 2012-04-29
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多