Example #1
0
        /// <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);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Parse message and extract public bank keys -> Encryption, Signing
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="BLZ"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static bool Parse_Segment_RDH_Key(string Message, int BLZ, string UserID)
        {
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var dir       = Path.Combine(documents, Program.Buildname);

            Message = Message.Replace("'?", "||?");

            String[] values = Message.Split('\'');

            List <string> msg = new List <string>();

            foreach (var item in values)
            {
                msg.Add(item + Environment.NewLine.Replace(",", ""));
            }

            string msg_ = string.Join("", msg.ToArray());

            string bpd = "HIBPA" + Parse_String(msg_, "HIBPA", "\r\n" + "HIUPA");
            string upd = "HIUPA" + Parse_String(msg_, "HIUPA", "\r\n" + "HNSHA");

            // BPD
            dir = Path.Combine(dir, "BPD");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (!File.Exists(Path.Combine(dir, "280_" + BLZ + ".bpd")))
            {
                using (File.Create(Path.Combine(dir, "280_" + BLZ + ".bpd")))
                { };

                File.WriteAllText(Path.Combine(dir, "280_" + BLZ + ".bpd"), bpd);
            }
            else
            {
                File.WriteAllText(Path.Combine(dir, "280_" + BLZ + ".bpd"), bpd);
            }

            // UPD
            dir = Path.Combine(documents, Program.Buildname);
            dir = Path.Combine(dir, "UPD");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (!File.Exists(Path.Combine(dir, "280_" + BLZ + "_" + UserID + ".upd")))
            {
                using (File.Create(Path.Combine(dir, "280_" + BLZ + "_" + UserID + ".upd")))
                { };

                File.WriteAllText(Path.Combine(dir, "280_" + BLZ + "_" + UserID + ".upd"), upd);
            }
            else
            {
                File.WriteAllText(Path.Combine(dir, "280_" + BLZ + "_" + UserID + ".upd"), upd);
            }

            foreach (var item in values)
            {
                if (item.Contains("HNHBK"))
                {
                    var ID = Parse_String(item.ToString(), "+1+", ":1");
                    Segment.HNHBK = ID;
                }

                if (item.Contains("HISYN"))
                {
                    var ID = item.Substring(13, item.Length - 13);
                    Segment.HISYN = ID;
                }

                if (item.Contains("HIISA"))
                {
                    if (item.Contains(":V:10"))
                    {
                        var item_ = item.ToString().Replace("||?", "'?");

                        RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK = Parse_String(item_, "@248@", ":12:");
                    }
                }

                if (item.Contains("HIISA"))
                {
                    if (item.Contains(":S:10"))
                    {
                        var item_ = item.ToString().Replace("||?", "'?");

                        RDH_KEYSTORE.KEY_SIGNING_PUBLIC_BANK = Parse_String(item_, "@248@", ":12:");
                    }
                }
            }

            if (!String.IsNullOrEmpty(RDH_KEYSTORE.KEY_ENCRYPTION_PUBLIC_BANK) &&
                !String.IsNullOrEmpty(RDH_KEYSTORE.KEY_SIGNING_PUBLIC_BANK))
            {
                // Update hbci key
                RDHKEY.Update(RDHKEY.RDHKEYFILE, RDHKEY.RDHKEYFILEPWD);

                // Release rdhkey credentials
                RDHKEY.RDHKEYFILE    = string.Empty;
                RDHKEY.RDHKEYFILEPWD = string.Empty;

                return(true);
            }
            else
            {
                // Error
                var BankCode = "HIRMG" + Helper.Parse_String(msg_, "HIRMG", "HNHBS");

                String[] values_ = BankCode.Split('+');

                foreach (var item in values_)
                {
                    if (!item.StartsWith("HIRMG"))
                    {
                        Console.WriteLine(item.Replace("::", ": "));

                        Log.Write(item.Replace("::", ": "));
                    }
                }

                return(false);
            }
        }