Example #1
0
        private void Worker_DoWorkEncryption(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;

            worker.ReportProgress(0);
            eo = Encryption.GenerateEncodedFile(inputFilePath, outputFilePath,
                                                Globals.blockSize, cipherMode, fileExtension, recipents, worker);
        }
Example #2
0
        public static EncryptionObject GenerateEncodedFile(string inputFilePath,
                                                           string outputFilePath, int blockSize, string cipherModeString,
                                                           string fileExtension, List <User> recipents,
                                                           BackgroundWorker worker)
        {
            int keySizeBits = 128;

            //generate session key
            byte[] sessionKey = EncryptionHelper.GenerateSessionKey(keySizeBits);

            //get a dictionary with recipent emails and their encrypted session keys
            Dictionary <string, string> recipentsKeysDict = Encryption.GetRecipentsEncryptedSessionKeys(sessionKey, recipents);

            //initialization vector is set in AesEncryptFromFile and then put into header
            byte[] IV = null;

            string tempEncodedFile = "tempEncoded.xml";

            CipherMode cipherMode = EncryptionHelper.CipherModeFromString(cipherModeString);

            //encrypting input file and saving it in destined out file
            using (Aes myAes = Aes.Create())
            {
                EncryptionHelper.AesEncryptFromFile(inputFilePath, tempEncodedFile, sessionKey, cipherMode, blockSize, out IV, worker);

                //EncryptionHelper.AesEncryptFromFile(inputFilePath, tempEncodedFile, myAes.Key, myAes.Mode, myAes.BlockSize, out IV, worker);
                //Encryption.DecryptToFile(pathToOutFile, decodedFileName, myAes.Key, myAes.Mode, myAes.BlockSize, IV);
            }

            string ivString = Convert.ToBase64String(IV);

            string tempFileWithHeader = "tempHeader.xml";

            XmlHelpers.GenerateXMLHeader(tempFileWithHeader, Globals.Algorithm,
                                         keySizeBits.ToString(), blockSize.ToString(), cipherModeString, ivString, recipentsKeysDict, fileExtension);

            //todo now only encoded text in file (no header)
            MergeHeaderAndEncodedContentIntoOutputFile(outputFilePath, tempFileWithHeader, tempEncodedFile);

            //todo temp
            EncryptionObject eo = new EncryptionObject();

            eo.blockSize = blockSize;
            eo.ivString  = ivString;

            recipentsKeysDict.TryGetValue(recipents.First().Email, out string encSessionKey);
            Console.WriteLine("enc session key " + encSessionKey);
            eo.encryptedSessionKey = encSessionKey;
            return(eo);
        }
Example #3
0
        public static void DecryptFile(EncryptionObject eo, string filePath, string decodedFileName, User selectedUser)
        {
            Console.WriteLine("decrypting");

            //todo temp

            /*string tempEncodedFilePath = "tempEncodedContents";
             * XmlHelpers.RetrieveXmlHeaderFromFile(filePath, out string xmlHeaderString, tempEncodedFilePath);
             *
             * XmlHelpers.ReadDataFromXMLHeader(xmlHeaderString,
             *  out string algorithm, out string keySize,
             *  out string blockSize, out string cipherMode,
             *  out string iv, out Dictionary<string, string> recipents,
             *  out string fileExtension);
             *
             */
            //recipents are kept in a dictionary as
            //<recipentEmail, encryptedUserSessionKey> pairs

            //to decrypt the file we need a session key
            //we need to find selectedUser- the user that current user of the app claims to be-
            //get their encryptedUserSessionKey
            //and decrypt the key using user's private key

            //todo maybe set to some noise, so that if foreach doesn't find anything, the decoding will work and produce noise-file

            //todo temp

            /*string encryptedSessionKeyString ="aaaa";
             * foreach(KeyValuePair<string, string> emailKey in recipents)
             * {
             *  if (emailKey.Key.Equals(selectedUser.Email))
             *  {
             *      encryptedSessionKeyString = emailKey.Value;
             *      break;
             *  }
             * }
             *
             * //decrypt session key using user's private key
             * string userPrivateKeyString = UsersManagement.GetUserPrivateKeyFromFile(selectedUser.Email);
             *
             * byte[] decryptedSessionKeyByte = EncryptionHelper.DecryptSessionKeyFromString(encryptedSessionKeyString, userPrivateKeyString);
             */

            //todo temp to remove
            string userPrivateKeyString = UsersManagement.GetUserPrivateKeyFromFile(selectedUser.Email);

            byte[] decryptedSessionKeyByte = EncryptionHelper.DecryptSessionKeyFromString(eo.encryptedSessionKey, userPrivateKeyString);
            Console.WriteLine("private key of " + selectedUser.Email + " is " + userPrivateKeyString);

            CipherMode mode = CipherMode.CBC;

            EncryptionHelper.AesDecryptToFile(filePath,
                                              decodedFileName + ".txt",
                                              decryptedSessionKeyByte, mode, eo.blockSize,
                                              Convert.FromBase64String(eo.ivString));


            //set cipher mode
            //todo uncomment

            /*CipherMode mode = CipherMode.CBC;
             *
             * switch (cipherMode)
             * {
             *  case Globals.modeCBC:
             *      mode = CipherMode.CBC;
             *      break;
             *  case Globals.modeCFB:
             *      mode = CipherMode.CFB;
             *      break;
             *  case Globals.modeECB:
             *      mode = CipherMode.ECB;
             *      break;
             *  case Globals.modeOFB:
             *      mode = CipherMode.OFB;
             *      break;
             *
             * }
             *
             * EncryptionHelper.AesDecryptToFile(tempEncodedFilePath,
             *  decodedFileName + fileExtension,
             *  decryptedSessionKeyByte, mode, Int32.Parse(keySize), //not key size, should be block size!
             *  Convert.FromBase64String(iv));
             */
        }