private EncryptedLicense LoadLicense(LicenseContext context, Type type, string licenseKey)
{
// check that validation parameters have been set by the client
//
if (_rsaParameters == null || _designSignature == null || _runtimeSignature == null)
throw new InvalidOperationException("EncryptedLicenseProvider.SetParameters must be called prior to using the EncryptedLicenseProvider");
if (licenseKey == null) return null;
try
{
byte[] encData = FromHex(licenseKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = _desKey;
des.IV = _desIV;
byte[] data = des.CreateDecryptor().TransformFinalBlock(encData, 0, encData.Length);
// extract the encryption key and encrypted product data - note that the encryption
// key has only 7 significant bytes
//
byte[] encryptionKey = new byte[ArraySize(8)];
byte[] encPayload = new byte[ArraySize(data.Length - keyLength)];
Array.Copy(data, 0, encryptionKey, 0, keyLength);
Array.Copy(data, keyLength, encPayload, 0, encPayload.Length);
// validate that the password matches what the client is expecting
//
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(_rsaParameters);
if (context.UsageMode == LicenseUsageMode.Designtime)
{
// if design time license requested then the license MUST be a design license
//
if (!rsa.VerifyData(encryptionKey, new SHA1CryptoServiceProvider(), _designSignature)) return null;
}
else
{
// if runtime license requested then first check if the license is a runtime license
// also allow design licenses to work at runtime
//
if (!rsa.VerifyData(encryptionKey, new SHA1CryptoServiceProvider(), _runtimeSignature))
{
if (!rsa.VerifyData(encryptionKey, new SHA1CryptoServiceProvider(), _designSignature)) return null;
}
}
// decrypt the payload using the encryption key
//
des.IV = encryptionKey;
byte[] payload = des.CreateDecryptor().TransformFinalBlock(encPayload, 0, encPayload.Length);
byte[] productData = new byte[ArraySize(payload.Length - 2)];
Array.Copy(payload, 2, productData, 0, productData.Length);
UInt16 serialNo = BitConverter.ToUInt16(payload, 0);
string product = System.Text.ASCIIEncoding.UTF8.GetString(productData);
// if in design time then create a runtime license and save it
//
if (context.UsageMode == LicenseUsageMode.Designtime && type != null)
{
// create the runtime password by encrypting the design time license
//
byte[] encKey = des.CreateEncryptor().TransformFinalBlock(encryptionKey, 0, encryptionKey.Length);
byte[] runtimeKey = new byte[ArraySize(8)];
Array.Copy(encKey, 0, runtimeKey, 0, keyLength);
// encrypt the payload using the runtime key
//
des.IV = runtimeKey;
encPayload = des.CreateEncryptor().TransformFinalBlock(payload, 0, payload.Length);
// Combine the runtime key and encrypted payload
// Note that only the first 7 bytes of the key contain information so we
// only pack this much information - this enables us to reduce the size of
// the final key by 8 bytes.
//
data = new byte[ArraySize(keyLength + encPayload.Length)];
runtimeKey.CopyTo(data, 0);
encPayload.CopyTo(data, keyLength);
// encrypt again to obscure the password - this time using preset encryption key
//
des.IV = _desIV;
encData = des.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
string runtimeLicenseKey = ToHex(encData);
// save the runtime key into the context
//
context.SetSavedLicenseKey(type, runtimeLicenseKey);
}
return new EncryptedLicense(licenseKey, serialNo, product);
}
catch
{
return null;
}
}