Exemplo n.º 1
0
        private void test(int keyLength, int messageLegth)
        {
            //setup
            string message = new string('a', messageLegth); //creates stirng of "a" of length <messageLength>
            int    tests   = 1000;                          //how many tests to run

            TestResult[] testResults = new TestResult[tests];

            CryptoUtility.AESkeyLength = keyLength;        //set up CryptoUtility to use correct key length
            AESKey = CryptoUtility.GenerateAESRandomKey(); //gets random key for set of tests

            //first run done seporately because of set up time
            TestResult firstRun = process(message);

            //Console.WriteLine("First run:");
            //Console.WriteLine($"Total time: {firstRun.totalLength} ms");
            Console.WriteLine($"First Run - Total: {firstRun.totalTime} ms  Encryption: {firstRun.encryptionTime}ms  Decryption: {firstRun.decryptionTime}ms");
            Console.WriteLine($"Number of tests: {tests}");

            for (int i = 0; i < testResults.Length; i += 1)
            {
                testResults[i] = process(message);
            }
            displayResults(testResults, keyLength, messageLegth); //send results to be displayed
            Console.WriteLine();

            //write results to excel file (in memory)
            excelWriter.addRow(keyLength, messageLegth, testResults);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the key whos date field is equal to todays date. If a key does not yet exist for todays date, it is created
        /// and stored.
        /// </summary>
        /// <returns>byte[] containing todays key</returns>
        public Key getTodaysKey()
        {
            SQLiteDataReader reader = db.retrieve("SELECT keyID, AESKey, IV FROM Keys WHERE date = CURRENT_DATE"); //gets key with todays date

            if (reader.HasRows)                                                                                    //if there were any keys with todays date
            {
                reader.Read();                                                                                     //prepares reader to extract data

                int    keyID  = Convert.ToInt32((long)reader["keyID"]);                                            //sqlite stored primary keys as int64s
                byte[] encKey = (byte[])reader["AESKey"];                                                          //extract encrypted key
                byte[] IV     = (byte[])reader["IV"];                                                              //extract initialisation vector it was encrypted with

                byte[] keyBytes = decryptKey(encKey, IV);                                                          //decrypt key
                Key    key      = new Key(keyID, keyBytes);                                                        //creates key object to return

                return(key);
            }
            else //must generate new key and create DB entry
            {
                byte[] newKey = CryptoUtility.GenerateAESRandomKey(); //generate random AES key

                byte[] masterKey = Globals.getMasterKey();                                      //gets master key to encrypt the new key
                Tuple <byte[], byte[]> encResult = CryptoUtility.AESEncrypt(newKey, masterKey); //encrypts new key with master key
                byte[] encNewKey = encResult.Item1;                                             //extract encrypted key
                byte[] IV        = encResult.Item2;                                             //extract initialisation vector

                insertNewKey(encNewKey, IV);                                                    //insert it into database

                return(getTodaysKey());                                                         //1 level of recursion, creates entry then fetches and returns it
            }
        }
Exemplo n.º 3
0
        //this method tests the creation of a random AES key, then using it encrypt and decrypt a message
        public void testRandomKeyEncryption()
        {
            string data = "hello, good morning, I hope all is well. blahblahblahblahblah";

            byte[] dataBytes = Encoding.UTF8.GetBytes(data);        //convert data to bytes

            byte[] keyBytes = CryptoUtility.GenerateAESRandomKey(); //gets randomly generated

            //tuple contains encrypted bytes plus initialisation vector needed to decrypt it
            Tuple <byte[], byte[]> enc = CryptoUtility.AESEncrypt(dataBytes, keyBytes); //encypts and gets encrypted bytes + IV

            byte[] encBytes = enc.Item1;                                                //encrypted bytes
            byte[] IV       = enc.Item2;                                                //Instantiation vector

            string decryptedString = CryptoUtility.AESDecrypt(encBytes, keyBytes, IV);

            Assert.AreEqual <string>(data, decryptedString); //compare original data to the decrypted string
        }