我正在尝试使用Didisoft pgp解密,并遇到一些问题。我可以很好地生成私钥和公钥,也可以使用SignAndEncryptFile函数加密文件,并返回.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
问题是这样的。
PGPLib.SignAndEncryptFile(dataFIle, signingPrivateKey, signingPrivateKeyPassword, encryptingPublicKey, desitinationFile)
PGPLib.DecryptAndVerifyFile(dataFIle, decryptingPrivateKey, decryptingPrivateKeyPassword, signatureVerifyingPublicKey, desitinationFile)
你在这两种情况下都使用了相同的公钥和私钥 - 但是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)