当前位置: 首页 > 知识库问答 >
问题:

用VB.NET实现OpenPGP的反弹

拓拔坚
2023-03-14

我主要是为任何其他需要它的人张贴这一点,因为似乎有一个很大的资源缺乏在互联网上为这一点。我找到的样本充其量似乎是蹒跚在一起的,对我来说从来都不起作用。如果你看到或发现这门课有什么问题,我很想知道。请注意,针对我的特定实现,这个类略作了修改,但在Bouncy Castle.NET源代码的1.7版中应该可以很好地工作。

Imports System.IO
Imports Org.BouncyCastle.Bcpg.OpenPgp

Public Class PgpDecrypt
    Private _mPublicKeyPath As String = "C:\MyPgpKeys\MyKey - Public.asc"
    Private _mPrivateKeyPath As String = "C:\MyPgpKeys\MyKey - Private.asc"
    Private _mPassPhrase As String = "<passwoid>"

    Public Function Decrypt(ByVal srcPath As String, ByVal dstPath As String) As Boolean
        Dim stEnc As Stream = PgpUtilities.GetDecoderStream(File.OpenRead(srcPath))
        Dim pof As New PgpObjectFactory(stEnc)
        Dim pked As PgpPublicKeyEncryptedData = Nothing

        Dim keyPrivate As PgpPrivateKey = Nothing
        For Each pked In GetEncryptedDataList(pof).GetEncryptedDataObjects
            keyPrivate = ReadPrivateKey(pked.KeyId)
            If Not keyPrivate Is Nothing Then Exit For
        Next
        If keyPrivate Is Nothing Then Return False

        Dim stDec As Stream = pked.GetDataStream(keyPrivate)
        pof = New PgpObjectFactory(stDec)
        Dim o As PgpObject = pof.NextPgpObject
        If TypeOf o Is PgpCompressedData Then
            pof = New PgpObjectFactory(DirectCast(o, PgpCompressedData).GetDataStream)
            o = pof.NextPgpObject
        End If
        While Not TypeOf o Is PgpLiteralData
            o = pof.NextPgpObject
            If o Is Nothing Then Return False
        End While
        Dim ld As PgpLiteralData = DirectCast(o, PgpLiteralData)
        Dim stOut As Stream = File.Create(dstPath)
        Dim stUnc As Stream = ld.GetInputStream
        Org.BouncyCastle.OpenPgp.Utilities.IO.Streams.PipeAll(stUnc, stOut)
        stOut.Close()

        Return True
    End Function

    Private Function GetEncryptedDataList(ByVal pof As PgpObjectFactory) As PgpEncryptedDataList
        Dim o As PgpObject = Nothing
        While Not TypeOf o Is PgpEncryptedDataList
            o = pof.NextPgpObject
            If o Is Nothing Then Return Nothing
        End While
        Return DirectCast(o, PgpEncryptedDataList)
    End Function

    Private Function ReadPublicKey(Optional ByVal useEmbedded As Boolean = False) As PgpPublicKey
        If useEmbedded Then
            Using st As Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(GetAssemblyName() & ".MyKey - Public.asc")
                Dim bundle As PgpSecretKeyRingBundle = New PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                For Each ring As PgpPublicKeyRing In bundle.GetKeyRings
                    For Each key As PgpPublicKey In ring.GetPublicKeys
                        If key.IsEncryptionKey Then Return key
                    Next
                Next
            End Using
        Else
            Using st As Stream = File.OpenRead(_mPublicKeyPath)
                Dim bundle As PgpPublicKeyRingBundle = New PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                For Each ring As PgpPublicKeyRing In bundle.GetKeyRings
                    For Each key As PgpPublicKey In ring.GetPublicKeys
                        If key.IsEncryptionKey Then Return key
                    Next
                Next
            End Using
        End If

        Return Nothing
    End Function

    Private Function ReadSecretKey(Optional ByVal useEmbedded As Boolean = False) As PgpSecretKey
        If useEmbedded Then
            Using st As Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(GetAssemblyName() & ".MyKey - Private.asc")
                Dim bundle As PgpSecretKeyRingBundle = New PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                For Each ring As PgpSecretKeyRing In bundle.GetKeyRings
                    For Each key As PgpSecretKey In ring.GetSecretKeys
                        If key.IsSigningKey Then Return key
                    Next
                Next
            End Using
        Else
            Using st As Stream = File.OpenRead(_mPrivateKeyPath)
                Dim bundle As PgpSecretKeyRingBundle = New PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                For Each ring As PgpSecretKeyRing In bundle.GetKeyRings
                    For Each key As PgpSecretKey In ring.GetSecretKeys
                        If key.IsSigningKey Then Return key
                    Next
                Next
            End Using
        End If

        Return Nothing
    End Function

    Private Function ReadPrivateKey(ByVal keyId As Long, Optional ByVal useEmbedded As Boolean = False) As PgpPrivateKey
        If useEmbedded Then
            Using st As Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(GetAssemblyName() & ".MyKey - Private.asc")
                Dim bundle As PgpSecretKeyRingBundle = New PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                Dim key As PgpSecretKey = bundle.GetSecretKey(keyId)
                If key Is Nothing Then Return Nothing
                Return key.ExtractPrivateKey(_mPassPhrase)
            End Using
        Else
            Using st As Stream = File.OpenRead(_mPrivateKeyPath)
                Dim bundle As PgpSecretKeyRingBundle = New PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(st))
                Dim key As PgpSecretKey = bundle.GetSecretKey(keyId)
                If key Is Nothing Then Return Nothing
                Return key.ExtractPrivateKey(_mPassPhrase)
            End Using
        End If
    End Function

    Private Function GetAssemblyName() As String
        Dim names() As String = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceNames
        If names.Length > 0 Then
            Return names(0).Split(".").First
        Else
            Return String.Empty
        End If
    End Function
End Class

共有1个答案

赵骏奇
2023-03-14

太棒了!非常感谢你把这个贴出来--它的效果非常好。一个变化是org.bouncycastle.openpgp.utilities.io.streams.PipeAll(stUnc,stOut)需要替换为org.bouncycastle.utilities.io.streams.PipeAll(stUnc,stOut)

 类似资料:
  • 问题内容: 我有以下JSON字符串要反序列化: 我正在使用DataContractJsonSerializer方法。 它由项目数组组成,我找不到使用VB.Net可以反序列化此结构的示例。我具有以下Application类来存储此信息: 问题答案: 我建议你使用过。原因如下: 更快 比简单的序列化需要更多的代码。 您无需将and 属性与 使用此数据类 并使用它反序列化您的: 如果仍要使用,则可以使用

  • PGP 来保护电子邮件和文件存储区。它提供一种对信息进行数字签名和加密的方法。该工作组正在将PGP集成到Internet邮件中。

  • Commons OpenPGP 项目的目的是提供一个统一的简单的应用接口来验证 OpenPGP 的签名。 示例代码: verifier = new BouncyCastleOpenPgpSignatureVerifier(); verifier.verifyDetachedSignature( getClass().getResourceAsStream( "/test-input" ),

  • ObjectivePGP 是 iOS 和 OS X 的 OpenPGP 实现。

  • 本文向大家介绍C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance),包括了C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)的使用技巧和注意事项,需要的朋友参考一下 从 <<Windows Forms 2.0 Programming, 2nd Edition>>   -  Single-Instance Applicat

  • OpenGPG 套件 OpenPGP 是一个加密和签名的标准。尽管 最近 有一些 关于 它的 争议,但他和 Yubikey 结合使用并没有什么问题。