Exemplo n.º 1
0
        public EncryptingResultModel Encrypt(byte[] input, byte[] keyInput, int w, int r, int b)
        {
            var key = GetKey(keyInput, b);

            for (int i = 0; i < keyInput.Length; i++)
            {
                keyInput[i] = 0;
            }

            var helper = new RC5Helper(w, r, key);

            input = GetFullInput(input, helper.BlockSize);

            var IV         = GetInitializeVector(helper.BlockSize);
            var resultData = new byte[input.Length];
            var result     = new EncryptingResultModel {
                IV = EncryptInitializeVector(IV, key, w, r)
            };

            for (int i = 0; i < input.Length; i += helper.BlockSize)
            {
                var currentBlock = new byte[helper.BlockSize];
                Array.Copy(input, i, currentBlock, 0, helper.BlockSize);

                currentBlock = XOR(IV, currentBlock, helper.W);
                IV           = EncryptBlock(currentBlock, helper);

                Array.Copy(IV, 0, resultData, i, helper.BlockSize);
            }
            result.EncryptedData = resultData;

            return(result);
        }
Exemplo n.º 2
0
        private EncryptingResultModel GetEncryptingResultModel(byte[] input, int blockSize)
        {
            var result = new EncryptingResultModel();

            result.IV            = new byte[blockSize];
            result.EncryptedData = new byte[input.Length - blockSize];
            Array.Copy(input, 0, result.IV, 0, blockSize);
            Array.Copy(input, blockSize, result.EncryptedData, 0, result.EncryptedData.Length);
            return(result);
        }
Exemplo n.º 3
0
        public void SaveEncriptingResut(EncryptingResultModel encryptingResult, FileInfo file)
        {
            var fileData = new byte[encryptingResult.IV.Length + encryptingResult.EncryptedData.Length];

            Array.Copy(encryptingResult.IV, 0, fileData, 0, encryptingResult.IV.Length);
            Array.Copy(encryptingResult.EncryptedData, 0, fileData, encryptingResult.IV.Length, encryptingResult.EncryptedData.Length);

            var encodedFilePath = file.FullName.Insert(file.FullName.Length - file.Extension.Length, "_encrypted");

            File.WriteAllBytes(encodedFilePath, fileData);
        }