/// <summary> /// Criptografa o texto informado. /// </summary> /// <param name="inputString">Texto que será criptografado.</param> /// <param name="key"></param> /// <returns></returns> public static string EncryptString(string inputString, RSACryptografyKey key) { key.Require("key").NotNull(); var xmlString = string.Format("<BitStrength>{0}</BitStrength><RSAKeyValue><Modulus>{1}</Modulus><Exponent>{2}</Exponent></RSAKeyValue>", key.BitStrength, key.RSAKeyValue != null ? key.RSAKeyValue.Modulus : null, key.RSAKeyValue != null ? key.RSAKeyValue.Exponent : null); return(EncryptString(inputString, key.BitStrength, xmlString)); }
/// <summary> /// Descriptografa o texto informado. /// </summary> /// <param name="inputString">Texto contem os dados criptografados.</param> /// <param name="key">Chavem que será usada.</param> public static string DecryptString(string inputString, RSACryptografyKey key) { key.Require("key").NotNull(); RSACryptografyKey.RSAKey keyValue = key.RSAKeyValue; if (keyValue == null) { keyValue = new RSACryptografyKey.RSAKey(); } var serializer = new System.Xml.Serialization.XmlSerializer(typeof(RSACryptografyKey.RSAKey)); var sb = new StringBuilder(); using (var writer = new System.IO.StringWriter(sb)) serializer.Serialize(writer, keyValue); var xmlString = sb.ToString(); return(DecryptString(inputString, key.BitStrength, xmlString)); }