public static string Create(int Version, string MsgNum, string DialogID, int BLZ, string UserID, string PIN, string SystemID, string Segments, string TAN, int SegmentNum) { if (String.IsNullOrEmpty(MsgNum)) { MsgNum = "1"; } MsgNum += ""; DialogID += ""; var HEAD_LEN = 29; var TRAIL_LEN = 11; Random Rnd = new Random(); int RndNr = Rnd.Next(); var encHead = string.Empty; var sigHead = string.Empty; var sigTrail = string.Empty; var secRef = Math.Round(Convert.ToDecimal(RndNr.ToString().Replace("-", "")) * 999999 + 1000000); string date = Convert.ToString(DateTime.Now.Year) + DateTime.Now.ToString("MM") + DateTime.Now.ToString("dd"); string time = Convert.ToString(DateTime.Now.TimeOfDay).Substring(0, 8).Replace(":", ""); string TAN_ = string.Empty; if (TAN != null) { if (TAN.Length > 3) { TAN_ = TAN.Substring(3, 7); TAN = TAN.Substring(0, 3); } } if (Version == 220) { encHead = "HNVSK:" + Enc.SECFUNC_ENC_PLAIN + ":2+998+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:13:@8@00000000:5:1+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":V:0:0+0'"; Log.Write(encHead.Replace(UserID, "XXXXXX")); sigHead = string.Empty; if (TAN == null) { sigHead = "HNSHK:2:3+" + "900" + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } else { sigHead = "HNSHK:2:3+" + TAN + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX").Replace(TAN, "XXXXXX")); } if (String.IsNullOrEmpty(TAN_)) { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + PIN + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + "XXXXXX" + "'"); } else { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + PIN + TAN_ + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + "XXXXXX" + "XXXXXX" + "'"); } } else if (Version == 300) { encHead = "HNVSK:998:3+PIN:1+998+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:13:@8@00000000:5:1+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":V:0:0+0'"; Log.Write(encHead.Replace(UserID, "XXXXXX")); if (TAN == null) { sigHead = "HNSHK:2:4+PIN:1+" + "999" + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } else { sigHead = "HNSHK:2:4+PIN:2+" + TAN + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX").Replace(TAN, "XXXXXX")); } if (String.IsNullOrEmpty(TAN_)) { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + PIN + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + "XXXXXX" + "'"); } else { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + PIN + TAN_ + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + "XXXXXX" + "XXXXXX" + "'"); } } else { Log.Write("HBCI version not supported"); return(string.Empty); } Segments = sigHead + Segments + sigTrail; var payload = Helper.Encrypt(Segments); if (TAN == null) { Log.Write(payload.Replace(UserID, "XXXXXX").Replace(PIN, "XXXXXX")); } else { Log.Write(payload.Replace(UserID, "XXXXXX").Replace(PIN, "XXXXXX").Replace(TAN, "XXXXXX")); } var msgLen = HEAD_LEN + TRAIL_LEN + MsgNum.Length * 2 + DialogID.Length + payload.Length + encHead.Length; var paddedLen = ("000000000000").Substring(0, 12 - Convert.ToString(msgLen).Length) + Convert.ToString(msgLen); var msgHead = string.Empty; if (Version == 220) { msgHead = "HNHBK:1:3+" + paddedLen + "+" + ("220") + "+" + DialogID + "+" + MsgNum + "'"; Log.Write(msgHead); } else if (Version == 300) { msgHead = "HNHBK:1:3+" + paddedLen + "+" + ("300") + "+" + DialogID + "+" + MsgNum + "'"; Log.Write(msgHead); } else { Log.Write("HBCI version not supported"); return(string.Empty); } var msgEnd = "HNHBS:" + Convert.ToString(SegmentNum + 2) + ":1+" + MsgNum + "'"; Log.Write(msgEnd); UserID = string.Empty; PIN = null; return(msgHead + encHead + payload + msgEnd); }
/// <summary> /// Create FinTS message /// </summary> /// <param name="client"></param> /// <param name="MsgNum"></param> /// /// <param name="DialogID"></param> /// <param name="Segments"></param> /// /// <param name="HIRMS_TAN"></param> /// <param name="SegmentNum"></param> /// /// <param name="SystemID"></param> /// <returns></returns> /// /// (iwen65) First redesign to make things easier and more readable. All important params were values that had been stored as properties of the FinTsClient /// If this is connected as close as this it is better to pass the client as ref parameter, that makes the hole method much more flexible and extensible /// without breaking changes. /// ConnectionDetails are a part of the client too. We can handle the new added ServiceProtocolType inside the common connectioninfo without breaking changes too. /// Mostly it will be TLS12 but who knows. /// I'm pretty sure the method can be simplified even more. /// public static string Create(FinTsClient client, string MsgNum, string DialogID, string Segments, string HIRMS_TAN, string SystemID = null) { int Version = client.ConnectionDetails.HbciVersion; int BLZ = client.ConnectionDetails.BlzPrimary; string UserID = client.ConnectionDetails.UserId; string PIN = client.ConnectionDetails.Pin; int SegmentNum = client.SEGNUM; if (SystemID == null) { SystemID = client.SystemId; } if (String.IsNullOrEmpty(MsgNum)) { MsgNum = "1"; } DialogID += ""; var HEAD_LEN = 29; var TRAIL_LEN = 11; Random Rnd = new Random(); int RndNr = Rnd.Next(); var encHead = string.Empty; var sigHead = string.Empty; var sigTrail = string.Empty; var secRef = Math.Round(Convert.ToDecimal(RndNr.ToString().Replace("-", "")) * 999999 + 1000000); string date = Convert.ToString(DateTime.Now.Year) + DateTime.Now.ToString("MM") + DateTime.Now.ToString("dd"); string time = Convert.ToString(DateTime.Now.TimeOfDay).Substring(0, 8).Replace(":", ""); string TAN_ = string.Empty; if (HIRMS_TAN != null) { if (HIRMS_TAN.Length >= 10) { var split = HIRMS_TAN.Split(':'); if (split.Length == 2) { HIRMS_TAN = split[0]; TAN_ = ":" + split[1]; } } } if (Version == 220) { encHead = "HNVSK:" + Enc.SECFUNC_ENC_PLAIN + ":2+998+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:13:@8@00000000:5:1+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":V:0:0+0'"; Log.Write(encHead.Replace(UserID, "XXXXXX")); sigHead = string.Empty; if (HIRMS_TAN == null) { sigHead = "HNSHK:2:3+" + "900" + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } else { sigHead = "HNSHK:2:3+" + HIRMS_TAN + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } if (String.IsNullOrEmpty(TAN_)) { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + PIN + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + "XXXXXX" + "'"); } else { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + PIN + TAN_ + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":1+" + secRef + "++" + "XXXXXX" + "XXXXXX" + "'"); } } else if (Version == 300) { if (HIRMS_TAN == null) { encHead = "HNVSK:998:3+PIN:1+" + Enc.SECFUNC_ENC_PLAIN + "+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:13:@8@00000000:5:1+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":V:0:0+0'"; } else { encHead = "HNVSK:998:3+PIN:2+" + Enc.SECFUNC_ENC_PLAIN + "+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:13:@8@00000000:5:1+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":V:0:0+0'"; } Log.Write(encHead.Replace(UserID, "XXXXXX")); if (HIRMS_TAN == null) { sigHead = "HNSHK:2:4+PIN:1+" + Sig.SECFUNC_SIG_PT_1STEP + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } else { var SECFUNC = HIRMS_TAN.Equals("999") ? "1" : "2"; sigHead = "HNSHK:2:4+PIN:" + SECFUNC + "+" + HIRMS_TAN + "+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:999:1+6:10:16+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":S:0:0'"; Log.Write(sigHead.Replace(UserID, "XXXXXX")); } if (String.IsNullOrEmpty(TAN_)) { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + PIN + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + "XXXXXX" + "'"); } else { sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + PIN + TAN_ + "'"; Log.Write("HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "++" + "XXXXXX" + "XXXXXX" + "'"); } } else { Log.Write("HBCI version not supported"); return(string.Empty); } Segments = sigHead + Segments + sigTrail; var payload = Helper.Encrypt(Segments); if (HIRMS_TAN == null) { Log.Write(payload.Replace(UserID, "XXXXXX").Replace(PIN, "XXXXXX")); } else if (!String.IsNullOrEmpty(TAN_)) { Log.Write(payload.Replace(UserID, "XXXXXX").Replace(PIN, "XXXXXX").Replace(TAN_, "XXXXXX")); } var msgLen = HEAD_LEN + TRAIL_LEN + MsgNum.Length * 2 + DialogID.Length + payload.Length + encHead.Length; var paddedLen = ("000000000000").Substring(0, 12 - Convert.ToString(msgLen).Length) + Convert.ToString(msgLen); var msgHead = string.Empty; if (Version == 220) { msgHead = "HNHBK:1:3+" + paddedLen + "+" + ("220") + "+" + DialogID + "+" + MsgNum + "'"; Log.Write(msgHead); } else if (Version == 300) { msgHead = "HNHBK:1:3+" + paddedLen + "+" + ("300") + "+" + DialogID + "+" + MsgNum + "'"; Log.Write(msgHead); } else { Log.Write("HBCI version not supported"); return(string.Empty); } var msgEnd = "HNHBS:" + Convert.ToString(SegmentNum + 2) + ":1+" + MsgNum + "'"; Log.Write(msgEnd); UserID = string.Empty; PIN = null; return(msgHead + encHead + payload + msgEnd); }