예제 #1
0
        public byte[] Encrypt(byte[] plainBytes, string password, Schema schemaVersion)
        {
            this.configureSettings (schemaVersion);

            PayloadComponents components = new PayloadComponents();
            components.schema = new byte[] {(byte)schemaVersion};
            components.options = new byte[] {(byte)this.options};
            components.salt = this.generateRandomBytes (Cryptor.saltLength);
            components.hmacSalt = this.generateRandomBytes (Cryptor.saltLength);
            components.iv = this.generateRandomBytes (Cryptor.ivLength);

            byte[] key = this.generateKey (components.salt, password);

            switch (this.aesMode) {
                case AesMode.CTR:
                    components.ciphertext = this.encryptAesCtrLittleEndianNoPadding(plainBytes, key, components.iv);
                    break;

                case AesMode.CBC:
                    components.ciphertext = this.encryptAesCbcPkcs7(plainBytes, key, components.iv);
                    break;
            }

            components.hmac = this.generateHmac(components, password);

            List<byte> binaryBytes = new List<byte>();
            binaryBytes.AddRange (this.assembleHeader(components));
            binaryBytes.AddRange (components.ciphertext);
            binaryBytes.AddRange (components.hmac);

            return binaryBytes.ToArray();
        }
예제 #2
0
        public string Encrypt(string plaintext, string password, Schema schemaVersion)
        {
            base.configureSettings(schemaVersion);
            byte[]            bytes            = base.TextEncoding.GetBytes(plaintext);
            PayloadComponents payloadComponent = new PayloadComponents()
            {
                schema   = new byte[] { (byte)schemaVersion },
                options  = new byte[] { (byte)this.options },
                salt     = this.generateRandomBytes(8),
                hmacSalt = this.generateRandomBytes(8),
                iv       = this.generateRandomBytes(16)
            };

            byte[]  numArray = base.generateKey(payloadComponent.salt, password);
            AesMode aesMode  = this.aesMode;

            if (aesMode == AesMode.CTR)
            {
                payloadComponent.ciphertext = base.encryptAesCtrLittleEndianNoPadding(bytes, numArray, payloadComponent.iv);
            }
            else if (aesMode == AesMode.CBC)
            {
                payloadComponent.ciphertext = this.encryptAesCbcPkcs7(bytes, numArray, payloadComponent.iv);
            }
            payloadComponent.hmac = base.generateHmac(payloadComponent, password);
            List <byte> nums = new List <byte>();

            nums.AddRange(base.assembleHeader(payloadComponent));
            nums.AddRange(payloadComponent.ciphertext);
            nums.AddRange(payloadComponent.hmac);
            return(Convert.ToBase64String(nums.ToArray()));
        }
예제 #3
0
        // Token: 0x06000013 RID: 19 RVA: 0x00004800 File Offset: 0x00002A00
        public string Decrypt(string encryptedBase64, string password)
        {
            PayloadComponents payloadComponents = this.unpackEncryptedBase64Data(encryptedBase64);
            bool   flag = !this.hmacIsValid(payloadComponents, password);
            string result;

            if (flag)
            {
                result = "";
            }
            else
            {
                byte[]  key     = base.generateKey(payloadComponents.salt, password);
                byte[]  bytes   = new byte[0];
                AesMode aesMode = this.aesMode;
                if (aesMode != AesMode.CTR)
                {
                    if (aesMode == AesMode.CBC)
                    {
                        bytes = this.decryptAesCbcPkcs7(payloadComponents.ciphertext, key, payloadComponents.iv);
                    }
                }
                else
                {
                    bytes = base.encryptAesCtrLittleEndianNoPadding(payloadComponents.ciphertext, key, payloadComponents.iv);
                }
                result = Encoding.UTF8.GetString(bytes);
            }
            return(result);
        }
        public byte[] Decrypt(byte[] encryptedBytes, string password)
        {
            PayloadComponents components = this.unpackEncryptedData(encryptedBytes);

            if (!this.hmacIsValid(components, password))
            {
                return(null);
            }

            byte[] key = this.generateKey(components.salt, password);

            byte[] plaintextBytes = new byte[0];

            switch (this.aesMode)
            {
            case AesMode.CTR:
                // Yes, we are "encrypting" here.  CTR uses the same code in both directions.
                plaintextBytes = this.encryptAesCtrLittleEndianNoPadding(components.ciphertext, key, components.iv);
                break;

            case AesMode.CBC:
                plaintextBytes = this.decryptAesCbcPkcs7(components.ciphertext, key, components.iv);
                break;
            }

            return(plaintextBytes);
        }
예제 #5
0
        // Token: 0x06000017 RID: 23 RVA: 0x00004098 File Offset: 0x00002298
        private bool hmacIsValid(PayloadComponents components, string password)
        {
            byte[] array = base.generateHmac(components, password);
            bool   flag  = array.Length != components.hmac.Length;
            bool   result;

            if (flag)
            {
                result = false;
            }
            else
            {
                for (int i = 0; i < components.hmac.Length; i++)
                {
                    bool flag2 = array[i] != components.hmac[i];
                    if (flag2)
                    {
                        result = false;
                        return(result);
                    }
                }
                result = true;
            }
            return(result);
        }
예제 #6
0
        private PayloadComponents unpackEncryptedBase64Data(string encryptedBase64)
        {
            PayloadComponents array = new PayloadComponents();
            List <byte>       nums  = new List <byte>();

            nums.AddRange(Convert.FromBase64String(encryptedBase64));
            int length = 0;

            array.schema = nums.GetRange(0, 1).ToArray();
            length++;
            base.configureSettings((Schema)nums[0]);
            array.options = nums.GetRange(1, 1).ToArray();
            length++;
            array.salt         = nums.GetRange(length, 8).ToArray();
            length            += (int)array.salt.Length;
            array.hmacSalt     = nums.GetRange(length, 8).ToArray();
            length            += (int)array.hmacSalt.Length;
            array.iv           = nums.GetRange(length, 16).ToArray();
            length            += (int)array.iv.Length;
            array.headerLength = length;
            array.ciphertext   = nums.GetRange(length, nums.Count - 32 - array.headerLength).ToArray();
            length            += (int)array.ciphertext.Length;
            array.hmac         = nums.GetRange(length, 32).ToArray();
            return(array);
        }
예제 #7
0
        private bool hmacIsValid(PayloadComponents components, string password)
        {
            bool flag;

            byte[] numArray = base.generateHmac(components, password);
            if ((int)numArray.Length == (int)components.hmac.Length)
            {
                int num = 0;
                while (num < (int)components.hmac.Length)
                {
                    if (numArray[num] == components.hmac[num])
                    {
                        num++;
                    }
                    else
                    {
                        flag = false;
                        return(flag);
                    }
                }
                flag = true;
            }
            else
            {
                flag = false;
            }
            return(flag);
        }
예제 #8
0
        public string Decrypt(string encryptedBase64, string password)
        {
            string            str;
            PayloadComponents payloadComponent = this.unpackEncryptedBase64Data(encryptedBase64);

            if (this.hmacIsValid(payloadComponent, password))
            {
                byte[]  numArray  = base.generateKey(payloadComponent.salt, password);
                byte[]  numArray1 = new byte[0];
                AesMode aesMode   = this.aesMode;
                if (aesMode == AesMode.CTR)
                {
                    numArray1 = base.encryptAesCtrLittleEndianNoPadding(payloadComponent.ciphertext, numArray, payloadComponent.iv);
                }
                else if (aesMode == AesMode.CBC)
                {
                    numArray1 = this.decryptAesCbcPkcs7(payloadComponent.ciphertext, numArray, payloadComponent.iv);
                }
                str = Encoding.UTF8.GetString(numArray1);
            }
            else
            {
                str = "";
            }
            return(str);
        }
