コード例 #1
0
        private void AddRecord(string fileName, CryptoChoice settings, string fileHash)
        {
            byte[] encryptedDataFile = ReadDataFile();
            byte[] fileBytes         = DecryptDataFile(encryptedDataFile);

            SHA1Hasher sha1 = new SHA1Hasher();

            sha1.ComputeHash(Encoding.UTF8.GetBytes(IdentificationUsername));
            SHA1Hasher sha2 = new SHA1Hasher();

            sha2.ComputeHash(Encoding.UTF8.GetBytes(fileName));

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"{sha2.HashedString}:{sha1.HashedString}:{fileHash}:{settings.Choice.ToString()}:{Encoding.UTF8.GetString(settings.Key)}:{settings.Depad}");
            byte[] recordBytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());

            byte[] newFileBytes = new byte[fileBytes.Length + recordBytes.Length];
            Array.Copy(fileBytes, 0, newFileBytes, 0, fileBytes.Length);
            Array.Copy(recordBytes, 0, newFileBytes, fileBytes.Length, recordBytes.Length);

            encryptedDataFile = EncryptDataFile(newFileBytes);

            WriteDataFile(encryptedDataFile);
        }
コード例 #2
0
        internal byte[] DecryptFileExplicit(byte[] encryptedBytes, CryptoChoice settings)
        {
            byte[] fileBytes;
            switch (settings.Choice)
            {
            case CryptoChoices.OneTimePad:
                OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key);
                fileBytes = otp.Decrypt(encryptedBytes);
                break;

            case CryptoChoices.TEA:
                TEACrypter tea = new TEACrypter(settings.Key);
                fileBytes = tea.Decrypt(encryptedBytes);
                if (settings.Depad)
                {
                    fileBytes = TEACrypter.DepadData(fileBytes);
                }
                break;

            default:
                throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist.");
            }

            return(fileBytes);
        }
コード例 #3
0
        public void RequestFileUpload(string filePath, CryptoChoice answer, bool replaceOnConflict = false)
        {
            string fileName = Path.GetFileName(filePath);

            byte[] fileBytes = File.ReadAllBytes(filePath);

            SHA1Hasher sha1 = new SHA1Hasher();

            sha1.ComputeHash(fileBytes);
            string hashValue = sha1.HashedString;

            byte[] encryptedBytes;
            try
            {
                encryptedBytes = cryptionController.EncryptFile(fileName, fileBytes, answer, replaceOnConflict);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Couldn't encrypt the file: " + e.Message);
            }

            HttpWebResponse resp = null;

            try
            {
                string FileManagerServiceUrl = "http://localhost:56082/MyCloudStore/CloudStoreService.svc";
                var    serviceUrl            = string.Format($"{FileManagerServiceUrl}/UploadFile/{fileName}/{hashValue}");
                var    request = (HttpWebRequest)WebRequest.Create(serviceUrl);
                request.Method      = "POST";
                request.ContentType = "text/plain";

                System.IO.Stream reqStream = request.GetRequestStream();
                reqStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                reqStream.Close();

                resp = (HttpWebResponse)request.GetResponse();
                System.Windows.Forms.MessageBox.Show(string.Format("Client: Receive Response HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription));
            }
            catch (Exception e)
            {
                throw new Exception("Error uploading the file to the service: " + e.GetFullMessage());
            }
            finally
            {
                if (resp != null && resp.StatusCode != HttpStatusCode.OK)
                {
                    cryptionController.RemoveFileRecord(fileName);
                }

                resp.Dispose();
            }
        }
コード例 #4
0
        public byte[] EncryptFile(string fileName, byte[] fileBytes, CryptoChoice settings, bool replaceRecordIfConflict = false)
        {
            if (ReadRecord(fileName) != null)
            {
                if (replaceRecordIfConflict)
                {
                    RemoveRecord(fileName);
                }
                else
                {
                    throw new InvalidOperationException("The record for a file with this name already exists. If you want to replace it, pass another bool as an argument to the method.");
                }
            }

            SHA1Hasher sha1 = new SHA1Hasher();

            sha1.ComputeHash(fileBytes);
            string fileHash = sha1.HashedString;

            byte[] encryptedBytes;

            switch (settings.Choice)
            {
            case CryptoChoices.OneTimePad:
                OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key);
                encryptedBytes = otp.Encrypt(fileBytes);
                break;

            case CryptoChoices.TEA:
                TEACrypter tea = new TEACrypter(settings.Key);
                if (TEACrypter.CheckIfDataNeedsPadding(fileBytes))
                {
                    fileBytes      = TEACrypter.PadData(fileBytes);
                    settings.Depad = true;
                }
                encryptedBytes = tea.Encrypt(fileBytes);
                break;

            default:
                throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist.");
            }

            AddRecord(fileName, settings, fileHash);

            return(encryptedBytes);
        }
コード例 #5
0
        public byte[] DecryptFile(byte[] encryptedBytes, string fileName)
        {
            var foundRecord = ReadRecord(fileName);

            if (foundRecord == null)
            {
                throw new ArgumentException("There is no record for this file.");
            }

            CryptoChoice settings         = foundRecord.Item1;
            string       originalFileHash = foundRecord.Item2;

            byte[] fileBytes;
            switch (settings.Choice)
            {
            case CryptoChoices.OneTimePad:
                OneTimePadCrypter otp = new OneTimePadCrypter(settings.Key);
                fileBytes = otp.Decrypt(encryptedBytes);
                break;

            case CryptoChoices.TEA:
                TEACrypter tea = new TEACrypter(settings.Key);
                fileBytes = tea.Decrypt(encryptedBytes);
                if (settings.Depad)
                {
                    fileBytes = TEACrypter.DepadData(fileBytes);
                }
                break;

            default:
                throw new InvalidOperationException("Cryption settings are not valid: Desired cryption algorithm doesn't exist.");
            }

            SHA1Hasher sha1 = new SHA1Hasher();

            sha1.ComputeHash(fileBytes);

            if (originalFileHash != sha1.HashedString)
            {
                throw new Exception("The hash value of the decrypted file and the original file hash value do not match. Access denied.");
            }

            return(fileBytes);
        }