Пример #1
0
        protected void BuildSSO()
        {
            try
            {
                xmlPublicKey  = GetPublicKey();
                xmlPrivateKey = GetPrivateKey();

                if (xmlPublicKey == null)
                {
                    LogError("Unable to retrieve Public Key");
                    return;
                }

                if (xmlPrivateKey == null)
                {
                    LogError("Unable to retrieve Private Key");
                    return;
                }

                CspParameters            Sendercp  = new CspParameters();
                RSACryptoServiceProvider SenderRSA = null;

                try
                {
                    Sendercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    SenderRSA      = new RSACryptoServiceProvider(Sendercp);
                    SenderRSA.FromXmlString(xmlPrivateKey.OuterXml);
                }
                catch (Exception ex)
                {
                    SenderRSA.Clear();
                    LogError("Error while constructing the PrivateKey: " + ex.Message);
                    return;
                }

                CspParameters            Receivercp  = new CspParameters();
                RSACryptoServiceProvider ReceiverRSA = null;

                try
                {
                    Receivercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    ReceiverRSA      = new RSACryptoServiceProvider(Receivercp);
                    ReceiverRSA.FromXmlString(xmlPublicKey.OuterXml);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    LogError("Error while constructing the PublicKey: " + ex.Message);
                    return;
                }

                string tpayload = AssemblePayload();

                byte[] TripleDESKey  = null;
                byte[] encryptedData = SSOEncryption.TripleDESEncrypt(Encoding.ASCII.GetBytes(tpayload), out TripleDESKey);

                string base64enc = Convert.ToBase64String(encryptedData);

                string plainsig = base64enc + _clientID + _serverID;

                MD5    md     = null;
                byte[] buffer = null;
                byte[] hash   = null;

                try
                {
                    md = MD5CryptoServiceProvider.Create();

                    //Convert the string into an array of bytes.
                    buffer = Encoding.ASCII.GetBytes(plainsig);

                    //Create the hash value from the array of bytes.
                    hash = md.ComputeHash(buffer);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Computing Hash: " + ex.Message);
                    return;
                }

                byte[] signed;
                byte[] cipher;

                try
                {
                    signed = SenderRSA.SignHash(hash, CryptoConfig.MapNameToOID("MD5"));
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Signing Hash: " + ex.Message);
                    return;
                }

                try
                {
                    cipher = ReceiverRSA.Encrypt(TripleDESKey, false);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Encrypting Symmetric Key: " + ex.Message);
                    return;
                }

                string base64sign   = Convert.ToBase64String(signed);
                string base64cipher = Convert.ToBase64String(cipher);


                _payload   = webcode(base64enc);
                _signature = webcode(base64sign);

                //byte[] EncBinarySig = Convert.FromBase64String(webDecode(_signature));

                _cipher = webcode(base64cipher);

                ReceiverRSA.Clear();
                SenderRSA.Clear();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while Building the SSO Package: \n" + ex.Message);
            }
        }
Пример #2
0
        public void DisAssemble()
        {
            try
            {
                xmlPublicKey  = GetPublicKey();
                xmlPrivateKey = GetPrivateKey();

                if (xmlPublicKey == null)
                {
                    LogError("Unable to retrieve Public Key");
                    return;
                }

                if (xmlPrivateKey == null)
                {
                    LogError("Unable to retrieve Private Key");
                    return;
                }


                CspParameters            Sendercp  = new CspParameters();
                RSACryptoServiceProvider SenderRSA = null;

                try
                {
                    Sendercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    SenderRSA      = new RSACryptoServiceProvider(Sendercp);
                    SenderRSA.FromXmlString(xmlPublicKey.OuterXml);
                }
                catch (Exception ex)
                {
                    SenderRSA.Clear();
                    LogError("Error while constructing the PublicKey: " + ex.Message);
                    return;
                }

                CspParameters            Receivercp  = new CspParameters();
                RSACryptoServiceProvider ReceiverRSA = null;

                try
                {
                    Receivercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    ReceiverRSA      = new RSACryptoServiceProvider(Receivercp);
                    ReceiverRSA.FromXmlString(xmlPrivateKey.OuterXml);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    LogError("Error while constructing the PrivateKey: " + ex.Message);
                    return;
                }

                // This is where I am having problems. SenderRSA.Decrypt returns an error of "Bad key".
                byte[] EncBinarySig = Convert.FromBase64String(webDecode(_signature));
                //byte[] transmittedHash = SenderRSA.Decrypt(EncBinarySig,false);

                string nameValuePairText = webDecode(_payload);
                string transmittedMsg    = nameValuePairText + _clientID + _serverID;
                string calculatedHash    = nameValuePairText + ClientID + ServerID;
                byte[] hash;
                byte[] signed = null;

                signed = Convert.FromBase64String(webDecode(_signature));

                MD5 md2 = null;

                try
                {
                    md2 = MD5CryptoServiceProvider.Create();

                    //Convert the string into an array of bytes.
                    byte[] buffer = Encoding.ASCII.GetBytes(calculatedHash);

                    //Create the hash value from the array of bytes.
                    hash = md2.ComputeHash(buffer);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Computing Hash: " + ex.Message);
                    return;
                }

                if (SenderRSA.VerifyHash(hash, CryptoConfig.MapNameToOID("MD5"), signed))
                {
                    byte[] TripleDESKey = null;

                    string encryptedKeyText   = webDecode(_cipher);
                    byte[] encryptedKeyBinary = Convert.FromBase64String(encryptedKeyText);
                    TripleDESKey = ReceiverRSA.Decrypt(encryptedKeyBinary, false);

                    byte[] nameValuePairBinary = Convert.FromBase64String(nameValuePairText);
                    byte[] nameValuePair       = SSOEncryption.TripleDESDecrypt(nameValuePairBinary, TripleDESKey, null);
                    string ClearTextPayload    = Encoding.ASCII.GetString(nameValuePair);

                    //LogError("Error matching Computed and Transmitted Hash");
                    //return;
                }



                ReceiverRSA.Clear();
                SenderRSA.Clear();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while Building the SSO Package: \n" + ex.Message);
            }
        }
