示例#1
0
        public Document(IAes aes, ISecureHash hash, ICompression compression, IPassword password, IFileProxy fileProxy)
        {
            if (aes == null)
            {
                throw new ArgumentNullException(nameof(aes));
            }

            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            if (compression == null)
            {
                throw new ArgumentNullException(nameof(compression));
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            if (fileProxy == null)
            {
                throw new ArgumentNullException(nameof(fileProxy));
            }

            _aes = aes;
            SecureHashProvider  = hash;
            CompressionProvider = compression;
            _password           = password;
            _fileProxy          = fileProxy;
        }
示例#2
0
 private static void TestStubsForDocument(out IAes aes, out ISecureHash hash, out IPassword password, out IFileProxy fileProxy, out ICompression compression)
 {
     aes         = new TestAES();
     hash        = new TestSecureHash();
     password    = new Password("password", "password");
     fileProxy   = new TestFileProxy();
     compression = new TestCompression();
 }
示例#3
0
        public AesCtrMode(IAes aes, ArraySegment <byte> iv)
        {
            _iv  = FixIv(iv);
            _aes = aes ?? throw new ArgumentNullException(nameof(aes));

            Log.Verbose($"--CTR CRYPTING--");
            Log.Verbose($"   NONCE:   {Log.ShowBytes(_iv)}");
        }
示例#4
0
        public FileLoaderBase(IPassword password)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            Aes         = new Aes();
            SecureHash  = new SecureHash();
            Password    = password;
            Compression = new GZipCompression();
        }
示例#5
0
        public Version10Loader(IPassword password)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            _aes         = new Aes();
            _secureHash  = new SecureHash();
            _password    = password;
            _compression = new GZipCompression();
        }
示例#6
0
        public Document(IPassword password)
        {
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            _aes = new Aes();
            SecureHashProvider  = new BCryptHash();
            _password           = password;
            _fileProxy          = new FileProxy();
            CompressionProvider = new GZipCompression();
        }
示例#7
0
        /// <summary>
        /// Create a new AES cipher
        /// </summary>
        /// <param name="key">Encrtyption key</param>
        public static IAes CreateAes(ByteSpan key)
        {
            if (OverrideCreateAes != null)
            {
                IAes result = OverrideCreateAes(key);
                if (null != result)
                {
                    return(result);
                }
            }

            return(new DefaultAes(key));
        }
示例#8
0
        public FastDfsWapper(FastDfsOpinions opinion)
        {
            var endPointsList = new List <IPEndPoint>();

            foreach (var e in opinion.EndPoints)
            {
                endPointsList.Add(e);
            }

            Client = new FastDfsClient(endPointsList);

            Node = Client.GetStorageNode(opinion.GroupName);

            Cryptor = new Aes(opinion.PassWord);
        }
示例#9
0
        /// <summary>
        /// Creates a new instance of an AEAD_AES128_GCM cipher
        /// </summary>
        /// <param name="key">Symmetric key</param>
        public Aes128Gcm(ByteSpan key)
        {
            if (key.Length != KeySize)
            {
                throw new ArgumentException("Invalid key length", nameof(key));
            }

            // Create the AES block cipher
            this.encryptor_ = CryptoProvider.CreateAes(key);

            // Allocate scratch space
            ByteSpan scratchSpace = new byte[96];

            this.hashSubkey_   = scratchSpace.Slice(0, 16);
            this.blockJ_       = scratchSpace.Slice(16, 16);
            this.blockS_       = scratchSpace.Slice(32, 16);
            this.blockZ_       = scratchSpace.Slice(48, 16);
            this.blockV_       = scratchSpace.Slice(64, 16);
            this.blockScratch_ = scratchSpace.Slice(80, 16);

            // Create the GHASH subkey by encrypting the 0-block
            this.encryptor_.EncryptBlock(this.hashSubkey_, this.hashSubkey_);
        }
