Пример #1
0
 public static DecryptionResult DecryptText(string inputText, string password)
 {
     DecryptionResult decryptionResult = null;
     try
     {
         byte[] EncryptedBytes = null;
         try
         {
             EncryptedBytes = System.Text.Encoding.UTF8.GetBytes(inputText);
         }
         catch (Exception)
         {
             decryptionResult = new DecryptionResult()
             {
                 Result = false,
                 Error = "Text is not a valid encrypted message",
                 DecryptedString = null
             };
             return decryptionResult;
         }
         byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
         passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
         byte[] DecryptedBytes = DecryptionService.GetDecryptedByteArray(EncryptedBytes, passwordBytes);
         try
         {
             decryptionResult = new DecryptionResult()
             {
                 DecryptedString = System.Text.Encoding.UTF8.GetString(DecryptedBytes),
                 Result = true,
                 Error = null
             };
         }
         catch (Exception)
         {
             decryptionResult = new DecryptionResult()
             {
                 Result = false,
                 Error = "Text is not a valid encrypted message",
                 DecryptedString = null
             };
             return decryptionResult;
         }
     }
     catch (Exception ex)
     {
         decryptionResult.Result = false;
         decryptionResult.Error = ex.Message;
     }
     return decryptionResult;
 }
Пример #2
0
        public async static Task<DecryptionResult> DecryptFile(Windows.Storage.StorageFile inputFile, string password)
        {
            DecryptionResult decryptionResult = null;
            try
            {
                byte[] FileContentBytes = null;
                using (Stream stream = await inputFile.OpenStreamForReadAsync())
                {
                    using (var memoryStream = new MemoryStream())
                    {

                        stream.CopyTo(memoryStream);
                        FileContentBytes = memoryStream.ToArray();
                    }
                }
                int ctr = 0;
                for (int i = 999; i >= 0; i--)
                {
                    if (FileContentBytes[i] != 0)
                    {
                        ctr = i;
                        break;
                    }
                }
                int ctr2 = 1000;
                for (int i = 1999; i >= 1000; i--)
                {
                    if (FileContentBytes[i] != 0)
                    {
                        ctr2 = i;
                        break;
                    }
                }
                int FileNameBytesLength = ctr + 1;
                int ExtensionBytesLength = ctr2 + 1 - 1000;
                byte[] passwordBytes = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password));
                byte[] SaltedHashedPassword = SHA256.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(password + "CryptoApp"));
                byte[] FileNameBytes = new byte[FileNameBytesLength];
                byte[] ExtensionBytes = new byte[ExtensionBytesLength];
                byte[] StoredHashedPassword = new byte[32];
                byte[] EncryptedBytes = new byte[FileContentBytes.Length - 2032];
                System.Buffer.BlockCopy(FileContentBytes, 2000, StoredHashedPassword, 0, 32);
                bool ArePasswordsSame = true;
                for (int i = 0; i < 32; i++)
                {
                    if (SaltedHashedPassword[i] != StoredHashedPassword[i])
                    {
                        ArePasswordsSame = false;
                        break;
                    }
                }
                if (ArePasswordsSame == false)
                {
                    decryptionResult = new DecryptionResult()
                    {
                        Result = false,
                        Error = "Password",
                        DecryptedString = null
                    };
                    return decryptionResult;
                }
                System.Buffer.BlockCopy(FileContentBytes, 0, FileNameBytes, 0, FileNameBytesLength);
                System.Buffer.BlockCopy(FileContentBytes, 1000, ExtensionBytes, 0, ExtensionBytesLength);
                System.Buffer.BlockCopy(FileContentBytes, 2032, EncryptedBytes, 0, EncryptedBytes.Length);

                int Blocks128000Size = EncryptedBytes.Length / 128000;
                int LastBlockSize = EncryptedBytes.Length % 128000;
                ctr = 0;
                byte[] DecryptedBytes = 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 = DecryptionService.GetDecryptedByteArray(input, passwordBytes);
                    System.Buffer.BlockCopy(result, 0, DecryptedBytes, ctr, 128000);
                    ctr = ctr + 128000;
                }
                if (LastBlockSize > 0)
                {
                    byte[] input = new byte[LastBlockSize];
                    System.Buffer.BlockCopy(EncryptedBytes, ctr, input, 0, LastBlockSize);
                    byte[] result = DecryptionService.GetDecryptedByteArray(input, passwordBytes);
                    System.Buffer.BlockCopy(result, 0, DecryptedBytes, ctr, LastBlockSize);
                }
                string CurrentDirectoryPath = System.IO.Path.GetDirectoryName(inputFile.Path);
                string CurrentFileName = System.Text.Encoding.ASCII.GetString(FileNameBytes) + "_1";
                string CurrentFileExtension = System.Text.Encoding.ASCII.GetString(ExtensionBytes);
                string WritePath = System.IO.Path.Combine(CurrentDirectoryPath, CurrentFileName + CurrentFileExtension);
                decryptionResult = new DecryptionResult()
                {
                    Result = true,
                    Error = null,
                    DecryptedString = null,
                    DecryptedContents = DecryptedBytes,
                    WritePath = WritePath
                };
            }
            catch (Exception ex)
            {
                decryptionResult = new DecryptionResult()
                {
                    Result = false,
                    Error = ex.Message,
                    DecryptedString = null
                };
            }
            return decryptionResult;
        }