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))); }
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 (Exception ex) { licStatus = LicenseStatus.CRACKED; } return(_lic); }
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); } LicenseEntity lic = null; try { //Get RSA key from certificate var cert = new X509Certificate2(certPubKeyData); var rsaKey = (RSACryptoServiceProvider)cert.PublicKey.Key; var 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)) { var nodeList = xmlDoc.GetElementsByTagName("Signature"); xmlDoc.DocumentElement.RemoveChild(nodeList[0]); string licXml = xmlDoc.OuterXml; //Deserialize license var serializer = new XmlSerializer(typeof(LicenseEntity), new[] { licenseObjType }); using (var reader = new StringReader(licXml)) { lic = (LicenseEntity)serializer.Deserialize(reader); } licStatus = lic.DoExtraValidation(out validationMsg); } else { licStatus = LicenseStatus.Invalid; } } catch { licStatus = LicenseStatus.Cracked; } return(lic); }
public static string GenerateLicenseBASE64String(LicenseEntity lic, string certPrivateKeyFilePath, string 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(certPrivateKeyFilePath, 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)); }