示例#10
0
        public static void RunAES(FileInfo input, FileInfo output, String passphrase, bool decrypt, AesImplementation use)
        {
            if (!input.Exists)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine("Input file doesn't exist, exiting.");
                Console.ResetColor();
                return;
            }

            string operation = decrypt ? "decryption" : "encryption";

            Console.WriteLine($"Performing {operation} using the {use} implementation of the AES-128-ECB algorithm.");

            AesFactory aesFactory = new AesFactory();
            IAes       aes        = aesFactory.GetAes(use);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            if (!decrypt)
            {
                // Create key using PBKDF2
                byte[] salt       = new byte[8];
                int    iterations = 10000;
                using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
                {
                    // Fill the array with a random value.
                    rngCsp.GetBytes(salt);
                }

                Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passphrase, salt, iterations, HashAlgorithmName.SHA256);

                using (FileStream fs = input.OpenRead())
                    using (FileStream fsOut = output.OpenWrite())
                    {
                        // Mark encrypted file as salted at beginning
                        string saltedMsg      = "Salted__";
                        byte[] saltedMsgBytes = Encoding.ASCII.GetBytes(saltedMsg);
                        fsOut.Write(saltedMsgBytes);

                        // Insert salt into file
                        fsOut.Write(salt);

                        byte[] message = new byte[16];

                        byte[] expandedKey = new byte[176];

                        aes.KeyExpansion(key.GetBytes(16), expandedKey);

                        bool ended = false;

                        while (!ended)
                        {
                            int readBytes = fs.Read(message, 0, 16);
                            if (readBytes == 0) // pad 16 byte block
                            {
                                ended = true;
                                for (int i = 0; i < 16; i++)
                                {
                                    message[i] = 16;
                                }
                            }
                            else if (readBytes % 16 != 0) // pad missing bytes according to PKCS#7
                            {
                                ended = true;
                                for (int i = 0; i < 16 - readBytes; i++)
                                {
                                    message[readBytes + i] = (byte)(16 - readBytes);
                                }
                            }
                            aes.Encrypt(message, expandedKey);

                            fsOut.Write(message, 0, 16);
                        }
                    }
            }
            else
            {
                using (FileStream fs = input.OpenRead())
                    using (FileStream fsOut = output.Open(FileMode.Create, FileAccess.ReadWrite))
                    {
                        byte[] salt = new byte[8];

                        fs.Read(salt, 0, 8);                              // Read first 8 bytes from input file
                        if (Encoding.ASCII.GetString(salt) == "Salted__") // Check if file is Salted
                        {
                            fs.Read(salt, 0, 8);                          // Read next 8 bytes into salt array
                        }

                        int iterations = 10000;

                        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passphrase, salt, iterations, HashAlgorithmName.SHA256);

                        byte[] message = new byte[16];

                        byte[] expandedKey = new byte[176];

                        aes.KeyExpansion(key.GetBytes(16), expandedKey);

                        bool ended = false;

                        // Store previous decrypted block
                        byte[] final = new byte[16];

                        while (!ended)
                        {
                            int readBytes = fs.Read(message, 0, 16);
                            if (readBytes == 0)
                            {
                                ended = true;
                                // Check padding correctness
                                int n = final[15];
                                int b = 16;
                                if (n == 0 || n > 16)
                                {
                                    Console.ForegroundColor = ConsoleColor.Red;
                                    Console.Error.WriteLine("Bad decrypt (is the supplied passphrase correct?)");
                                    Console.ResetColor();
                                    return;
                                }
                                for (int i = 0; i < n; i++)
                                {
                                    if (final[--b] != n)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Red;
                                        Console.Error.WriteLine("Bad decrypt (is the supplied passphrase correct?)");
                                        Console.ResetColor();
                                        return;
                                    }
                                }

                                // Strip padding
                                fsOut.SetLength(fsOut.Length - n);
                                fsOut.Close();
                            }
                            else
                            {
                                aes.Decrypt(message, expandedKey);

                                for (int i = 0; i < 16; i++)
                                {
                                    final[i] = message[i];
                                }
                                fsOut.Write(message, 0, 16);
                            }
                        }
                    }
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine($"The selected operation was performed in {elapsedMs} ms.");
        }
示例#11
0
 public static void Initialize(this IAes aes, bool forEncryption, byte[] key) =>
 aes.Initialize(forEncryption, new ArraySegment <byte>(key));
示例#12
0
 public DocumentOverload(IAes aes, ISecureHash secureHash, ICompression compression, IPassword password, IFileProxy fileProxy)
     : base(aes, secureHash, compression, password, fileProxy)
 {
 }
示例#13
0
 public AesCtrMode(IAes aes, byte[] iv, int offset, int length)
     : this(aes, new ArraySegment <byte>(iv, offset, length))
 {
 }
示例#14
0
 public AesCtrMode(IAes aes, byte[] iv)
     : this(aes, new ArraySegment <byte>(iv))
 {
 }