zoukankan      html  css  js  c++  java
  • 程序集强命名替换

    // .NET Assembly Strong Name Replacer
    // (C) 2004 Michael Giagnocavo
    // www.atrevido.net

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;

    public class SNReplace
    {
        
    public static void Main(string[] args)
        {
            
    if (args.Length < 2)
            {
                Console.WriteLine(
    "Usage: SNReplace <assembly> <keyfile>");
                
    return;
            }
            SNReplace snr 
    = new SNReplace();
            snr.AssemblyFile            
    = args[0];
            snr.KeyFile                    
    = args[1];
            snr.Replace();
        }


        
    public string KeyFile;
        
    public string AssemblyFile;

        
    byte[] currentKey;
        
    byte[] newKey;
            
        
    byte[] assembly;

        
    private int keyStart;

        
    public void Replace()
        {
            readKeys();
            readAssembly();
            findKeyStart();
            replaceKey();
            save();
            resign();
        }


        
    private void readKeys()
        {
            
    this.currentKey = AssemblyName.GetAssemblyName(this.AssemblyFile).GetPublicKey();

            FileStream keyStream 
    = File.OpenRead(this.KeyFile);
            
    this.newKey = new StrongNameKeyPair(keyStream).PublicKey;
            keyStream.Close();
        }


        
    private void readAssembly()
        {
            
    using (FileStream fs = File.OpenRead(this.AssemblyFile))
            {
                
    this.assembly = new byte[fs.Length];
                fs.Read(
    this.assembly, 0, (int)fs.Length);
            }
        }
            

        
    private void findKeyStart()
        {
            
    // Yes, it's a slow algorithm.
            byte startByte = currentKey[0];
            
    for(int i = 0; i < this.assembly.Length; i++)
            {
                
    if (assembly[i] == startByte)
                {
                    
    // Possible match
                    if (isKeyFound(i)) 
                    {
                        
    this.keyStart = i;
                        
    return;
                    }
                }
            }
            
    // Not found
            Console.WriteLine("Something's wrong. Public key not found.");
            Environment.Exit(
    1);
        }


        
    private bool isKeyFound(int startIndex)
        {
            
    for(int i = 0; i < currentKey.Length; i++)
            {
                
    if (currentKey[i] != assembly[i + startIndex])
                {
                    
    return false;
                }
            }
            
    return true;
        }


        
    private void replaceKey()
        {
            
    for(int i = 0; i < newKey.Length; i++)
            {
                assembly[keyStart 
    + i] = newKey[i];
            }
        }


        
    private void save()
        {
            
    using (FileStream fs = File.Create(this.AssemblyFile))
            {
                fs.Write(assembly, 
    0, assembly.Length);
            }
        }


        
    private void resign()
        {
            ProcessStartInfo si 
    = new ProcessStartInfo("sn.exe");
            si.Arguments 
    = string.Format("-R {0} {1}"this.AssemblyFile, this.KeyFile);
            
    try 
            {
                Process p 
    = Process.Start(si);
                
    if (!p.WaitForExit(15000))
                {
                    Console.WriteLine(
    "Timeout while waiting for SN to resign the assembly.");
                    p.Kill();
                    Environment.Exit(
    2);
                }
            }
            
    catch (System.ComponentModel.Win32Exception)
            {
                Console.WriteLine(
    "The assembly's public key has been replaced, but SN.exe could not be started. Ensure SN.exe (in the .NET Framework SDK) is in your path, then run SN -R <assembly> <keyfile>.");
            }
        }
    }
  • 相关阅读:
    枚举--分巧克力--蓝桥杯--二分法
    枚举笔记之哈希表 四个平方和
    枚举 蓝桥杯 四个平方数和2--暴力解法 超时
    枚举例题之平方十位数思路无代码
    LeetCode----盛最多水的容器「贪心」
    2020校招笔试
    2020校招美团点评笔试
    2020校招搜狗笔试
    2020网易校招笔试
    2020校招途家名宿开发笔试
  • 原文地址:https://www.cnblogs.com/jintan/p/1544153.html
Copyright © 2011-2022 走看看