Free Support Forum - aspose.com

Licence number visible in the DLL

Hi there,

I've used a "embedded resource" file type for Aspose.Words.lic, and I realize that when I open my DLL with notepad, this file content is fully visible/readable. How can I encrypt that file ?

Best regards

Hi

Thanks for your request. Here are the steps that you should follow to protect your license file by encoding/decoding.

1. Encode your license file using the following method or your own method. For example I used simplest method to encode/decode string using XOR operation.

public void Test()

{

int key = 94;

//Read text from file

string input = File.ReadAllText(@"C:\Temp\Aspose.Words.lic");

//Encode license

string output = EncodeDecodeLicense(key, input);

//Save encoded file

Stream outputFile = new FileStream(@"C:\Temp\encodedLicense", FileMode.Create);

StreamWriter writer = new StreamWriter(outputFile);

writer.Write(output);

writer.Close();

outputFile.Close();

}

///

/// Method encode txt using XOR

///

/// Key used for encoding

/// input string

private string EncodeDecodeLicense(int key, string input)

{

//Read original file as byte array

char[] originalBytes = input.ToCharArray();

//Create output string

string outputString = string.Empty;

//Loop through all bytes in original array and encode each byte using XOR operation

for (int i = 0; i < originalBytes.Length; i++)

{

outputString += (char)(originalBytes[i] ^ key);

}

return outputString;

}

2. Include encrypted license as embedded resource by performing the following steps:

- In Visual Studio .NET, include the .lic file into the project using the File | Add Existing Item... menu.

- Select the file in the Solution Explorer and set Build Action to Embedded Resource in the Properties window.

- In your code, invoke SetLicense passing only the short name of the resource file.

3. In your application you should read embedded resource decode it and apply license. Here is the code:

int key = 94;

System.IO.MemoryStream licStrm = new System.IO.MemoryStream();

try

{

System.Reflection.Assembly assem = this.GetType().Assembly;

using (System.IO.Stream stream = assem.GetManifestResourceStream("MyApp.encodedLicense"))

{

// read data from stream

stream.Position = 0;

System.IO.StreamReader reader = new System.IO.StreamReader(stream);

string encodedLicense = reader.ReadToEnd();

reader.Close();

//Decode license

string decodedLicense = EncodeDecodeLicense(key, encodedLicense);

licStrm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(decodedLicense));

}

//Set license

Aspose.Words.License lic = new Aspose.Words.License();

lic.SetLicense(licStrm);

}

catch { }

Please note:

- Key should be that same for encoding and for decoding.

- Embedded resource name will contain Application name and file name of embedded recourse. (see highlighted code)

Best regards,

Thank you for your answer !

Best regards

But if the “key” is included in the source code, why bather encrypting the file?

Yes you are right, but it is better than nothing. Otherwise, you can use a Dotfuscator.

Hi Magnus,

Thanks for your remark. The code above is just an example that shows the possible way of encryption. You can use any other algorithm to encrypt the license file. Also obfuscation of your code is a possible way to prevent it’s reverse-engineering.

Best regards,