Click here to Skip to main content
13,506,186 members
Click here to Skip to main content

Tagged as


10 bookmarked
Posted 17 Dec 2012

Resigning a signed assembly

, 18 Dec 2012
In this project we learn how to resign a signed assembly with another one of your choice
´╗┐Imports System.Reflection
Imports System.IO
Imports System.Runtime.InteropServices

Public Class ResignClass

#Region " ******    Fields              "

    Private _bytAssembly As Byte()
    Private _bytAssemblyPublicKey As Byte()

    Private _bytSNKFile As Byte()
    Private _strAssemblyPath As String

    Private _strNewStrongKeyName As String
    Private csRandom As Random = New Random(Environment.TickCount)

#End Region

#Region " ******    Constructor         "

    Public Sub New(ByVal assembly As String, ByVal snk As Byte())
        _strAssemblyPath = assembly
        _bytSNKFile = snk
    End Sub

    Public Sub New(ByVal assembly As String, ByVal snk As String)
        _strAssemblyPath = assembly
        _bytSNKFile = IO.File.ReadAllBytes(snk)
    End Sub

#End Region

#Region " ******    API Functions       "

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameFreeBuffer", CharSet:=CharSet.Auto)> _
    Private Shared Sub StrongNameFreeBuffer(ByVal pbMemory As IntPtr)
    End Sub

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameKeyDelete", CharSet:=CharSet.Auto)> _
    Private Shared Function StrongNameKeyDelete(ByVal wszKeyContainer As String) As Boolean
    End Function

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameKeyInstall", CharSet:=CharSet.Auto)> _
    Private Shared Function StrongNameKeyInstall( _
            <MarshalAs(UnmanagedType.LPWStr)> ByVal wszKeyContainer As String, _
            <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=2, SizeConst:=0)> ByVal pbKeyBlob As Byte(), _
            ByVal cbKeyBlob As Integer) As Boolean
    End Function

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameErrorInfo", CharSet:=CharSet.Auto)> _
    Private Shared Function StrongNameErrorInfo() As UInt32
    End Function

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameSignatureGeneration", CharSet:=CharSet.Auto)> _
    Private Shared Function StrongNameSignatureGeneration( _
            ByVal wszFilePath As String, _
            ByVal wszKeyContainer As String, _
            ByVal pbKeyBlob As Integer, _
            ByVal cbKeyBlob As Integer, _
            ByVal ppbSignatureBlob As Integer, _
            ByVal pcbSignatureBlob As Integer) As Boolean
    End Function

    <DllImport("MsCorEE.dll", EntryPoint:="StrongNameTokenFromAssemblyEx", CharSet:=CharSet.Auto)> _
    Private Shared Function StrongNameTokenFromAssemblyEx( _
            ByVal wszFilePath As String, _
            <Out()> ByRef ppbStrongNameToken As IntPtr, _
            <Out()> ByRef pcbStrongNameToken As ULong, _
            ByRef ppbPublicKeyBlob As IntPtr, _
            ByRef pcbPublicKeyBlob As ULong) As Boolean
    End Function

#End Region

#Region " ******    Help Functions      "

    Private Function CheckForPubliKeyIndex(ByVal intIndex1 As Integer) As Boolean

        Dim intIndex2 As Integer

            If _bytAssemblyPublicKey(intIndex2) <> _bytAssembly(intIndex2 + intIndex1) Then Return False
            intIndex2 += 1

        Loop While (intIndex2 < _bytAssemblyPublicKey.Length)

        Return True

    End Function

#End Region

#Region " ******    Form Events         "

    Public Function Resign() As Boolean

        Dim bytSNK As Byte()
        Dim blnReturn As Boolean
        Dim strSNKFile As String = ""

            strSNKFile = My.Computer.FileSystem.GetTempFileName
            IO.File.WriteAllBytes(strSNKFile, _bytSNKFile)

            Using keyPairFile As FileStream = File.OpenRead(strSNKFile)
                bytSNK = New StrongNameKeyPair(keyPairFile).PublicKey
            End Using

            _bytAssemblyPublicKey = AssemblyName.GetAssemblyName(_strAssemblyPath).GetPublicKey

            If _bytAssemblyPublicKey Is Nothing Then Exit Try

            Using csFileStream As FileStream = File.OpenRead(_strAssemblyPath)
                _bytAssembly = New Byte(csFileStream.Length - 1) {}
                csFileStream.Read(_bytAssembly, 0, csFileStream.Length)
            End Using

            Dim intAssemblyPublicKeyIndex As Byte = _bytAssemblyPublicKey(0)
            Dim intCounter As Integer
            Dim intPublicKeyIndex As Integer

                If ((_bytAssembly(intCounter) = intAssemblyPublicKeyIndex) AndAlso Me.CheckForPubliKeyIndex(intCounter)) Then
                    intPublicKeyIndex = intCounter
                    Exit Do
                End If
                intCounter += 1
            Loop While (intCounter < Me._bytAssembly.Length)

            Dim num4 As Integer

                _bytAssembly((intPublicKeyIndex + num4)) = bytSNK(num4)
                num4 += 1
            Loop While (num4 < bytSNK.Length)

            Using csFileStream As FileStream = File.Create(Me._strAssemblyPath)
                csFileStream.Write(_bytAssembly, 0, Me._bytAssembly.Length)
            End Using

            Me._strNewStrongKeyName = String.Format("{0}-{1}", Me.csRandom.Next(&H989680, &H5F5E0FF).ToString, Me.csRandom.Next(&H989680, &H5F5E0FF).ToString)


            Using input As New FileStream(strSNKFile, FileMode.Open, FileAccess.Read)
                Using reader As New BinaryReader(input)

                    Dim buffer2 As Byte() = New Byte(CInt(reader.BaseStream.Length) - 1) {}
                    reader.BaseStream.Seek(0, SeekOrigin.Begin)
                    reader.Read(buffer2, 0, CInt(reader.BaseStream.Length))

                    Dim length As Integer = CInt(reader.BaseStream.Length)

                    If Not StrongNameKeyInstall(Me._strNewStrongKeyName, buffer2, length) Then
                        ' Failed to sign the assembly
                    ElseIf StrongNameSignatureGeneration(Me._strAssemblyPath, Me._strNewStrongKeyName, 0, 0, 0, 0) Then
                        blnReturn = True
                        ' Failed to sign the assembly
                    End If

                End Using
            End Using

        Catch exc As Exception
            ' blnReturn = False ' MsgBox(exc.Message)
        End Try

        Return blnReturn

    End Function

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ahmad Dekmak
Technical Lead
Lebanon Lebanon
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 18 Dec 2012
Article Copyright 2012 by Ahmad Dekmak
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid