Beispiel #1
0
 public CryptoData(Settings.EncryptionScope scope, byte[] data, byte[] key, byte[] iv)
 {
     this.scope = scope;
     this.data  = data;
     this.key   = key;
     this.iv    = iv;
 }
Beispiel #2
0
        /// <summary>
        /// Decrypts the data
        /// </summary>
        /// <param name="scope">Encryption scope</param>
        /// <param name="data">Data to decrypt</param>
        /// <param name="key">Key used</param>
        /// <param name="iv">IV used</param>
        /// <returns>Decrypted data</returns>
        public byte[] Decrypt(Settings.EncryptionScope scope, byte[] data, byte[] key, byte[] iv)
        {
            switch (scope)
            {
            case Settings.EncryptionScope.CurrentUser:

                return(ProtectedData.Unprotect(data, iv, DataProtectionScope.CurrentUser));

            case Settings.EncryptionScope.LocalMachine:

                return(ProtectedData.Unprotect(data, iv, DataProtectionScope.LocalMachine));

            case Settings.EncryptionScope.Portable:

                if (key == null)
                {
                    key = this.key;
                }
                using (var aes = GetAes().CreateDecryptor(key, iv))
                {
                    using (var cs = new CryptoStream(new MemoryStream(data), aes, CryptoStreamMode.Read))
                    {
                        var buffer = new byte[data.Length];
                        var length = 0;

                        do
                        {
                            var r = cs.Read(buffer, length, buffer.Length - length);
                            if (r == 0)
                            {
                                break;
                            }
                            length += r;
                        }while (true);

                        if (length != buffer.Length)
                        {
                            var buffer2 = new byte[length];
                            Array.Copy(buffer, buffer2, length);
                            Array.Clear(buffer, 0, length);
                            buffer = buffer2;
                        }

                        return(buffer);
                    }
                }

            case Settings.EncryptionScope.Unencrypted:
            default:

                return(data);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Compresses the data into a single byte array
        /// </summary>
        /// <param name="scope">Encryption scope</param>
        /// <param name="data">Optional inclusion of the data</param>
        /// <param name="key">Optional inclusion of the key</param>
        /// <param name="iv">Optional inclusion of the IV</param>
        /// <returns>Byte array of compressed data</returns>
        private byte[] Compress(Settings.EncryptionScope scope, byte[] data, byte[] key, byte[] iv)
        {
            var ivLength   = iv != null ? iv.Length : 0;
            var keyLength  = key != null ? key.Length : 0;
            var dataLength = data != null ? data.Length : 0;
            var buffer     = new byte[4 + dataLength + ivLength + keyLength];
            var i          = 1;

            buffer[i++] = (byte)scope;
            buffer[i++] = (byte)ivLength;
            buffer[i++] = (byte)keyLength;

            foreach (var b in new byte[][] { iv, key, data })
            {
                if (b != null && b.Length > 0)
                {
                    Array.Copy(b, 0, buffer, i, b.Length);
                    i += b.Length;
                }
            }

            return(buffer);
        }
Beispiel #4
0
 public Crypto(Settings.EncryptionScope scope, CryptoCompressionFlags flags, byte[] key)
 {
     this.scope = scope;
     this.flags = flags;
     this.key   = key;
 }
Beispiel #5
0
 public Crypto()
 {
     this.scope = Settings.EncryptionScope.CurrentUser;
     this.flags = CryptoCompressionFlags.All;
 }
Beispiel #6
0
        /// <summary>
        /// Encrypts the data
        /// </summary>
        /// <param name="scope">Encryption scope</param>
        /// <param name="data">Data to encrypt</param>
        /// <param name="key">Optional key to use</param>
        /// <param name="iv">Optional IV to use</param>
        /// <returns>Encrypted data that may be further processed</returns>
        public IData Encrypt(Settings.EncryptionScope scope, byte[] data, byte[] key = null, byte[] iv = null)
        {
            byte[] buffer;

            switch (scope)
            {
            case Settings.EncryptionScope.CurrentUser:

                if (iv == null)
                {
                    iv = GenerateIV();
                }
                buffer = ProtectedData.Protect(data, iv, DataProtectionScope.CurrentUser);

                break;

            case Settings.EncryptionScope.LocalMachine:

                if (iv == null)
                {
                    iv = GenerateIV();
                }
                buffer = ProtectedData.Protect(data, iv, DataProtectionScope.LocalMachine);

                break;

            case Settings.EncryptionScope.Portable:

                if (key == null)
                {
                    key = GenerateKey();
                }
                if (iv == null)
                {
                    iv = GenerateIV();
                }
                using (var aes = GetAes().CreateEncryptor(key, iv))
                {
                    using (var ms = new MemoryStream((data.Length / aes.OutputBlockSize + 1) * aes.OutputBlockSize))
                    {
                        using (var cs = new CryptoStream(ms, aes, CryptoStreamMode.Write))
                        {
                            cs.Write(data, 0, data.Length);
                            cs.FlushFinalBlock();
                        }
                        buffer = ms.ToArray();
                    }
                }

                break;

            case Settings.EncryptionScope.Unencrypted:
            default:

                buffer = data;

                break;
            }

            return(new CryptoData(scope, buffer, key, iv));
        }