Пример #3
0
        protected void BuildSSO()
        {
            try
            {
                xmlPublicKey  = GetPublicKey();
                xmlPrivateKey = GetPrivateKey();

                if (xmlPublicKey == null)
                {
                    LogError("Unable to retrieve Public Key");
                    return;
                }

                if (xmlPrivateKey == null)
                {
                    LogError("Unable to retrieve Private Key");
                    return;
                }

                CspParameters            Sendercp  = new CspParameters();
                RSACryptoServiceProvider SenderRSA = null;

                try
                {
                    Sendercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    SenderRSA      = new RSACryptoServiceProvider(Sendercp);
                    SenderRSA.FromXmlString(xmlPrivateKey.OuterXml);
                }
                catch (Exception ex)
                {
                    SenderRSA.Clear();
                    LogError("Error while constructing the PrivateKey: " + ex.Message);
                    return;
                }

                CspParameters            Receivercp  = new CspParameters();
                RSACryptoServiceProvider ReceiverRSA = null;

                try
                {
                    Receivercp.Flags = CspProviderFlags.UseMachineKeyStore;
                    ReceiverRSA      = new RSACryptoServiceProvider(Receivercp);
                    ReceiverRSA.FromXmlString(xmlPublicKey.OuterXml);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    LogError("Error while constructing the PublicKey: " + ex.Message);
                    return;
                }

                string tpayload = AssemblePayload();

                byte[] TripleDESKey  = null;
                byte[] encryptedData = SSOEncryption.TripleDESEncrypt(Encoding.ASCII.GetBytes(tpayload), out TripleDESKey);

                string base64enc = Convert.ToBase64String(encryptedData);

                string plainsig = base64enc + _clientID + _serverID;

                MD5    md     = null;
                byte[] buffer = null;
                byte[] hash   = null;

                try
                {
                    md = MD5CryptoServiceProvider.Create();

                    //Convert the string into an array of bytes.
                    buffer = Encoding.ASCII.GetBytes(plainsig);

                    //Create the hash value from the array of bytes.
                    hash = md.ComputeHash(buffer);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Computing Hash: " + ex.Message);
                    return;
                }

                byte[] signed;
                byte[] cipher;

                try
                {
                    signed = SenderRSA.SignHash(hash, CryptoConfig.MapNameToOID("MD5"));
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Signing Hash: " + ex.Message);
                    return;
                }

                try
                {
                    cipher = ReceiverRSA.Encrypt(TripleDESKey, false);
                }
                catch (Exception ex)
                {
                    ReceiverRSA.Clear();
                    SenderRSA.Clear();
                    LogError("Error Encrypting Symmetric Key: " + ex.Message);
                    return;
                }

                string base64sign   = Convert.ToBase64String(signed);
                string base64cipher = Convert.ToBase64String(cipher);


                _payload   = webcode(base64enc);
                _signature = webcode(base64sign);

                //byte[] EncBinarySig = Convert.FromBase64String(webDecode(_signature));

                _cipher = webcode(base64cipher);

                WebClient           wc   = new WebClient();
                NameValueCollection vars = new NameValueCollection();
                vars.Add("Payload", webcode(base64enc));
                vars.Add("Signature", webcode(base64sign));
                vars.Add("Cipher", webcode(base64cipher));
                vars.Add("ClientID", _clientID);
                vars.Add("ServerID", _serverID);
                vars.Add("SenderKeyVersion", _senderKeyVersion);
                vars.Add("ReceiverKeyVersion", _receiverKeyVersion);

                ShellWindows shWin = new ShellWindows();

                object o = null;
                object v = (object)vars;
                SHDocVw.InternetExplorer IE = new InternetExplorerClass();
                //IWebBrowserApp wb = (IWebBrowserApp)IE;
                ////wb.FullName = "AnswerKey";
                //wb.Visible = true;
                //wb.Navigate("http://www.c-sharpcorner.com/", ref o, ref o, ref o, ref o);
                //IE.Name
                bool b = false;

                foreach (InternetExplorer ies in shWin)
                {
                    if (ies.HWND == IE.HWND)
                    {
                        IE.Visible = true;
                        IE.Navigate("http://www.c-sharpcorner.com/", ref o, ref o, ref o, ref o);
                    }
                    else
                    {
                        IE         = new InternetExplorerClass();
                        IE.Visible = true;
                        IE.Navigate("http://www.codeproject.com/", ref o, ref o, ref o, ref o);
                    }
                }



                //IE.Visible = true;
                //IE.Navigate("https://sso.ehr.com/Destination.aspx", ref o, ref o, ref v, ref o);
                ////MessageBox.Show(IE.Name.ToString());

                //int hndl = IE.HWND;



                //byte[] retBytes = wc.UploadValues("https://sso.ehr.com/Destination.aspx", vars);

                //string retASCII = Encoding.ASCII.GetString(retBytes);

                ReceiverRSA.Clear();
                SenderRSA.Clear();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while Building the SSO Package: \n" + ex.Message);
            }
        }