Example #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;
        }
Example #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();
        }
Example #3
0
        private void saveSettings()
        {
            try
            {
                dcSQL.Server = txtServerName.Text;
                dcSQL.Database = cbDatabaseName.Text;

                if (rbNTSecurity.Checked)
                {
                    dcSQL.SecurityType = DatabaseConfiguration.WINDOWS_NT_SECURITY;
                }
                else if (rbSQLSecurity.Checked)
                {
                    dcSQL.UserId = txtUserId.Text;
                    dcSQL.Password = txtPassword.Text;
                    dcSQL.SecurityType = DatabaseConfiguration.SQLSERVER_SECURITY;
                }
                dcSQL.TestConnection();
                if (dcSQL.Connected)
                {

                    keysize = Aes.KeySize.Bits128;

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

                    plainText = Encoding.Unicode.GetBytes(txtPassword.Text.PadRight(8, ' '));
                    AesLib.Aes a = new Aes(keysize, new byte[16]);
                    a.Cipher(plainText, cipherText);
                    string passasc = Encoding.Unicode.GetString(cipherText);

                    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "settings.dat"))
                        File.Delete(AppDomain.CurrentDomain.BaseDirectory + "settings.dat");

                    FileInfo inf = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "settings.dat");
                    StreamWriter writer = inf.CreateText();
                    writer.WriteLine(txtServerName.Text);
                    writer.WriteLine(txtUserId.Text);
                    writer.WriteLine(passasc);
                    writer.WriteLine(cbDatabaseName.Text);
                    ExMessageBox.Show("Đã lưu", "Thông tin", ExMessageBox.CYButtons.OK, ExMessageBox.CYIcon.Information);
                    writer.Close();
                }//if
                else
                {
                    ExMessageBox.Show("Kết nối không thành công. Vui lòng kiểm tra lại các thông số kết nối!", "Thông tin", ExMessageBox.CYButtons.OK, ExMessageBox.CYIcon.Error);
                }
            }
            catch(AppException ex)
            {
                String strMessage =ex.Message;

            }
        }
Example #4
0
        static void Main(string[] args)
        {
            // Starting with a console application
            string originalText = "";
            string key          = "";

            Console.WriteLine("Choose Encrypt/Decrypt - 1/2");
            int mode = int.Parse(Console.ReadLine());

            if (mode == 1)
            {
                Console.WriteLine("Enter text to encrypt");
                originalText = Console.ReadLine();
                Console.WriteLine("Enter the secret key");
                key = Console.ReadLine();



                byte[] plainText      = new byte[16];
                byte[] keybytes       = new byte[16];
                byte[] cipherText     = new byte[16];
                byte[] decryptedBytes = new byte[16];



                plainText = Encoding.UTF8.GetBytes(originalText);
                keybytes  = Encoding.UTF8.GetBytes(key);

                AesLib.Aes a = new AesLib.Aes(AesLib.Aes.KeySize.Bits192, keybytes);

                byte[] enciphered = a.AESEncryptPadding(originalText);


                a.Dump();

                Console.WriteLine("With AES LIB " + a.VisualizeInMonkey(enciphered));
                Console.WriteLine("With AES LIB " + a.VisualizeInHex(enciphered));


                byte[] verified = ManagedAesSample.EncryptAesManaged(originalText, keybytes);

                Console.WriteLine("With AES INTERNAL " + a.VisualizeInMonkey(verified));
                Console.WriteLine("With AES INTERNAL " + a.VisualizeInHex(verified));

                Console.ReadKey();

                AesLib.Aes b = new AesLib.Aes(AesLib.Aes.KeySize.Bits192, keybytes);

                byte[] deciphered = b.Decrypt(enciphered);

                Console.WriteLine("Decyphered: " + Encoding.UTF8.GetString(deciphered));


                //a.Decrypt(cipherText, decryptedBytes);
                //a.Dump();

                //string decryption_hex = string.Join(" ", plainText.Select(x => x.ToString("X2")));
                //string decryption_clear = string.Join(" ", decryptedBytes.Select(x => x.ToString("X2")));

                //Console.WriteLine($"Decrypted in hex: {decryption_hex}");
                //Console.WriteLine($"Decrypted in plain text: {Encoding.UTF8.GetString(decryptedBytes)}");
            }
            else
            {
                Console.WriteLine("Not implemented yet");
            }
        }
        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;
        }