/// <summary> /// Decrypts the key and returns the decrypted buffer. /// </summary> /// <param name="buffer">The key to decrypt.</param> /// <param name="offset">The offset to start the decryption at.</param> /// <returns></returns> private static void Decrypt(byte[] buffer, int offset) { // First decrypt the key SecurityEncryption.Decrypt(buffer, offset); // Then XOR the entire array with the salt for (int i = (offset + 2); i < (offset + 24); i += 2) { buffer[i] = (byte)(buffer[i] ^ buffer[offset]); buffer[i + 1] = (byte)(buffer[i + 1] ^ buffer[offset + 1]); } }
/// <summary> /// Encrypts the key and returns the encrypted buffer. /// </summary> /// <returns>Encrypted buffer.</returns> private byte[] Encrypt() { // The buffer for encryption var buffer = new byte[24]; buffer[0] = this.Buffer[this.Offset]; buffer[1] = this.Buffer[this.Offset + 1]; // First XOR the entire array with the salt for (int i = 2; i < 24; i += 2) { buffer[i] = (byte)(this.Buffer[this.Offset + i] ^ buffer[0]); buffer[i + 1] = (byte)(this.Buffer[this.Offset + i + 1] ^ buffer[1]); } // Then encrypt the key using the master key return(SecurityEncryption.Encrypt(buffer)); }
/// <summary> /// Decrypts the license with the public key. /// </summary> /// <param name="data"></param> /// <param name="keySize"></param> /// <returns></returns> public static SecurityLicense LoadAndVerify(string data) { try { // We should have the data if (String.IsNullOrWhiteSpace(data)) { throw new SecurityLicenseException("No license was found, please provide a valid license key through the configuration file, an EMITTER_LICENSE environment variable or a valid vault key 'secrets/emitter/license'."); } // Load the license Current = new SecurityLicense( Base64.UrlEncoding.FromBase(data) ); // Expired? if (DateTime.UtcNow > Current.Expires && Current.Expires > new DateTime(2000, 1, 1)) { throw new SecurityLicenseException("The license file provided has expired."); } // Set the encryption key for the service SecurityEncryption.SupplyKey(Current.EncryptionKey); // Return the license now, but scramble the encryption key Current.EncryptionKey = null; return(Current); } catch (SecurityLicenseException ex) { // We do not return an exception, as the license is invalid anyway Service.Logger.Log(LogLevel.Error, ex.Message); return(null); } catch (Exception) { // We do not return an exception, as the license is invalid anyway Service.Logger.Log(LogLevel.Error, "Invalid license was provided, please check the configuration file or use EMITTER_LICENSE environment variable to provide a valid license key."); return(null); } }