コード例 #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);
                }
            }
        }
コード例 #2
0
ファイル: RDHXCrypt.cs プロジェクト: codebude/libfintx
        /// <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);
        }