public static EncryptionResult EncryptText(string inputText, string password) { EncryptionResult encryptionResult = new EncryptionResult() { Result = false }; try { byte[] encryptedBytes = System.Text.Encoding.UTF8.GetBytes(inputText); byte[] passwordToByteArray = System.Text.Encoding.UTF8.GetBytes(password); passwordToByteArray = SHA256.Create().ComputeHash(passwordToByteArray); byte[] encryptedByteArray = EncryptionService.GetEncryptedByteArray(encryptedBytes, passwordToByteArray); try { encryptionResult.EncryptedString = System.Convert.ToBase64String(encryptedByteArray); encryptionResult.Error = null; encryptionResult.Result = true; } catch (Exception) { encryptionResult.Result = false; encryptionResult.Error = "This string can not be enrypted"; } } catch (Exception ex) { encryptionResult.Result = false; encryptionResult.Error = ex.Message; } return(encryptionResult); }
public static EncryptionResult EncryptFile(string inputFile, string password) { EncryptionResult encryptionResult = null; try { byte[] encryptedBytes = File.ReadAllBytes(inputFile); byte[] passwordToByteArray = System.Text.Encoding.ASCII.GetBytes(password); passwordToByteArray = SHA256.Create().ComputeHash(passwordToByteArray); byte[] SaltedHashedPassword = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password + "CryptoApp")); byte[] encryptedByteArray = EncryptionService.GetEncryptedByteArray(encryptedBytes, passwordToByteArray); string CurrentDirectoryPath = System.IO.Path.GetDirectoryName(inputFile); string CurrentFileName = Path.GetFileNameWithoutExtension(inputFile); string CurrentFileExtension = Path.GetExtension(inputFile); byte[] FileNameBytes = System.Text.Encoding.ASCII.GetBytes(CurrentFileName); byte[] ExtensionBytes = System.Text.Encoding.ASCII.GetBytes(CurrentFileExtension); int FileNameBytesLength = FileNameBytes.Length; int ExtensionBytesLength = ExtensionBytes.Length; FileNameBytesLength = (FileNameBytesLength >= 1000) ? 999 : FileNameBytesLength; ExtensionBytesLength = (ExtensionBytesLength >= 1000) ? 999 : ExtensionBytesLength; byte[] FileContentBytes = new byte[1000 + 1000 + 32 + encryptedByteArray.Length]; System.Buffer.BlockCopy(FileNameBytes, 0, FileContentBytes, 0, FileNameBytesLength); System.Buffer.BlockCopy(ExtensionBytes, 0, FileContentBytes, 1000, ExtensionBytesLength); System.Buffer.BlockCopy(SaltedHashedPassword, 0, FileContentBytes, 2000, 32); System.Buffer.BlockCopy(encryptedByteArray, 0, FileContentBytes, 2032, encryptedByteArray.Length); var CryptoAppPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "Crypto App"); if (!Directory.Exists(CryptoAppPath)) { System.IO.Directory.CreateDirectory(CryptoAppPath); } var EncryptedFilesPath = Path.Combine(CryptoAppPath, "Encrypted Files"); if (!Directory.Exists(EncryptedFilesPath)) { System.IO.Directory.CreateDirectory(EncryptedFilesPath); } string WritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + ".senc"); if (System.IO.File.Exists(WritePath)) { Int64 ctr3 = 1; var TempWritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + "_" + ctr3.ToString() + ".senc"); while (System.IO.File.Exists(TempWritePath) && ctr3 < int.MaxValue) { ctr3 = ctr3 + 1; TempWritePath = System.IO.Path.Combine(EncryptedFilesPath, CurrentFileName + "_" + ctr3.ToString() + ".senc"); } WritePath = TempWritePath; } File.WriteAllBytes(WritePath, FileContentBytes); encryptionResult = new EncryptionResult() { Result = true, Error = null, EncryptedString = CurrentFileName + ".senc" }; } catch (Exception ex) { encryptionResult = new EncryptionResult() { Result = false, Error = ex.Message, EncryptedString = null, }; } return(encryptionResult); }