private static RSACryptoServiceProvider LoadCertificateFile(string filename) { IDictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("priKeyFile", filename); using (FileStream fileStream = File.OpenRead(filename)) { byte[] array = new byte[fileStream.Length]; byte[] privkey = null; fileStream.Read(array, 0, array.Length); if (array[0] != 48) { privkey = AliOHClientHelper.GetPem("RSA PRIVATE KEY", array); } try { return(AliOHClientHelper.DecodeRSAPrivateKey(privkey)); } catch (Exception ex) { Globals.WriteExceptionLog(ex, dictionary, "LoadCertificateFile"); } return(null); } }
public static AlipayOHClient Instance(string serverRootPath) { SiteSettings masterSettings = SettingsManager.GetMasterSettings(); string aliOHServerUrl = masterSettings.AliOHServerUrl; string aliOHAppId = masterSettings.AliOHAppId; string text = serverRootPath + "/config/rsa_private_key.pem"; string aliPubKey = serverRootPath + "/config/alipay_pubKey.pem"; string pubKey = serverRootPath + "/config/rsa_public_key.pem"; AliOHClientHelper.LoadCertificateFile(text); return(new AlipayOHClient(aliOHServerUrl, aliOHAppId, aliPubKey, text, pubKey, "UTF-8")); }
private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey) { MemoryStream input = new MemoryStream(privkey); BinaryReader binaryReader = new BinaryReader(input); IDictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("priKeyFile", binaryReader.ReadString().ToNullString()); ushort num = 0; int num2 = 0; try { switch (binaryReader.ReadUInt16()) { case 33072: binaryReader.ReadByte(); break; case 33328: binaryReader.ReadInt16(); break; default: return(null); } if (binaryReader.ReadUInt16() != 258) { return(null); } if (binaryReader.ReadByte() != 0) { return(null); } num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] modulus = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] exponent = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] d = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] p = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] q = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] dP = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] dQ = binaryReader.ReadBytes(num2); num2 = AliOHClientHelper.GetIntegerSize(binaryReader); byte[] inverseQ = binaryReader.ReadBytes(num2); CspParameters parameters = new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }; RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(1024, parameters); RSAParameters rSAParameters = default(RSAParameters); rSAParameters.Modulus = modulus; rSAParameters.Exponent = exponent; rSAParameters.D = d; rSAParameters.P = p; rSAParameters.Q = q; rSAParameters.DP = dP; rSAParameters.DQ = dQ; rSAParameters.InverseQ = inverseQ; RSAParameters parameters2 = rSAParameters; rSACryptoServiceProvider.ImportParameters(parameters2); return(rSACryptoServiceProvider); } catch (Exception ex) { Globals.WriteExceptionLog(ex, dictionary, "DecodeRSAPrivateKey"); return(null); } finally { binaryReader.Close(); } }