public static string GenerateLicenseBASE64String(LicenseEntity lic, byte[] certPrivateKeyData, SecureString certFilePwd) { //Serialize license object into XML XmlDocument _licenseObject = new XmlDocument(); using (StringWriter _writer = new StringWriter()) { XmlSerializer _serializer = new XmlSerializer(typeof(LicenseEntity), new Type[] { lic.GetType() }); _serializer.Serialize(_writer, lic); _licenseObject.LoadXml(_writer.ToString()); } //Get RSA key from certificate X509Certificate2 cert = new X509Certificate2(certPrivateKeyData, certFilePwd); RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PrivateKey; //Sign the XML SignXML(_licenseObject, rsaKey); //Convert the signed XML into BASE64 string return(Convert.ToBase64String(Encoding.UTF8.GetBytes(_licenseObject.OuterXml))); }
private static LicenseEntity ReadLicense(Type licenseObjType, string licenseString, byte[] certPubKeyData, out LicenseStatus licStatus, out string validationMsg) { if (string.IsNullOrWhiteSpace(licenseString)) { licStatus = LicenseStatus.Cracked; validationMsg = "Licencja uszkodzona"; return(null); } LicenseEntity license = null; try { //Get RSA key from certificate X509Certificate2 cert = new X509Certificate2(certPubKeyData); RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PublicKey.Key; XmlDocument xmlDoc = new XmlDocument { PreserveWhitespace = true }; // Load an XML file into the XmlDocument object. xmlDoc.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(licenseString))); // Verify the signature of the signed XML. if (VerifyXml(xmlDoc, rsaKey)) { XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature"); xmlDoc.DocumentElement?.RemoveChild(nodeList[0]); string licXml = xmlDoc.OuterXml; //Deserialize license XmlSerializer serializer = new XmlSerializer(typeof(LicenseEntity), new[] { licenseObjType }); using (StringReader reader = new StringReader(licXml)) { license = (LicenseEntity)serializer.Deserialize(reader); } licStatus = license.DoExtraValidation(out validationMsg); } else { licStatus = LicenseStatus.Invalid; validationMsg = "Nieprawidłowy plik licencji"; } } catch { licStatus = LicenseStatus.Cracked; validationMsg = "Licencja uszkodzona"; } return(license); }
public static LicenseEntity ParseLicenseFromBASE64String(Type licenseObjType, string licenseString, byte[] certPubKeyData, out LicenseStatus licStatus, out string validationMsg) { validationMsg = string.Empty; licStatus = LicenseStatus.UNDEFINED; if (string.IsNullOrWhiteSpace(licenseString)) { licStatus = LicenseStatus.CRACKED; return(null); } string _licXML = string.Empty; LicenseEntity _lic = null; try { //Get RSA key from certificate X509Certificate2 cert = new X509Certificate2(certPubKeyData); RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PublicKey.Key; XmlDocument xmlDoc = new XmlDocument(); // Load an XML file into the XmlDocument object. xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(licenseString))); // Verify the signature of the signed XML. if (VerifyXml(xmlDoc, rsaKey)) { XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature"); xmlDoc.DocumentElement.RemoveChild(nodeList[0]); _licXML = xmlDoc.OuterXml; //Deserialize license XmlSerializer _serializer = new XmlSerializer(typeof(LicenseEntity), new Type[] { licenseObjType }); using (StringReader _reader = new StringReader(_licXML)) { _lic = (LicenseEntity)_serializer.Deserialize(_reader); } licStatus = _lic.DoExtraValidation(out validationMsg); } else { licStatus = LicenseStatus.INVALID; } } catch { licStatus = LicenseStatus.CRACKED; } return(_lic); }