-
-
Save adam-skelton/cfb80dee94771064a3e487a953519db1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void EncryptDecryptLicense() | |
{ | |
string encryptedFilePath = MyDir + "EncryptedLicense.txt"; | |
// Load the contents of the license into a byte array. | |
byte[] licBytes = File.ReadAllBytes("YourLicense.lic"); | |
// Use this key only once for this license file. | |
// To protect another file first generate a new key. | |
byte[] key = GenerateKey(licBytes.Length); | |
// Write the encrypted license to disk. | |
File.WriteAllBytes(encryptedFilePath, EncryptDecryptLicense(licBytes, key)); | |
// Load the encrypted license and decrypt it using the key. | |
byte[] decryptedLicense; | |
decryptedLicense = EncryptDecryptLicense(File.ReadAllBytes(encryptedFilePath), key); | |
// Load the decrypted license into a stream and set the license. | |
MemoryStream licenseStream = new MemoryStream(decryptedLicense); | |
License license = new License(); | |
license.SetLicense(licenseStream); | |
} | |
/// <summary> | |
/// A method used for encrypting and decrypting data using XOR. | |
/// </summary> | |
public byte[] EncryptDecryptLicense(byte[] licBytes, byte[] key) | |
{ | |
byte[] output = new byte[licBytes.Length]; | |
for (int i = 0; i < licBytes.Length; i++) | |
output[i] = Convert.ToByte(licBytes[i] ^ key[i]); | |
return output; | |
} | |
/// <summary> | |
/// Generates a random key the same length as the license (a one time pad). | |
/// </summary> | |
public byte[] GenerateKey(long size) | |
{ | |
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | |
byte[] strongBytes = new Byte[size]; | |
rng.GetBytes(strongBytes); | |
return strongBytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment