示例#1
0
        public static SecureboxKey GetSecureboxKey(string GUID)
        {
            SecureboxKey el = new SecureboxKey();

            byte[]           IV             = new byte[16];
            byte[]           KEY            = new byte[16];
            SQLiteConnection m_dbConnection = new SQLiteConnection(connstring);

            m_dbConnection.Open();

            string        sql     = "select IV, KEY from lookup WHERE GUID=@guid";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);

            command.Parameters.AddWithValue("@guid", GUID);
            SQLiteDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                IV  = (byte[])reader["IV"];
                KEY = (byte[])reader["KEY"];
            }
            reader.Close();
            m_dbConnection.Close();
            el.IV   = IV;
            el.KEY  = KEY;
            el.GUID = GUID;
            return(el);
        }
示例#2
0
        private static void _DecryptPreprocess(string inputFile)
        {
            string outputFile = inputFile + ".tmp";

            int numberOfBytes = 16;

            byte[] bytes = new byte[numberOfBytes];
            //get GUID from file
            using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
            {
                fs.Read(bytes, 0, bytes.Length);
            }
            // var buffer2 = File.ReadAllBytes(inputFile);
            Guid   guid = new Guid(bytes);
            string GUID = guid.ToString();

            //create new file without GUID

            char[]   delimiterChars      = { '.' };
            string[] newFile             = outputFile.Split(delimiterChars);
            string   newFileFullPathName = String.Join(".", newFile.Take(newFile.Length - 2));

            using (var fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
            {
                byte[] fullfile = new byte[fs.Length];
                int    toRead = (int)fs.Length - 1, bytesRead;
                fs.Seek(16, SeekOrigin.Begin);
                while (toRead > 0 && (bytesRead = fs.Read(fullfile, 0, toRead)) > 0)
                {
                    toRead        -= bytesRead;
                    numberOfBytes += bytesRead;
                }
                using (var newFS = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
                {
                    newFS.Write(fullfile, 0, fullfile.Length);
                }
            }
            //get IV and key from SQLite DB
            SecureboxKey el = DatabaseManagement.GetSecureboxKey(GUID);

            if (el != null &&
                el.IV != null &&
                el.IV.Length > 0)
            {
                Securebox._DecryptFile(outputFile, newFileFullPathName, el);
            }
            Securebox.DeleteCurrentFile(outputFile);
        }
示例#3
0
        private static void _DecryptFile(string inputFile, string outputFile, SecureboxKey skey)
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    // byte[] key = ASCIIEncoding.UTF8.GetBytes(skey.KEY);
                    byte[] key = skey.KEY;

                    /* This is for demostrating purposes only.
                     * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                    byte[] IV = skey.IV;

                    using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                    {
                        using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                        {
                            using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
                            {
                                using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                                {
                                    int data;
                                    while ((data = cs.ReadByte()) != -1)
                                    {
                                        fsOut.WriteByte((byte)data);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }
        }
示例#4
0
        private static void _DecryptFile(string inputFile, string outputFile, SecureboxKey skey)
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    // byte[] key = ASCIIEncoding.UTF8.GetBytes(skey.KEY);
                    byte[] key = skey.KEY;
                    /* This is for demostrating purposes only. 
                     * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
                    byte[] IV = skey.IV;

                    using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
                    {
                        using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
                        {
                            using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
                            {
                                using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
                                {
                                    int data;
                                    while ((data = cs.ReadByte()) != -1)
                                    {
                                        fsOut.WriteByte((byte)data);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }
        }
示例#5
0
        public static SecureboxKey GetSecureboxKey(string GUID)
        {
            SecureboxKey el = new SecureboxKey();

            byte[] IV = new byte[16];
            byte[] KEY = new byte[16];
            SQLiteConnection m_dbConnection = new SQLiteConnection(connstring);
            m_dbConnection.Open();

            string sql = "select IV, KEY from lookup WHERE GUID=@guid";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.Parameters.AddWithValue("@guid", GUID);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                IV = (byte[])reader["IV"];
                KEY = (byte[])reader["KEY"];
                
            }
            reader.Close();
            m_dbConnection.Close();
            el.IV = IV;
            el.KEY = KEY;
            el.GUID = GUID;
            return el;
        }