Exemplo n.º 1
0
        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.Text.Encoding.UTF8.GetString(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);
        }
Exemplo n.º 2
0
        public async static Task <EncryptionResult> EncryptFile(Windows.Storage.StorageFile inputFile, string password)
        {
            EncryptionResult encryptionResult = null;

            try
            {
                byte[] encryptedBytes = null;
                using (Stream stream = await inputFile.OpenStreamForReadAsync())
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        stream.CopyTo(memoryStream);
                        encryptedBytes = memoryStream.ToArray();
                    }
                }
                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"));
                int    Blocks128000Size     = encryptedBytes.Length / 128000;
                int    LastBlockSize        = encryptedBytes.Length % 128000;
                int    ctr = 0;
                byte[] encryptedByteArray = new byte[encryptedBytes.Length];
                for (long i = 0; i < Blocks128000Size; i++)
                {
                    byte[] input = new byte[128000];
                    System.Buffer.BlockCopy(encryptedBytes, ctr, input, 0, 128000);
                    byte[] result = EncryptionService.GetEncryptedByteArray(input, passwordToByteArray);
                    System.Buffer.BlockCopy(result, 0, encryptedByteArray, ctr, 128000);
                    ctr = ctr + 128000;
                }
                if (LastBlockSize > 0)
                {
                    byte[] input = new byte[LastBlockSize];
                    System.Buffer.BlockCopy(encryptedBytes, ctr, input, 0, LastBlockSize);
                    byte[] result = EncryptionService.GetEncryptedByteArray(input, passwordToByteArray);
                    System.Buffer.BlockCopy(result, 0, encryptedByteArray, ctr, LastBlockSize);
                }
                string CurrentDirectoryPath = System.IO.Path.GetDirectoryName(inputFile.Path);
                string CurrentFileName      = Path.GetFileNameWithoutExtension(inputFile.Path);
                string CurrentFileExtension = Path.GetExtension(inputFile.Path);
                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);
                string WritePath = System.IO.Path.Combine(CurrentDirectoryPath, CurrentFileName + ".senc");
                encryptionResult = new EncryptionResult()
                {
                    Result            = true,
                    Error             = null,
                    EncryptedString   = null,
                    EncryptedContents = FileContentBytes,
                    WritePath         = WritePath
                };
            }
            catch (Exception ex)
            {
                encryptionResult = new EncryptionResult()
                {
                    Result          = false,
                    Error           = ex.Message,
                    EncryptedString = null,
                };
            }
            return(encryptionResult);
        }