public string SerializeCngKeyWithPrivateKey(CngKey toBeSerialized) { if (toBeSerialized == null) { throw new ArgumentNullException("toBeSerialized"); } var privateBlob = toBeSerialized.Export(CngKeyBlobFormat.EccPrivateBlob); var lengthBytes = new[] { privateBlob[4], privateBlob[5], privateBlob[6], privateBlob[7] }; // Part size in octet var partLength = BitConverter.ToInt32(lengthBytes, 0); if (!MappingSizeAndMagicNumberForPrivateKey.ContainsKey(partLength)) { throw new InvalidOperationException(string.Format("the part length {0} is not correct", partLength)); } var allPartsInBytes = ByteManipulator.RightmostBits(privateBlob, partLength * 8 * 3); var keyParts = ByteManipulator.Slice(allPartsInBytes, partLength); var cngKey = new CngKeySerialized { X = keyParts[0], Y = keyParts[1], D = keyParts[2] }; var serializer = new XmlSerializer(typeof(CngKeySerialized)); using (var writer = new StringWriter()) { serializer.Serialize(writer, cngKey); return(writer.ToString()); } }