예제 #9
0
        // Token: 0x0600000C RID: 12 RVA: 0x00004588 File Offset: 0x00002788
        protected byte[] assembleHeader(PayloadComponents components)
        {
            List <byte> list = new List <byte>();

            list.AddRange(components.schema);
            list.AddRange(components.options);
            list.AddRange(components.salt);
            list.AddRange(components.hmacSalt);
            list.AddRange(components.iv);
            return(list.ToArray());
        }
예제 #10
0
        private bool hmacIsValid(PayloadComponents components, string password)
        {
            byte[] generatedHmac = this.generateHmac (components, password);

            if (generatedHmac.Length != components.hmac.Length) {
                return false;
            }

            for (int i = 0; i < components.hmac.Length; i++) {
                if (generatedHmac[i] != components.hmac[i]) {
                    return false;
                }
            }
            return true;
        }
        private bool hmacIsValid(PayloadComponents components, string password)
        {
            byte[] generatedHmac = this.generateHmac(components, password);

            if (generatedHmac.Length != components.hmac.Length)
            {
                return(false);
            }

            for (int i = 0; i < components.hmac.Length; i++)
            {
                if (generatedHmac[i] != components.hmac[i])
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #12
0
        // Token: 0x0600000B RID: 11 RVA: 0x000044B8 File Offset: 0x000026B8
        protected byte[] generateHmac(PayloadComponents components, string password)
        {
            List <byte> list = new List <byte>();
            bool        flag = this.hmac_includesHeader;

            if (flag)
            {
                list.AddRange(this.assembleHeader(components));
            }
            list.AddRange(components.ciphertext);
            byte[]        key           = this.generateKey(components.hmacSalt, password);
            HMAC          hmac          = null;
            HmacAlgorithm hmacAlgorithm = this.hmac_algorithm;

            if (hmacAlgorithm != HmacAlgorithm.SHA1)
            {
                if (hmacAlgorithm == HmacAlgorithm.SHA256)
                {
                    hmac = new HMACSHA256(key);
                }
            }
            else
            {
                hmac = new HMACSHA1(key);
            }
            List <byte> list2 = new List <byte>();

            list2.AddRange(hmac.ComputeHash(list.ToArray()));
            bool flag2 = this.hmac_includesPadding;

            if (flag2)
            {
                for (int i = list2.Count; i < 32; i++)
                {
                    list2.Add(0);
                }
            }
            return(list2.ToArray());
        }
예제 #13
0
        // Token: 0x0600001A RID: 26 RVA: 0x00004124 File Offset: 0x00002324
        public string Encrypt(string plaintext, string password, Schema schemaVersion)
        {
            base.configureSettings(schemaVersion);
            byte[]            bytes             = base.TextEncoding.GetBytes(plaintext);
            PayloadComponents payloadComponents = default(PayloadComponents);

            payloadComponents.schema = new byte[]
            {
                (byte)schemaVersion
            };
            payloadComponents.options = new byte[]
            {
                (byte)this.options
            };
            payloadComponents.salt     = this.generateRandomBytes(8);
            payloadComponents.hmacSalt = this.generateRandomBytes(8);
            payloadComponents.iv       = this.generateRandomBytes(16);
            byte[]  key     = base.generateKey(payloadComponents.salt, password);
            AesMode aesMode = this.aesMode;

            if (aesMode != AesMode.CTR)
            {
                if (aesMode == AesMode.CBC)
                {
                    payloadComponents.ciphertext = this.encryptAesCbcPkcs7(bytes, key, payloadComponents.iv);
                }
            }
            else
            {
                payloadComponents.ciphertext = base.encryptAesCtrLittleEndianNoPadding(bytes, key, payloadComponents.iv);
            }
            payloadComponents.hmac = base.generateHmac(payloadComponents, password);
            List <byte> list = new List <byte>();

            list.AddRange(base.assembleHeader(payloadComponents));
            list.AddRange(payloadComponents.ciphertext);
            list.AddRange(payloadComponents.hmac);
            return(Convert.ToBase64String(list.ToArray()));
        }
        protected byte[] generateHmac(PayloadComponents components, string password)
        {
            List <byte> hmacMessage = new List <byte>();

            if (this.hmac_includesHeader)
            {
                hmacMessage.AddRange(this.assembleHeader(components));
            }
            hmacMessage.AddRange(components.ciphertext);

            byte[] key = this.generateKey(components.hmacSalt, password);

            HMAC hmacAlgo = null;

            switch (this.hmac_algorithm)
            {
            case HmacAlgorithm.SHA1:
                hmacAlgo = new HMACSHA1(key);
                break;

            case HmacAlgorithm.SHA256:
                hmacAlgo = new HMACSHA256(key);
                break;
            }
            List <byte> hmac = new List <byte>();

            hmac.AddRange(hmacAlgo.ComputeHash(hmacMessage.ToArray()));

            if (this.hmac_includesPadding)
            {
                for (int i = hmac.Count; i < Cryptor.hmac_length; i++)
                {
                    hmac.Add(0x00);
                }
            }

            return(hmac.ToArray());
        }
예제 #15
0
        public string Encrypt(string plaintext, string password, Schema schemaVersion)
        {
            this.configureSettings(schemaVersion);

            byte[] plaintextBytes = TextEncoding.GetBytes(plaintext);

            PayloadComponents components = new PayloadComponents();

            components.schema   = new byte[] { (byte)schemaVersion };
            components.options  = new byte[] { (byte)this.options };
            components.salt     = this.generateRandomBytes(Cryptor.saltLength);
            components.hmacSalt = this.generateRandomBytes(Cryptor.saltLength);
            components.iv       = this.generateRandomBytes(Cryptor.ivLength);

            byte[] key = this.generateKey(components.salt, password);

            switch (this.aesMode)
            {
            case AesMode.CTR:
                components.ciphertext = this.encryptAesCtrLittleEndianNoPadding(plaintextBytes, key, components.iv);
                break;

            case AesMode.CBC:
                components.ciphertext = this.encryptAesCbcPkcs7(plaintextBytes, key, components.iv);
                break;
            }

            components.hmac = this.generateHmac(components, password);

            List <byte> binaryBytes = new List <byte>();

            binaryBytes.AddRange(this.assembleHeader(components));
            binaryBytes.AddRange(components.ciphertext);
            binaryBytes.AddRange(components.hmac);

            return(Convert.ToBase64String(binaryBytes.ToArray()));
        }
예제 #16
0
        protected byte[] assembleHeader(PayloadComponents components)
        {
            List<byte> headerBytes = new List<byte>();
            headerBytes.AddRange (components.schema);
            headerBytes.AddRange (components.options);
            headerBytes.AddRange (components.salt);
            headerBytes.AddRange (components.hmacSalt);
            headerBytes.AddRange (components.iv);

            return headerBytes.ToArray();
        }
예제 #17
0
        protected byte[] generateHmac(PayloadComponents components, string password)
        {
            List<byte> hmacMessage = new List<byte>();
            if (this.hmac_includesHeader) {
                hmacMessage.AddRange (this.assembleHeader(components));
            }
            hmacMessage.AddRange (components.ciphertext);

            byte[] key = this.generateKey (components.hmacSalt, password);

            HMAC hmacAlgo = null;
            switch (this.hmac_algorithm) {
                case HmacAlgorithm.SHA1:
                    hmacAlgo = new HMACSHA1(key);
                    break;

                case HmacAlgorithm.SHA256:
                    hmacAlgo = new HMACSHA256(key);
                    break;
            }
            List<byte> hmac = new List<byte>();
            hmac.AddRange (hmacAlgo.ComputeHash(hmacMessage.ToArray()));

            if (this.hmac_includesPadding) {
                for (int i = hmac.Count; i < Cryptor.hmac_length; i++) {
                    hmac.Add (0x00);
                }
            }

            return hmac.ToArray ();
        }