/// <summary> /// 使用公钥解密 /// </summary> /// <param name="data"></param> /// <returns></returns> public byte[] DecryptByPublic(byte[] data) { //var xml = this._provider.ToXmlString(false); var parameter = this._provider.ExportParameters(false); var pub = new Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters(false, new Org.BouncyCastle.Math.BigInteger(1, parameter.Modulus), new Org.BouncyCastle.Math.BigInteger(1, parameter.Exponent)); var helper = new RsaKeyHelper(pub, null); return(helper.DecryptByPublic(data)); }
private static RsaKeyHelper TryResolvePrivateKeyWithNoFormatDeclare(string keyString, RsaKeyHelper.KeyFormat format) { try { string pkcs1 = RsaKeyHelper.FormatPrivateKey(keyString, format); var helper = RsaKeyHelper.FromPemKeyString(pkcs1); helper.RSACryptoServiceProvider(); return(helper); } catch (Exception ex) { return(null); } }
/// <summary> /// 生成RSA密钥 /// </summary> /// <param name="size">1024或2048</param> /// <returns></returns> public static RsaKeyHelper GenRsaKey(int size) { if (size == 0 || (size % 1024) != 0) { throw new Exceptions.ArgumentFormatException("size数值不正确,只能在1024和2048选择一个"); } RsaKeyPairGenerator rsaGen = new RsaKeyPairGenerator(); rsaGen.Init(new KeyGenerationParameters(new SecureRandom(), size)); AsymmetricCipherKeyPair keys = rsaGen.GenerateKeyPair(); RsaKeyHelper helper = new RsaKeyHelper((RsaKeyParameters)keys.Public, (RsaPrivateCrtKeyParameters)keys.Private); return(helper); }
/// <summary> /// 使用私钥加密 /// </summary> /// <param name="data"></param> /// <returns></returns> public byte[] EncryptByPrivate(byte[] data) { //var xml = this._provider.ToXmlString(true); var parameter = this._provider.ExportParameters(true); var pub = new Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters(false, new Org.BouncyCastle.Math.BigInteger(1, parameter.Modulus), new Org.BouncyCastle.Math.BigInteger(1, parameter.Exponent)); var pvt = new Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters( new Org.BouncyCastle.Math.BigInteger(1, parameter.Modulus), new Org.BouncyCastle.Math.BigInteger(1, parameter.Exponent), new Org.BouncyCastle.Math.BigInteger(1, parameter.D), new Org.BouncyCastle.Math.BigInteger(1, parameter.P), new Org.BouncyCastle.Math.BigInteger(1, parameter.Q), new Org.BouncyCastle.Math.BigInteger(1, parameter.DP), new Org.BouncyCastle.Math.BigInteger(1, parameter.DQ), new Org.BouncyCastle.Math.BigInteger(1, parameter.InverseQ) ); RsaKeyHelper helper = new RsaKeyHelper(pub, pvt); //var helper = RsaKeyHelper.FromXmlKey(xml); return(helper.EncryptByPrivate(data)); }
/// <summary> /// /// </summary> /// <param name="keyString">密钥字符串</param> /// <exception cref="InvalidKeyFormatException"></exception> /// <returns></returns> public static PemCertificate ReadFromKeyString(string keyString) { PemCertificate pemcert = new PemCertificate(); RsaKeyHelper helper = null; if (keyString.StartsWith("-----")) { helper = RsaKeyHelper.FromPemKeyString(keyString); } else { //纯密钥文本,不带声明格式 keyString = keyString.Replace("\r", "").Replace("\n", ""); if (Convert.FromBase64String(keyString).Length < 400) { keyString = RsaKeyHelper.FormatPublicKey(keyString); helper = RsaKeyHelper.FromPemKeyString(keyString); } else { helper = TryResolvePrivateKeyWithNoFormatDeclare(keyString, RsaKeyHelper.KeyFormat.pkcs1) ?? TryResolvePrivateKeyWithNoFormatDeclare(keyString, RsaKeyHelper.KeyFormat.pkcs8); } } if (helper == null) { throw new InvalidKeyFormatException("无法识别的PEM密钥格式"); } pemcert._provider = helper.RSACryptoServiceProvider(); return(pemcert); //byte[] res = null; //string type = pem.StartsWith(PEM_PRIVATE_KEY_HEADER) ? "RSA PRIVATE KEY" : pem.StartsWith(PEM_PUBLIC_KEY_HEADER) ? "PUBLIC KEY" : "PLAIN TEXT"; //if (type == "PLAIN TEXT") //{ // res = Convert.FromBase64String(pem); //} //else //{ // byte[] data = Encoding.UTF8.GetBytes(pem); // res = GetPem(type, data); //} //if (type.Equals("RSA PRIVATE KEY"))//私钥 //{ // pemcert._provider = DecodeRSAPrivateKey(res); //} //else if (type.Equals("PUBLIC KEY")) //{ // pemcert._provider = DecodeX509PublicKey(res); //} //else if (type.Equals("PLAIN TEXT")) //{ // if (res.Length == 608 || res.Length == 611 || res.Length == 1193) //PKCS#1 PRIVATE KEY,keysize=1024,bytes=608 // { // pemcert._provider = DecodeRSAPrivateKey(res); // } // else if (res.Length == 634 || res.Length == 635 || res.Length == 1217 || res.Length == 1218)//ASN.1 PRIVATE KEY // { // pemcert._provider = DecodeASN1PrivateKey(res); // } // else // { // pemcert._provider = DecodeX509PublicKey(res); // if (pemcert._provider == null) // { // pemcert._provider = DecodeRSAPrivateKey(res); // if (pemcert._provider == null) // { // pemcert._provider = DecodeASN1PrivateKey(res); // } // } // } //} //return pemcert; }
/// <summary> /// 从xml密钥初始化 /// </summary> /// <param name="xmlPrivateKey"></param> /// <returns></returns> public static RsaKeyHelper FromXmlKey(string xmlPrivateKey) { XElement root = XElement.Parse(xmlPrivateKey); //Modulus var modulus = root.Element("Modulus"); var M = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(modulus.Value)); //Exponent var exponent = root.Element("Exponent"); var E = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(exponent.Value)); Org.BouncyCastle.Math.BigInteger P = null, Q = null, DP = null, DQ = null, QI = null, D = null; //P var pXml = root.Element("P"); if (pXml != null) { P = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(pXml.Value)); } //Q var qXml = root.Element("Q"); if (qXml != null) { Q = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(qXml.Value)); } //DP var dpXml = root.Element("DP"); if (dpXml != null) { DP = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(dpXml.Value)); } //DQ var dqXml = root.Element("DQ"); if (dqXml != null) { DQ = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(dqXml.Value)); } //InverseQ var iqXml = root.Element("InverseQ"); if (iqXml != null) { QI = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(iqXml.Value)); } //D var dXml = root.Element("D"); if (dXml != null) { D = new Org.BouncyCastle.Math.BigInteger(1, Convert.FromBase64String(dXml.Value)); } RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = null; if (D != null) { rsaPrivateCrtKeyParameters = new RsaPrivateCrtKeyParameters(M, E, D, P, Q, DP, DQ, QI); } RsaKeyParameters pubc = new RsaKeyParameters(false, M, E); var helper = new RsaKeyHelper(pubc, rsaPrivateCrtKeyParameters); return(helper); }