public static string GenerateLicenseBASE64String(LicenseEntity lic, byte[] certPrivateKeyData, SecureString certFilePwd) { XmlDocument _licenseObject = new XmlDocument(); using (StringWriter _writer = new StringWriter()) { var _serializer = new XmlSerializer(typeof(LicenseEntity), new Type[] { lic.GetType() }); _serializer.Serialize(_writer, lic); _licenseObject.LoadXml(_writer.ToString()); } var cert = new X509Certificate2(certPrivateKeyData, certFilePwd); var rsaKey = (RSACryptoServiceProvider)cert.PrivateKey; SignXML(_licenseObject, rsaKey); 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 { X509Certificate2 cert = new X509Certificate2(certPubKeyData); RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PublicKey.Key; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(licenseString))); if (VerifyXml(xmlDoc, rsaKey)) { XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature"); xmlDoc.DocumentElement.RemoveChild(nodeList[0]); _licXML = xmlDoc.OuterXml; 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); }