public RemoteUserInfo Login(string username, string password) { RemoteUserInfo answer = null; SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(Encoding.Unicode.GetBytes(password)); UserInfo userInfo = new UserInfo(username, sha1.HashedString); try { using (var context = new DBContext()) { if (context.CheckUserCredentials(userInfo)) { SessionManager.Instance.AddSession(GetChannelIdentification(), userInfo); answer = new RemoteUserInfo(); answer.Username = userInfo.Username; answer.LeftoverSpace = userInfo.LeftoverSpace; } } } catch (Exception e) { //Shouldnt throw exceptions //throw new Exception("Error logging in.", e); } return(answer); }
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); }
public bool Register(string username, string password) { SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(Encoding.Unicode.GetBytes(password)); UserInfo userInfo = new UserInfo(username, sha1.HashedString); try { using (var context = new DBContext()) { if (context.TryAddUser(userInfo) != -1) { return(true); } else { return(false); } } } catch (Exception e) { //Shouldnt throw exceptions //throw new Exception("Error registering the user.", e); } return(false); }
public void RequestFileDownload(string fileName, string directoryPath, string supposedFileHash) { string FileManagerServiceUrl = "http://localhost:56082/MyCloudStore/CloudStoreService.svc"; var serviceUrl = string.Format($"{FileManagerServiceUrl}/DownloadFile/{fileName}"); var request = (HttpWebRequest)WebRequest.Create(serviceUrl); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (var fileStream = response.GetResponseStream()) { if (fileStream == null) { System.Windows.Forms.MessageBox.Show("File not recieved"); return; } byte[] fileBytes = fileStream.ReadToEnd(); //decrypt bytes byte[] decryptedFile; try { decryptedFile = cryptionController.DecryptFile(fileBytes, fileName); } catch (Exception e) { throw new Exception("Error decrypting the downloaded file: " + e.Message); } //Compare hash (double check since the cryptionController already does this SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(decryptedFile); string hashValue = sha1.HashedString; if (hashValue != supposedFileHash) { System.Windows.Forms.MessageBox.Show("Your downloaded file's hash and its recorded hash before uploading do not match!"); } //save them to a location File.WriteAllBytes(Path.Combine(directoryPath, fileName), decryptedFile); //open with adequate application System.Diagnostics.Process.Start(Path.Combine(directoryPath, fileName)); } System.Windows.Forms.MessageBox.Show(string.Format("Client: Receive Response HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription)); } } catch (Exception e) { throw new Exception("Error downloading the file: " + e.GetFullMessage()); } }
private byte[] GenerateBytePattern(string fileName) { SHA1Hasher sha1 = new SHA1Hasher(); sha1.ComputeHash(Encoding.UTF8.GetBytes(IdentificationUsername)); SHA1Hasher sha2 = new SHA1Hasher(); sha2.ComputeHash(Encoding.UTF8.GetBytes(fileName)); return(Encoding.UTF8.GetBytes(sha2.HashedString + ":" + sha1.HashedString)); }
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(); } }
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); }
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); }