An effective method for encrypting a license file?

前端 未结 9 1540
走了就别回头了
走了就别回头了 2021-01-29 18:58

For a web application, I would like to create a simple but effective licensing system. In C#, this is a little difficult, since my decryption method could be viewed by anyone wi

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 19:33

    Wolfwyrd's answer is excellent, but I just wanted to offer a muich simpler version of Wolfwyrd's GetPublicKeyFromAssembly method (which uses a lot of helper methods from the library Wolfwyrd graciously supplied).

    In this version, this is all the code you need:

    /// 
    /// Extracts an RSA public key from a signed assembly
    /// 
    /// Signed assembly to extract the key from
    /// RSA Crypto Service Provider initialised with the public key from the assembly
    private RSACryptoServiceProvider GetPublicKeyFromAssembly(Assembly assembly)
    {
        // Extract public key - note that public key stored in assembly has an extra 3 headers
        // (12 bytes) at the front that are not part of a CAPI public key blob, so they must be removed
        byte[] rawPublicKeyData = assembly.GetName().GetPublicKey();
    
        int extraHeadersLen = 12;
        int bytesToRead = rawPublicKeyData.Length - extraHeadersLen;
        byte[] publicKeyData = new byte[bytesToRead];
        Buffer.BlockCopy(rawPublicKeyData, extraHeadersLen, publicKeyData, 0, bytesToRead);
    
        RSACryptoServiceProvider publicKey = new RSACryptoServiceProvider();
        publicKey.ImportCspBlob(publicKeyData);
    
        return publicKey;
    }
    

提交回复
热议问题