Exemplo n.º 1
0
        public static string GetConnectionString()
        {
            string str = "";

            Aes.KeySize keysize;

            keysize = Aes.KeySize.Bits128;
            byte[] cipherText = new byte[16];
            byte[] decipheredText = new byte[16];
            FileInfo inf = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "settings.dat");
            if (inf.Exists)
            {
                StreamReader reader = inf.OpenText();
                gConnectionParam.setServer(reader.ReadLine());
                gConnectionParam.setUserName(reader.ReadLine());
                //base.Server = reader.ReadLine();
                //base.UserName = reader.ReadLine();

                cipherText = Encoding.Unicode.GetBytes(reader.ReadLine());
                AesLib.Aes a = new Aes(keysize, new byte[16]);
                a.InvCipher(cipherText, decipheredText);

                //Password = Encoding.Unicode.GetString(decipheredText);
                gConnectionParam.setPassword(Encoding.Unicode.GetString(decipheredText));

                //base.Database = reader.ReadLine();
                gConnectionParam.setDatabase(reader.ReadLine());
                // Fix error : error for There is already an open DataReader associated with this Command which must be closed first ("MultipleActiveResultSets=True;")
                str = gConnectionParam.getConnString();

            }
            return str;
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            byte[] plainText = new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                      0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};

            byte[] cipherText = new byte[16];
            byte[] decipheredText = new byte[16];

            byte[] keyBytes = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                                     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};

            Aes a = new Aes(Aes.KeySize.Bits128, keyBytes);

            Console.WriteLine("\nAdvanced Encryption Standard Demo in .NET");
            Console.WriteLine("\nThe plaintext is: ");
            DisplayAsBytes(plainText);

            Console.WriteLine("\nUsing a " + Aes.KeySize.Bits192.ToString() + "-key of: ");
            DisplayAsBytes(keyBytes);

            a.Cipher(plainText, cipherText);

            Console.WriteLine("\nThe resulting ciphertext is: ");
            DisplayAsBytes(cipherText);

            a.InvCipher(cipherText, decipheredText);

            Console.WriteLine("\nAfter deciphering the ciphertext, the result is: ");
            DisplayAsBytes(decipheredText);

            Console.WriteLine("\nDone");
            Console.ReadLine();
        }
        public override string BuildConnectionString(ConnectionParam pconparam)
        {
            //    with OdbcConnectionString la` 1 trong cac chuoi~ duoi day
            //    @"Driver={SQL Server};Server=.;Trusted_Connection=yes;Database=pubs;"; // --> ket noi csdl SQL bang che do authentication cua windows
            //    @"Driver={SQL Server};server=.;uid=sa;pwd=12345;database=pubs;"; // --> ket noi csdl SQL bang che do username va password cua SQL
            //    @"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=yes"
            //    @"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\nwind.mdb"
            //    @"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"
            //    @"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"
            //    @"DSN= dsnname" ==>voi dsnname duoc cau hinh nhu sau : vao administrator Tools/Data Sources (ODBC)->chon tab System (DSN) ->bam nut Add -> chon driver do Microsoft Access(*.mdb)->o muc Data Source Name danh ten DSN bat ky(dsnname) ,vi du la odbcTest->nhan nut Select de chon database Access,vi du D:\test.mdb ->bam nut ok de ket thuc

            if (DbType == "ODBC_SQL")
            {
                keysize = Aes.KeySize.Bits128;
                byte[] cipherText = new byte[16];
                byte[] decipheredText = new byte[16];
                FileInfo inf = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "settingsODBC_SQL.dat");
                if (inf.Exists)
                {
                    StreamReader reader = inf.OpenText();
                    //base.Server = reader.ReadLine();
                    pconparam.setServer(reader.ReadLine());
                    //base.UserName = reader.ReadLine();
                    pconparam.setUserName(reader.ReadLine());

                    cipherText = Encoding.Unicode.GetBytes(reader.ReadLine());
                    AesLib.Aes a = new Aes(keysize, new byte[16]);
                    a.InvCipher(cipherText, decipheredText);

                    //Password = Encoding.Unicode.GetString(decipheredText);
                    pconparam.setPassword(Encoding.Unicode.GetString(decipheredText));

                    //base.Database = reader.ReadLine();
                    pconparam.setDatabase(reader.ReadLine());

                    strconn = reader.ReadLine();

                    if (strconn == "sa")
                    {
                        //string OdbcConnectionString = @"Driver={SQL Server};server=.;uid=sa;pwd=12345;database=pubs;"; // --> ket noi csdl SQL bang che do username va password cua SQL
                        //strconn = @"Driver={SQL Server};server="+ base.Server + ";uid="+base.UserName +";pwd="+base.Password +";database=" + base.Database;
                        strconn = @"Driver={SQL Server}" + ";Connect Timeout=60" + ";MultipleActiveResultSets=True; server=" + pconparam.getServer() + ";uid=" + pconparam.getUserName() + ";pwd=" + pconparam.getPassword() + ";database=" + pconparam.getDatabase();

                    }
                    else if (strconn == "rbNTSecurity")
                    {
                        //string OdbcConnectionString = @"Driver={SQL Server};Server=.;Trusted_Connection=yes;Database=pubs;"; // --> ket noi csdl SQL bang che do authentication cua windows
                        //strconn = @"Driver={SQL Server};Server=" + base.Server + ";Trusted_Connection=yes;database="+base.Database;
                        strconn = @"Driver={SQL Server}" + ";Connect Timeout=60" + ";MultipleActiveResultSets=True; Server=" + pconparam.getServer() + ";Trusted_Connection=yes;database=" + pconparam.getDatabase();

                    }
                    reader.Close();
                }
                else
                {
                    StreamWriter writer = inf.CreateText();
                    writer.Close();
                }
                base.ConnectionString = strconn;

            }

            else if (DbType == "ODBC_Access")
            {
                keysize = Aes.KeySize.Bits128;
                byte[] cipherText = new byte[16];
                byte[] decipheredText = new byte[16];
                FileInfo inf = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "settingsOLEDB_Access.dat");
                if (inf.Exists)
                {
                    StreamReader reader = inf.OpenText();
                    //base.Server = reader.ReadLine();
                    pconparam.setServer(reader.ReadLine());
                    //base.UserName = reader.ReadLine();
                    pconparam.setUserName(reader.ReadLine());

                    cipherText = Encoding.Unicode.GetBytes(reader.ReadLine());
                    AesLib.Aes a = new Aes(keysize, new byte[16]);
                    a.InvCipher(cipherText, decipheredText);
                    //Password = Encoding.Unicode.GetString(decipheredText);
                    pconparam.setPassword(Encoding.Unicode.GetString(decipheredText));

                    //base.Database = reader.ReadLine();
                    pconparam.setDatabase(reader.ReadLine());

                    strconn = reader.ReadLine();

                    string accessDBpath = pconparam.getDatabase(); //duong dan chua file csdl Access
                    //set duong` dan cho file csdl Access neu no ko nam` trong thu muc ung dung \bin\Debug

                    //hoac string OdbcConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\test.mdb";
                    strconn = @"Driver={Microsoft Access Driver (*.mdb)};DBQ=" + accessDBpath;
                    //example 1 : accessDBpath = test.mdb (neu file test.mdb nam` trong thu muc bin\Debug cua ung dung)
                    //example 2 : accessDBpath = D:\test.mdb trong truong` hop nay` phai set duong dan den file access de ung dung co the truy cap vao file access

                    reader.Close();
                }
                else
                {
                    StreamWriter writer = inf.CreateText();
                    writer.Close();
                }
                base.ConnectionString = strconn;
            }
            else if (DbType == "ODBC_Oracle")
            {
                //@"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=yes"
                //code later
            }

            else if (DbType == "ODBC_Excel")
            {
                //@"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"
                //code later
            }

            else if (DbType == "ODBC_TextFile")
            {
                //@"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"
                //code later
            }

            else if (DbType == "ODBC_DSN")
            {

                string DSN_ODBC_ACCESS = "tenDSN"; //ten DSN
                keysize = Aes.KeySize.Bits128;
                byte[] cipherText = new byte[16];
                byte[] decipheredText = new byte[16];
                FileInfo inf = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "settingsOLEDB_DSN.dat");
                if (inf.Exists)
                {
                    StreamReader reader = inf.OpenText();
                    //base.Server = reader.ReadLine(); //ko dung`
                    //base.UserName = reader.ReadLine(); //ko dung`

                    cipherText = Encoding.Unicode.GetBytes(reader.ReadLine());
                    AesLib.Aes a = new Aes(keysize, new byte[16]);
                    a.InvCipher(cipherText, decipheredText);
                    //Password = Encoding.Unicode.GetString(decipheredText);
                    DSN_ODBC_ACCESS = Encoding.Unicode.GetString(decipheredText);
                    //base.Database = reader.ReadLine();
                    //strconn = reader.ReadLine();

                    //hoac string OdbcConnectionString = @"DSN=tenDSN";
                    strconn = @"DSN=" + DSN_ODBC_ACCESS;

                    reader.Close();
                }
                else
                {
                    StreamWriter writer = inf.CreateText();
                    writer.Close();
                }
                base.ConnectionString = strconn;

                //@"DSN= dsnname" ==>voi dsnname duoc cau hinh nhu sau :
                //vao administrator Tools/Data Sources (ODBC)->chon tab System (DSN) ->bam nut Add -> chon driver do Microsoft Access(*.mdb)
                //->o muc Data Source Name danh ten DSN bat ky(dsnname) ,vi du la odbcTest
                //->nhan nut Select de chon database Access,vi du D:\test.mdb ->bam nut ok de ket thuc
                //code later
            }

            else throw new Exception("Invalid database type");

            return base.ConnectionString;
        }