public bool RetrieveKeys() { //retrieve file settings keys char ctype = trxType.Substring(4, 1)[0]; byte[] bKey = null; object objKey = null; string ftpPath = System.Configuration.ConfigurationManager.AppSettings["ftpFolder"]; string dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Hours.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Minutes.ToString().PadLeft(2, '0'); Letters.stpCaller sCaller = new Letters.stpCaller(); List <object> arrParams = new List <object>(); arrParams.Add("GetKeys"); arrParams.Add(clientID); arrParams.Add(ctype); //0-Export, 1-Import arrParams.Add(-1); //triggerID arrParams.Add(-1); //encType arrParams.Add(DateTime.Now); System.Data.DataSet theDS = sCaller.executeSPDataSet(arrParams, Letters.stpCaller.stpType.TriggerUpdateKey); if (theDS != null) { for (int ix = 0; ix < theDS.Tables[0].Rows.Count; ix++) { objKey = theDS.Tables[0].Rows[ix]["KeySettings"]; bKey = (byte[])objKey; Int16 kType = Convert.ToInt16(theDS.Tables[0].Rows[ix]["KeyType"]); if (kType == (Int16)MYOB_TRXTYPE.EXPORT) { exportSettingsFileName = clientID.ToString() + "_E" + dateStamp + ".txt"; if (System.IO.File.Exists(ftpPath + exportSettingsFileName)) { System.IO.File.Delete(ftpPath + exportSettingsFileName); } System.IO.File.WriteAllBytes(ftpPath + exportSettingsFileName, bKey); } else if (kType == (Int16)MYOB_TRXTYPE.IMPORT) { importSettingsFileName = clientID.ToString() + "_I" + dateStamp + ".txt"; if (System.IO.File.Exists(ftpPath + importSettingsFileName)) { System.IO.File.Delete(ftpPath + importSettingsFileName); } System.IO.File.WriteAllBytes(ftpPath + importSettingsFileName, bKey); } } } return(true); }
public bool isAuthenticated(bool genMYOBXML) { string ftpPath = System.Configuration.ConfigurationManager.AppSettings["ftpFolder"]; string dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Hours.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Minutes.ToString().PadLeft(2, '0'); string strMsg = "authenticating started at [" + dateStamp + "]" + Environment.NewLine; if (System.Configuration.ConfigurationManager.AppSettings["DebugOn"] == "true") { System.IO.File.WriteAllText(ftpPath + "debug.txt", strMsg); } try { if (genMYOBXML) { //authenticate user based on clientid and passkey string strEncType = StringEnum.GetStringValue(MYOB_ENCTYPES.TRIPLE_DES); string strKey = ""; //string strDeciphered = ""; string[] strCiphered = null; byte[] rjkey = null; byte[] rjIV = null; object objKey = null; Letters.stpCaller sCaller = new Letters.stpCaller(); List <object> arrParams = new List <object>(); arrParams.Add("FindPasskey"); arrParams.Add(clientID); arrParams.Add(0); //0-Export, 1-Import arrParams.Add(0); arrParams.Add(passKey); System.Data.DataSet theDS = sCaller.executeSPDataSet(arrParams, Letters.stpCaller.stpType.authenticateUser); if (theDS == null) { return(false); } if (theDS.Tables[0].Rows.Count == 0) { return(false); } if (theDS.Tables.Count > 0 && theDS.Tables[1].Rows.Count > 0) { strEncType = theDS.Tables[1].Rows[0]["keyTypeDesc"].ToString(); if (theDS.Tables[1].Rows[0]["Key1"] != null) { strKey = theDS.Tables[1].Rows[0]["Key1"].ToString(); } if (strEncType.ToUpper() == StringEnum.GetStringValue(MYOB_ENCTYPES.RIJNDAEL)) { objKey = theDS.Tables[1].Rows[0]["Key2"]; rjkey = (byte[])objKey; objKey = theDS.Tables[1].Rows[0]["Key3"]; rjIV = (byte[])objKey; } } if (System.Configuration.ConfigurationManager.AppSettings["DebugOn"] == "true") { System.IO.File.WriteAllText(ftpPath + "debug.txt", "passkey found for " + clientID.ToString() + Environment.NewLine); } //user exists, retrieve cff data exportXML xmlExport = new exportXML(clientID, cardType, trxType, crdTrxFrom, crdTrxTo, strIDs); if (xmlExport.retrieveData()) { //generate myobxml dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Hours.ToString().PadLeft(2, '0') + DateTime.Now.TimeOfDay.Minutes.ToString().PadLeft(2, '0'); //retrieve the keys from exkeys table - always encrypt this for security reason BLL.CryptoClass xCrypt = new BLL.CryptoClass(strEncType, rjkey, rjIV, ""); if (strEncType == StringEnum.GetStringValue(MYOB_ENCTYPES.TRIPLE_DES)) { strEncType = "0"; } else { strEncType = "1"; } this.xmlFileName = clientID.ToString() + "_" + dateStamp + "_" + strEncType + ".txt"; //+ ".zip"; // this.xmlFileName = this.xmlFileName.Replace(" ", ""); string xmlPath = ftpPath + this.xmlFileName; string XMLData = xmlExport.createXMLData().ToString(); bool isWritten = xCrypt.Encrypt(XMLData, xmlPath, ref strCiphered); //write encrypted text to file xmlPath = BLL.Compression.CompressFile(xmlPath, true); //compress to zip file this.xmlFileName += ".zip"; return(isWritten); } } else { //authenticate user without generating MYOB File Letters.stpCaller sCaller = new Letters.stpCaller(); List <object> arrParams = new List <object>(); arrParams.Add("FindPasskey"); arrParams.Add(clientID); arrParams.Add(0); //0-Export, 1-Import arrParams.Add(0); arrParams.Add(passKey); System.Data.DataSet theDS = sCaller.executeSPDataSet(arrParams, Letters.stpCaller.stpType.authenticateUser); if (theDS == null) { return(false); } if (theDS.Tables[0].Rows.Count == 0) { return(false); } if (theDS.Tables.Count > 0 && theDS.Tables[1].Rows.Count > 0) { return(true); } } } catch (Exception exc) { if (System.Configuration.ConfigurationManager.AppSettings["DebugOn"] == "true") { strMsg += "Exception encountered as of [" + dateStamp + "] - " + exc.Message + Environment.NewLine; System.IO.File.WriteAllText(ftpPath + "debug.txt", strMsg); } return(false); } return(true); }