public static byte[] SignDataSHA256(string Message) { var message = Encoding.GetEncoding("iso8859-1").GetBytes(Message); SHA256Managed hashString = new SHA256Managed(); var hashValue = hashString.ComputeHash(message); if (DEBUG.Enabled) { DEBUG.Write("Hashed message: " + Converter.ByteArrayToString(hashValue)); } return(hashValue); }
public static byte[] SignMessage(byte[] hash) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(RDH_KEYSTORE.KEY_SIGNING_PRIVATE_XML); var signedMessage = rsa.SignHash(hash, CryptoConfig.MapNameToOID("SHA1withRSA")); if (DEBUG.Enabled) { DEBUG.Write("Signed message: " + Converter.ByteArrayToString(signedMessage)); } return(signedMessage); } }
/// <summary> /// Encrypt RDH-10 message /// </summary> /// <param name="Message"></param> /// <param name="encSessionKey"></param> /// <param name="encMsg"></param> public static void Encrypt(string Message, out byte[] encSessionKey, out byte[] encMsg) { if (DEBUG.Enabled) { DEBUG.Write("Plain message before encryption: " + Message); } if (DEBUG.Enabled) { DEBUG.Write("Plain message length: " + Message.Length); } if (DEBUG.Enabled) { DEBUG.Write("Public bank encryption key: " + Converter.ByteArrayToString(Encoding.GetEncoding("iso8859-1").GetBytes(RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK))); } encSessionKey = encryptKey(Encoding.GetEncoding("iso8859-1").GetBytes(RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK)); encMsg = encryptMessage(Message); }
/// <summary> /// INI RDH /// </summary> public static bool Init_INI_RDH(int BLZ, string URL, int Port, int HBCIVersion, string UserID, string FilePath, string Password) { Log.Write("Starting Synchronisation"); try { string segments; // Get public keys from bank if (HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+" + SEG_Country.Germany + ":" + BLZ + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+11+0+1+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; if (DEBUG.Enabled) { DEBUG.Write("INI message: " + segments); } if (Helper.Parse_Segment_RDH_Key(RDHMessage.Send(URL, Port, RDHMessageAnonymous.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, segments)), BLZ, UserID)) { // Sync OK Log.Write("Synchronisation ok"); if (RDHKEY.OpenFromFile(FilePath, Password)) { segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+" + SEG_Country.Germany + ":" + BLZ + "+" + UserID + "+0+1'" + "HKSAK:" + SEGNUM.SETVal(4) + ":3+2+112+" + RDH_Profile.RDHPROFILE + "+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":" + Keytype.Enc + ":" + RDH_Profile.Version + ":1+5:2:10:@" + RDH_KEYSTORE.KEY_ENCRYPTION_PRIVATE.Length + "@" + RDH_KEYSTORE.KEY_ENCRYPTION_PRIVATE + ":12:@3@" + Converter.FromHexString("01 00 01") + ":13'" + "HKSAK:" + SEGNUM.SETVal(5) + ":3+2+112+" + RDH_Profile.RDHPROFILE + "+" + SEG_Country.Germany + ":" + BLZ + ":" + UserID + ":" + Keytype.Sig + ":" + RDH_Profile.Version + ":1+6:19:10:@" + RDH_KEYSTORE.KEY_SIGNING_PRIVATE.Length + "@" + RDH_KEYSTORE.KEY_SIGNING_PRIVATE + ":12:@3@" + Converter.FromHexString("01 00 01") + ":13'"; segments = segments_; if (DEBUG.Enabled) { DEBUG.Write("Key exchange message: " + segments); } RDHMessage.Send(URL, Port, RDHMessage.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, UserID, SYS.SETVal(0), segments, SEGNUM.SETInt(5))); // INI OK Log.Write("INI ok"); return(true); } else { Log.Write("INI failed"); return(false); } } else { UserID = string.Empty; Log.Write("Initialisation failed"); throw new Exception(HBCI_Exception.INI()); } } else { Log.Write("HBCI version not supported"); throw new Exception(HBCI_Exception.HBCIVERSIONNOTSUPPORTED()); } } catch (Exception ex) { UserID = string.Empty; Log.Write(ex.ToString()); if (DEBUG.Enabled) { DEBUG.Write("Software error: " + ex.ToString()); } // Exception if (!DEBUG.Enabled) { throw new Exception(HBCI_Exception.SOFTWARE() + ex.ToString()); } else { return(false); } } }
/// <summary> /// INI /// </summary> public static async Task <String> Init_INI(FinTsClient client) { var connectionDetails = client.ConnectionDetails; if (!client.Anonymous) { /// <summary> /// Sync /// </summary> try { string segments; /// <summary> /// INI /// </summary> if (connectionDetails.HbciVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + client.SystemId + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + FinTsConfig.ProductId + "+" + FinTsConfig.Version + "'"; segments = segments_; } else if (connectionDetails.HbciVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + client.SystemId + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + FinTsConfig.ProductId + "+" + FinTsConfig.Version + "'"; if (client.HITANS != null && client.HITANS.Substring(0, 3).Equals("6+4")) { client.SEGNUM = SEGNUM.SETInt(5); segments_ = HKTAN.Init_HKTAN(client, segments_); } else { client.SEGNUM = SEGNUM.SETInt(4); } segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } var message = FinTSMessage.Create(client, "1", "0", segments, client.HIRMS); var response = await FinTSMessage.Send(client, message); Helper.Parse_Segment(client, response); client.HITAN = Helper.Parse_String(Helper.Parse_String(response, "HITAN:", "'").Replace("?+", "??"), "++", "+").Replace("??", "?+"); return(response); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); throw new Exception("Software error", ex); } } else { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation anonymous"); string segments; if (connectionDetails.HbciVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+280:" + connectionDetails.BlzPrimary + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+0+0+1+" + FinTsConfig.ProductId + "+" + FinTsConfig.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } client.SEGNUM = SEGNUM.SETInt(4); string message = FinTsMessageAnonymous.Create(connectionDetails.HbciVersion, "1", "0", connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, "0", segments, null, client.SEGNUM); string response = await FinTSMessage.Send(client, message); var messages = Helper.Parse_Segment(client, response); var result = new HBCIDialogResult(messages, response); if (!result.IsSuccess) { Log.Write("Synchronisation anonymous failed. " + result); return(response); } // Sync OK Log.Write("Synchronisation anonymous ok"); /// <summary> /// INI /// </summary> if (connectionDetails.HbciVersion == 300) { string segments__ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + client.SystemId + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + FinTsConfig.ProductId + "+" + FinTsConfig.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments__; } else { Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } client.SEGNUM = SEGNUM.SETInt(5); message = FinTSMessage.Create(client, "1", "0", segments, client.HIRMS); response = await FinTSMessage.Send(client, message); Helper.Parse_Segment(client, response); client.HITAN = Helper.Parse_String(Helper.Parse_String(response, "HITAN:", "'").Replace("?+", "??"), "++", "+").Replace("??", "?+"); return(response); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); DEBUG.Write("Software error: " + ex.ToString()); throw new Exception("Software error: " + ex.ToString()); } } }
/// <summary> /// INI /// </summary> public static bool Init_INI(ConnectionDetails connectionDetails, bool anonymous) { if (!anonymous) { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation"); string segments; if (connectionDetails.HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.Blz + "+" + connectionDetails.UserId + "+0+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":2+0'"; segments = segments_; } else if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.Blz + "+" + connectionDetails.UserId + "+0+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); if (Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, FinTSMessage.Send(connectionDetails.Url, FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, SYS.SETVal(0), segments, null, SEG.NUM)))) { // Sync OK Log.Write("Synchronisation ok"); /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.Blz + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.Blz + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); if (Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, FinTSMessage.Send(connectionDetails.Url, FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM)))) { return(true); } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("Initialisation failed"); throw new Exception("Initialisation failed"); } } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("Sync failed"); return(false); } } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); throw new Exception("Software error"); } } else { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation anonymous"); string segments; if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+280:" + connectionDetails.Blz + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+0+0+1+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); if (Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, FinTSMessage.Send(connectionDetails.Url, FinTSMessageAnonymous.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, SYS.SETVal(0), segments, null, SEG.NUM)))) { // Sync OK Log.Write("Synchronisation anonymous ok"); /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 300) { string segments__ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.Blz + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments__; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); if (Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, FinTSMessage.Send(connectionDetails.Url, FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM)))) { return(true); } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("Initialisation failed"); throw new Exception("Initialisation failed"); } } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("Sync failed"); return(false); } } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); DEBUG.Write("Software error: " + ex.ToString()); throw new Exception("Software error: " + ex.ToString()); } } }
/// <summary> /// INI /// </summary> public static HBCIDialogResult Init_INI(ConnectionDetails connectionDetails, bool anonymous) { if (!anonymous) { /// <summary> /// Sync /// </summary> try { // If there is no customer system ID available, perform sync to get one if (connectionDetails.CustomerSystemId == null) { var BankCode = Transaction.HKSYN(connectionDetails); var syncResult = new HBCIDialogResult(Helper.Parse_BankCode(BankCode)); if (!syncResult.IsSuccess) { Log.Write("Synchronisation failed."); return(syncResult); } } else { Segment.HISYN = connectionDetails.CustomerSystemId; } string segments; /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); var message = FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.BlzPrimary, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM); var response = FinTSMessage.Send(connectionDetails.Url, message); var bankMessages = Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); var result = new HBCIDialogResult(bankMessages); if (!result.IsSuccess) { Log.Write("Initialisation failed: " + result); } return(result); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); throw new Exception("Software error", ex); } } else { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation anonymous"); string segments; if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+280:" + connectionDetails.BlzPrimary + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+0+0+1+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); string message = FinTSMessageAnonymous.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, SYS.SETVal(0), segments, null, SEG.NUM); string response = FinTSMessage.Send(connectionDetails.Url, message); var messages = Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); var result = new HBCIDialogResult(messages); if (!result.IsSuccess) { Log.Write("Synchronisation anonymous failed. " + result); return(result); } // Sync OK Log.Write("Synchronisation anonymous ok"); /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 300) { string segments__ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments__; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); message = FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.BlzPrimary, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM); response = FinTSMessage.Send(connectionDetails.Url, message); messages = Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); result = new HBCIDialogResult(messages); if (!result.IsSuccess) { Log.Write("Initialisation failed."); } return(result); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); DEBUG.Write("Software error: " + ex.ToString()); throw new Exception("Software error: " + ex.ToString()); } } }
/// <summary> /// Create RDH message /// </summary> /// <param name="Version"></param> /// <param name="MsgNum"></param> /// <param name="DialogID"></param> /// <param name="BLZ"></param> /// <param name="UserID"></param> /// <param name="SystemID"></param> /// <param name="Segments"></param> /// <param name="SegmentNum"></param> /// <returns></returns> public static string Create(int Version, string MsgNum, string DialogID, int BLZ, string UserID, string SystemID, string Segments, 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(":", ""); if (Version != 300) { Log.Write("HBCI version not supported"); throw new Exception("HBCI Version not supported"); } sigHead = "HNSHK:" + SEGNUM.SETVal(2) + ":4+" + RDH_Profile.RDHPROFILE + "+2+" + secRef + "+1+1+1::" + SystemID + "+1+1:" + date + ":" + time + "+1:" + Sig.HASHALG_SHA256_SHA256 + ":1+6:" + Sig.SIGALG_RSA + ":" + Sig.SIGMODE_PSS + "+" + SEG_Country.Germany + ":" + RDH_KEYSTORE.BLZ + ":" + RDH_KEYSTORE.UserID + ":" + Keytype.Sig + ":" + RDH_Profile.Version + ":1'"; // TODO: Signature is revoked from bank -> Must be a mistake while implementing var signature = Sig.Sign(Segments); var v = Sig.Verify(Segments, signature); var signedsig = signature; sigTrail = "HNSHA:" + Convert.ToString(SegmentNum + 1) + ":2+" + secRef + "+" + "@" + signedsig.Length + "@" + signedsig + "'"; if (DEBUG.Enabled) { DEBUG.Write("sigTrail: " + sigTrail); } Segments = sigHead + Segments + sigTrail; byte[] encryptedSessionKey = null; byte[] encryptedMessage = null; Crypt.Encrypt(Segments, out encryptedSessionKey, out encryptedMessage); encHead = "HNVSK:" + Enc.SECFUNC_ENC_PLAIN + ":3+" + RDH_Profile.RDHPROFILE + "+4+1+1::" + SystemID + "+1:" + date + ":" + time + "+2:2:" + Enc.ENCALG_2K3DES + ":@" + encryptedSessionKey.Length + "@" + Encoding.GetEncoding("iso8859-1").GetString(encryptedSessionKey) + ":" + Enc.ENC_KEYTYPE_RSA + ":1+" + SEG_Country.Germany + ":" + RDH_KEYSTORE.BLZ + ":0:" + Keytype.Enc + ":" + RDH_Profile.Version + ":1+0'"; if (DEBUG.Enabled) { DEBUG.Write("encHead: " + encHead); } var payload = "HNVSD:999:1+@" + encryptedMessage.Length + "@" + Encoding.GetEncoding("iso8859-1").GetString(encryptedMessage) + "'"; var msgLen = HEAD_LEN + TRAIL_LEN + MsgNum.Length + DialogID.Length + (payload.Length + 1) + encHead.Length; // (payload.Length + 1) = Add one digit for "abschließendes Trennzeichen" var paddedLen = ("000000000000").Substring(0, 12 - Convert.ToString(msgLen).Length) + Convert.ToString(msgLen); var msgHead = "HNHBK:" + SEGNUM.SETVal(1) + ":3+" + paddedLen + "+" + ("300") + "+" + DialogID + "+" + MsgNum + "'"; if (DEBUG.Enabled) { DEBUG.Write("msgHead: " + msgHead); } var msgEnd = "HNHBS:" + Convert.ToString(SegmentNum + 2) + ":1+" + MsgNum + "'"; if (DEBUG.Enabled) { DEBUG.Write("msgEnd: " + msgEnd); } return(msgHead + encHead + payload + msgEnd); }
/// <summary> /// Send RDH message /// </summary> /// <param name="Url"></param> /// <param name="Port"></param> /// <param name="Message"></param> /// <returns></returns> public static string Send(string Url, int Port, string Message) { Log.Write("Connect to HBCI Server"); Log.Write("Url: " + Url); Log.Write("Port: " + Port); if (Trace.Enabled) { Trace.Write(Message); } if (DEBUG.Enabled) { DEBUG.Write("Message send: " + Message); } try { IPAddress[] ipaddress = Dns.GetHostAddresses(Url); string ip3 = string.Empty; foreach (IPAddress ip2 in ipaddress) { ip3 = ip2.ToString(); } IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ip3), Port); // Init and connect to client TcpClient client = new TcpClient(); client.Connect(Url, Port); // Stream string to server // input += "\n"; Stream stream = client.GetStream(); byte[] @byte = Encoding.GetEncoding("iso8859-1").GetBytes(Message); stream.Write(@byte, 0, @byte.Length); // Read response from server. // Provide enough buffer byte[] buffer = new byte[16384]; System.Threading.Thread.Sleep(1000); int bytesRead = stream.Read(buffer, 0, buffer.Length); var response = Encoding.GetEncoding("iso8859-1").GetString(buffer, 0, bytesRead); client.Close(); string RDHMessage = response; if (Trace.Enabled) { Trace.Write(RDHMessage); } if (DEBUG.Enabled) { DEBUG.Write("Message received: " + RDHMessage); } return(RDHMessage); } catch (Exception ex) { Log.Write(ex.ToString()); if (DEBUG.Enabled) { DEBUG.Write(ex.ToString()); } return(string.Empty); } }
/// <summary> /// INI /// </summary> public static string Init_INI(ConnectionDetails connectionDetails, bool anonymous) { if (!anonymous) { /// <summary> /// Sync /// </summary> try { string segments; SEG.NUM = SEGNUM.SETInt(5); /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.ProductId + "+" + Program.Version + "'"; segments = segments_; } else if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.ProductId + "+" + Program.Version + "'"; if (Segment.HITANS != null && Segment.HITANS.Substring(0, 3).Equals("6+4")) { segments_ = HKTAN.Init_HKTAN(segments_); } segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } var message = FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.BlzPrimary, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM); var response = FinTSMessage.Send(connectionDetails.Url, message); Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); Segment.HITAN = Helper.Parse_String(Helper.Parse_String(response, "HITAN:", "'").Replace("?+", "??"), "++", "+").Replace("??", "?+"); return(response); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); throw new Exception("Software error", ex); } } else { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation anonymous"); string segments; if (connectionDetails.HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+280:" + connectionDetails.BlzPrimary + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+0+0+1+" + Program.ProductId + "+" + Program.Version + "'"; segments = segments_; } else { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); string message = FinTSMessageAnonymous.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.Blz, connectionDetails.UserId, connectionDetails.Pin, SYS.SETVal(0), segments, null, SEG.NUM); string response = FinTSMessage.Send(connectionDetails.Url, message); var messages = Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); var result = new HBCIDialogResult(messages, response); if (!result.IsSuccess) { Log.Write("Synchronisation anonymous failed. " + result); return(response); } // Sync OK Log.Write("Synchronisation anonymous ok"); /// <summary> /// INI /// </summary> if (connectionDetails.HBCIVersion == 300) { string segments__ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + connectionDetails.BlzPrimary + "+" + connectionDetails.UserId + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.ProductId + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments__; } else { Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); message = FinTSMessage.Create(connectionDetails.HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), connectionDetails.BlzPrimary, connectionDetails.UserId, connectionDetails.Pin, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM); response = FinTSMessage.Send(connectionDetails.Url, message); Helper.Parse_Segment(connectionDetails.UserId, connectionDetails.Blz, connectionDetails.HBCIVersion, response); Segment.HITAN = Helper.Parse_String(Helper.Parse_String(response, "HITAN:", "'").Replace("?+", "??"), "++", "+").Replace("??", "?+"); return(response); } catch (Exception ex) { //Since connectionDetails is a re-usable object, this shouldn't be cleared. //connectionDetails.UserId = string.Empty; //connectionDetails.Pin = null; Log.Write(ex.ToString()); DEBUG.Write("Software error: " + ex.ToString()); throw new Exception("Software error: " + ex.ToString()); } } }
/// <summary> /// INI /// </summary> public static bool Init_INI(int BLZ, string URL, int HBCIVersion, string UserID, string PIN, bool Anonymous) { if (!Anonymous) { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation"); string segments; if (HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + BLZ + "+" + UserID + "+0+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":2+0'"; segments = segments_; } else if (HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + BLZ + "+" + UserID + "+0+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments_; } else { UserID = string.Empty; PIN = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); if (Helper.Parse_Segment(UserID, BLZ, HBCIVersion, FinTSMessage.Send(URL, FinTSMessage.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, UserID, PIN, SYS.SETVal(0), segments, null, SEG.NUM)))) { // Sync OK Log.Write("Synchronisation ok"); /// <summary> /// INI /// </summary> if (HBCIVersion == 220) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + BLZ + "+" + UserID + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":2+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else if (HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + BLZ + "+" + UserID + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { UserID = string.Empty; PIN = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); if (Helper.Parse_Segment(UserID, BLZ, HBCIVersion, FinTSMessage.Send(URL, FinTSMessage.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, UserID, PIN, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM)))) { return(true); } else { UserID = string.Empty; PIN = null; Log.Write("Initialisation failed"); throw new Exception("Initialisation failed"); } } else { UserID = string.Empty; PIN = null; Log.Write("Sync failed"); return(false); } } catch (Exception ex) { UserID = string.Empty; PIN = null; Log.Write(ex.ToString()); throw new Exception("Software error"); } } else { /// <summary> /// Sync /// </summary> try { Log.Write("Starting Synchronisation anonymous"); string segments; if (HBCIVersion == 300) { string segments_ = "HKIDN:" + SEGNUM.SETVal(2) + ":2+280:" + BLZ + "+" + "9999999999" + "+0+0'" + "HKVVB:" + SEGNUM.SETVal(3) + ":3+0+0+1+" + Program.Buildname + "+" + Program.Version + "'"; segments = segments_; } else { UserID = string.Empty; PIN = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(4); if (Helper.Parse_Segment(UserID, BLZ, HBCIVersion, FinTSMessage.Send(URL, FinTSMessageAnonymous.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, UserID, PIN, SYS.SETVal(0), segments, null, SEG.NUM)))) { // Sync OK Log.Write("Synchronisation anonymous ok"); /// <summary> /// INI /// </summary> if (HBCIVersion == 300) { string segments__ = "HKIDN:" + SEGNUM.SETVal(3) + ":2+280:" + BLZ + "+" + UserID + "+" + Segment.HISYN + "+1'" + "HKVVB:" + SEGNUM.SETVal(4) + ":3+0+0+0+" + Program.Buildname + "+" + Program.Version + "'" + "HKSYN:" + SEGNUM.SETVal(5) + ":3+0'"; segments = segments__; } else { UserID = string.Empty; PIN = null; Log.Write("HBCI version not supported"); throw new Exception("HBCI version not supported"); } SEG.NUM = SEGNUM.SETInt(5); if (Helper.Parse_Segment(UserID, BLZ, HBCIVersion, FinTSMessage.Send(URL, FinTSMessage.Create(HBCIVersion, MSG.SETVal(1), DLG.SETVal(0), BLZ, UserID, PIN, Segment.HISYN, segments, Segment.HIRMS, SEG.NUM)))) { return(true); } else { UserID = string.Empty; PIN = null; Log.Write("Initialisation failed"); throw new Exception("Initialisation failed"); } } else { UserID = string.Empty; PIN = null; Log.Write("Sync failed"); return(false); } } catch (Exception ex) { UserID = string.Empty; PIN = null; Log.Write(ex.ToString()); if (DEBUG.Enabled) { DEBUG.Write("Software error: " + ex.ToString()); } throw new Exception("Software error: " + ex.ToString()); } } }
/// <summary> /// Encrpyt DES3 key /// </summary> /// <param name="Key"></param> /// <returns></returns> static byte[] encryptKey(byte[] Key) { sessionKey = InitDES3Key(); if (DEBUG.Enabled) { DEBUG.Write("Public key length: " + Key.Length); } var Exponent = new byte[] { 1, 0, 1 }; var key = Encoding.GetEncoding("iso8859-1").GetBytes(RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK); BigInteger n = new BigInteger(key); int cryptDataSize = n.BitLength; byte[] plainText = new byte[cryptDataSize]; Array.Copy(sessionKey, 0, plainText, plainText.Length - 16, 16); BigInteger m = new BigInteger(plainText); BigInteger ex = new BigInteger(Exponent); BigInteger mo = new BigInteger(+1, key); var v = m.ModPow(ex, mo); byte[] result = v.ToByteArray(); if (DEBUG.Enabled) { DEBUG.Write("Encrypted session key: " + Converter.ByteArrayToString(result)); } if (DEBUG.Enabled) { DEBUG.Write("Encrypted session key length: " + result.Length); } // Check for encrypted session key size var cryptLength = HBCI_Util.checkForCryptSize(RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK.Length, result.Length); if (DEBUG.Enabled) { DEBUG.Write("Crypted session key length is valid: " + cryptLength.ToString()); } if (Trace.Enabled) { Trace.Write("Session key length: " + result.Length); } // Throw exception when size is not valid if (!cryptLength) { throw new Exception(HBCI_Exception.CRYPTEDLENGTH()); } return(result); }