您的位置:首页 > 其它

利用RSACryptoServiceProvider进行RSA加密解密

2017-07-04 15:46 621 查看

利用RSACryptoServiceProvider进行RSA加密解密

加密获取公私钥

static void Main(string[] args)
{
RsaPrivateKey xmlPrivateKeys;
RsaPublicKey xmlPublicKey;
RSAKey(out xmlPrivateKeys,out xmlPublicKey);
}

public static void RSAKey(out RsaPrivateKey xmlPrivateKeys, out RsaPublicKey xmlPublicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string xmlPrivateKeyStr = rsa.ToXmlString(true);
string xmlPublicKeyStr = rsa.ToXmlString(false);
xmlPublicKey = XmlCustomSerialization.XmlDeserialize<RsaPublicKey>(xmlPublicKeyStr);
xmlPublicKey.PublicKeyStr = xmlPublicKeyStr;
xmlPrivateKeys = XmlCustomSerialization.XmlDeserialize<RsaPrivateKey>(xmlPrivateKeyStr);
xmlPrivateKeys.PrivateKeyStr = xmlPrivateKeyStr;
}


RsaPrivateKey

[XmlType(AnonymousType = true), XmlRoot(Namespace = "", IsNullable = false, ElementName = "RSAKeyValue")]
public class RsaPublicKey
{
// Fields
//[CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never)]
//private string <PublicKeyStr>k__BackingField;
private string dField;
private string dpField;
private string dqField;
private string exponentField;
private string inverseQField;
private string modulusField;
private string pField;
private string qField;

// Methods
public RsaPublicKey() { }

// Properties
public string D { get; set; }
public string DP { get; set; }
public string DQ { get; set; }
public string Exponent { get; set; }
public string InverseQ { get; set; }
public string Modulus { get; set; }
public string P { get; set; }
public string PublicKeyStr { get; set; }
public string Q { get; set; }
}


View Code

XmlCustomSerialization xml序列化帮助类

public class XmlCustomSerialization
{
// Methods
public static T XmlDeserialize<T>(string item)
{
using (StringReader stringReader = new StringReader(item))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stringReader);
}
}

public static string XmlSerialize(object item, bool removeNamespace = false, bool omitXmlDeclaration = false)
{
string serialXML = string.Empty;
try
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings1 = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = omitXmlDeclaration,
Encoding = Encoding.UTF8
};
XmlWriterSettings settings = settings1;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlSerializer xmlSerializer = new XmlSerializer(item.GetType());
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
if (removeNamespace)
{
xmlSerializer.Serialize(writer, item, ns);
}
else
{
xmlSerializer.Serialize(writer, item);
}
}
serialXML = sb.ToString();
}
catch
{
throw;
}
return serialXML;
}

public static string XmlSerializeToPureXML(object item)
{
string serialXML = string.Empty;
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
CloseOutput = true,
Encoding = Encoding.UTF8
};
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
{
XmlSerializer xmlSerializer = new XmlSerializer(item.GetType());
XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add(string.Empty, string.Empty);
xmlSerializer.Serialize(xmlWriter, item, xmlSerializerNamespaces);
serialXML = stringWriter.ToString();
xmlWriter.Flush();
}
stringWriter.Flush();
}
return serialXML;
}

public static string XmlSerializeWithNoXmlDeclaration(object item)
{
string serialXML = string.Empty;
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
CloseOutput = true,
Encoding = Encoding.UTF8
};
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings))
{
new XmlSerializer(item.GetType()).Serialize(xmlWriter, item);
serialXML = stringWriter.ToString();
xmlWriter.Flush();
}
stringWriter.Flush();
}
return serialXML;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: