Exemplo n.º 1
0
 /// <summary>
 /// Asynchronously transforms an array of cryptographic data into its original form using AES256 in CBC mode.
 /// </summary>
 /// <param name="data">The encrypted data to transform into its original form.</param>
 /// <param name="password">The key to use during the transformation.</param>
 /// <returns>An array containing the original plaintext data.</returns>
 public static async Task <byte[]> DecryptDataAsync(byte[] data, byte[] password)
 {
     using (var original = new MemoryStream(data))
         using (var decrypted = new MemoryStream())
             using (var transform = new EtM_DecryptTransform(key: password))
             {
                 using (var crypto = new CryptoStream(original, transform, CryptoStreamMode.Read))
                     await crypto.CopyToAsync(decrypted);
                 if (!transform.IsComplete)
                 {
                     throw new Exception("Not all blocks have been decrypted.");
                 }
                 return(decrypted.ToArray());
             }
 }
Exemplo n.º 2
0
        public static void Authenticate(string originalFilename)
        {
            string encryptedFilename = originalFilename + ".enc" + Path.GetExtension(originalFilename);

            using (var encryptedStream = new FileStream(encryptedFilename, FileMode.Open))
                using (var decTransform = new EtM_DecryptTransform(key: key, authenticateOnly: true))
                {
                    using (var cryptoStream = new CryptoStream(encryptedStream, decTransform, CryptoStreamMode.Read))
                        cryptoStream.CopyTo(Stream.Null);

                    if (!decTransform.IsComplete)
                    {
                        throw new Exception("Not all blocks are authenticated.");
                    }
                }
        }
Exemplo n.º 3
0
        public static void Decrypt(string originalFilename)
        {
            string encryptedFilename = originalFilename + ".enc" + Path.GetExtension(originalFilename);
            string decryptedFilename = originalFilename;

            using (var encryptedStream = new FileStream(encryptedFilename, FileMode.Open))
                using (var decryptedStream = new FileStream(decryptedFilename, FileMode.Create))
                    using (var decTransform = new EtM_DecryptTransform(key: key))
                    {
                        using (var cryptoStream = new CryptoStream(encryptedStream, decTransform, CryptoStreamMode.Read))
                            cryptoStream.CopyTo(decryptedStream);

                        if (!decTransform.IsComplete)
                        {
                            throw new Exception("Not all blocks are decrypted.");
                        }
                    }

            File.Delete(encryptedFilename);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Transforms a cryptographically strong sequence of bytes into its plainbyte equivalent using AES.
        /// </summary>
        /// <param name="data">The data to transform.</param>
        /// <param name="key">The password to use during the transformation.</param>
        internal static byte[] Decrypt(this byte[] data, string key)
        {
            // Create a new memory stream.
            MemoryStream plaintext = new MemoryStream();

            // Transform our ciphertext into plaintext.
            using (MemoryStream ciphertext = new MemoryStream(data))
                using (EtM_DecryptTransform transform = new EtM_DecryptTransform(key: key.ToBytes()))
                {
                    using (CryptoStream crypto = new CryptoStream(ciphertext, transform, CryptoStreamMode.Read))
                        crypto.CopyTo(plaintext);
                    if (!transform.IsComplete)
                    {
                        throw new Exception("The data could not be decrypted.");
                    }
                }

            // Return our decrypted text.
            return(plaintext.ToArray());
        }
Exemplo n.º 5
0
            /// <summary>
            /// Asynchronously transforms a cryptographic file into its original form using AES256 in CBC mode.
            /// </summary>
            /// <param name="originalFile">The path to the file to be transformed into its decrypted form.</param>
            /// <param name="decryptedFile">The location the decrypted file will be written to.</param>
            /// <param name="password">The key to use during the cryptographic transformation.</param>
            /// <returns>A flag determining whether or not the operation was successful or not.</returns>
            public static async Task <bool> DecryptFileAsync(string originalFile, string decryptedFile, byte[] password)
            {
                try
                {
                    string originalFilename  = originalFile;
                    string decryptedFilename = decryptedFile;

                    using (var encryptedStream = LongFile.Open(originalFilename, FileMode.Open))
                        using (var decryptedStream = LongFile.Create(decryptedFilename, 8192))
                            using (var decTransform = new EtM_DecryptTransform(key: password))
                            {
                                using (var cryptoStream = new CryptoStream(encryptedStream, decTransform, CryptoStreamMode.Read))
                                    await cryptoStream.CopyToAsync(decryptedStream);

                                if (!decTransform.IsComplete)
                                {
                                    throw new Exception("Not all blocks have been decrypted.");
                                }
                            }
                    return(true);
                }
                catch { return(false); }
            }