Esempio n. 1
0
        private byte[] XorKeySizePadding(BigInteger cipheredAgainPreviousBlock, BigInteger block)
        {
            byte[] xorResult = BigIntegerExtensions.UnsignedToBytes(cipheredAgainPreviousBlock ^ block);
            int    keySize   = rsa.ExportParameters().Modulus.Length;

            return(BlockCipherSupport.PadWithZeros(xorResult, keySize));
        }
Esempio n. 2
0
        public byte[] Cipher(byte[] data)
        {
            byte[]        dataToDivide   = BlockCipherSupport.AddPadding(data, BlockSize);
            List <byte[]> blocks         = BlockCipherSupport.DivideIntoBlocks(dataToDivide, BlockSize);
            List <byte[]> cipheredBlocks = CipherBlocks(blocks);

            return(BlockCipherSupport.ConcatenateBlocks(cipheredBlocks));
        }
Esempio n. 3
0
        public List <Chunk> DecipherWithoutFiltering(List <Chunk> chunks)
        {
            byte[]       decompressedBytes = BlockCipherSupport.DecompressIDATs(chunks);
            byte[]       decipheredBytes   = blockCipher.Decipher(decompressedBytes);
            List <Chunk> resultIdats       = BlockCipherSupport.CompressIDATs(decipheredBytes);
            List <Chunk> resultChunks      = BlockCipherSupport.SwapIDATs(chunks, resultIdats);

            return(resultChunks);
        }
Esempio n. 4
0
        public byte[] Decipher(byte[] data)
        {
            RSAParameters parameters       = rsa.ExportParameters();
            int           keySize          = parameters.Modulus.Length;
            List <byte[]> blocks           = BlockCipherSupport.DivideIntoBlocks(data, keySize);
            List <byte[]> decipheredBlocks = DecipherBlocks(blocks);

            return(BlockCipherSupport.RemovePadding(BlockCipherSupport.ConcatenateBlocks(decipheredBlocks)));
        }
Esempio n. 5
0
 private void DecipherBlocks(List <byte[]> blocks)
 {
     for (int i = 0; i < blocks.Count; i++)
     {
         BigInteger nonceCounterXor      = InitializationVector ^ new BigInteger(i);
         byte[]     nonceCounterXorBytes = rsa.Encrypt(BigIntegerExtensions.UnsignedToBytes(nonceCounterXor));
         nonceCounterXor = BigIntegerExtensions.UnsignedFromBytes(nonceCounterXorBytes);
         BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]);
         block    ^= nonceCounterXor;
         blocks[i] = BlockCipherSupport.PadWithZeros(BigIntegerExtensions.UnsignedToBytes(block), BlockSize);
     }
 }
Esempio n. 6
0
        public byte[] Cipher(byte[] data)
        {
            byte[]        dataToDivide = BlockCipherSupport.AddPadding(data, BlockSize);
            List <byte[]> blocks       = BlockCipherSupport.DivideIntoBlocks(dataToDivide, BlockSize);

            for (int i = 0; i < blocks.Count; i++)
            {
                blocks[i] = rsa.Encrypt(blocks[i]);
            }

            return(BlockCipherSupport.ConcatenateBlocks(blocks));
        }
Esempio n. 7
0
 private void CipherBlocks(List <byte[]> blocks)
 {
     for (int i = 0; i < blocks.Count; i++)
     {
         BigInteger nonceCounterXor      = InitializationVector ^ new BigInteger(i);
         byte[]     nonceCounterXorBytes = rsa.Encrypt(BigIntegerExtensions.UnsignedToBytes(nonceCounterXor));
         nonceCounterXor = BigIntegerExtensions.UnsignedFromBytes(nonceCounterXorBytes);
         BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]);
         block ^= nonceCounterXor;
         RSAParameters parameters = rsa.ExportParameters();
         int           keySize    = parameters.Modulus.Length;
         blocks[i] = BlockCipherSupport.PadWithZeros(BigIntegerExtensions.UnsignedToBytes(block), keySize);
     }
 }
Esempio n. 8
0
        public byte[] Decipher(byte[] data)
        {
            RSAParameters parameters = rsa.ExportParameters();
            int           keySize    = parameters.Modulus.Length;
            List <byte[]> blocks     = BlockCipherSupport.DivideIntoBlocks(data, keySize);

            for (int i = 0; i < blocks.Count; i++)
            {
                blocks[i] = rsa.Decrypt(blocks[i]);
                blocks[i] = BlockCipherSupport.PadWithZeros(blocks[i], BlockSize);
            }

            return(BlockCipherSupport.RemovePadding(BlockCipherSupport.ConcatenateBlocks(blocks)));
        }
Esempio n. 9
0
        private List <byte[]> DecipherBlocks(List <byte[]> blocks)
        {
            List <byte[]> decipheredBlocks = new List <byte[]>(blocks.Count);

            for (int i = 0; i < blocks.Count; i++)
            {
                byte[]     cipheredPreviousBlock      = GetCipheredPreviousBlock(blocks, i);
                BigInteger cipheredAgainPreviousBlock = BigIntegerExtensions.UnsignedFromBytes(rsa.Encrypt(cipheredPreviousBlock)); // Cipher intended
                BigInteger block = BigIntegerExtensions.UnsignedFromBytes(blocks[i]);
                decipheredBlocks.Add(BigIntegerExtensions.UnsignedToBytes(cipheredAgainPreviousBlock ^ block));
                decipheredBlocks[i] = BlockCipherSupport.PadWithZeros(decipheredBlocks[i], BlockSize);
            }

            return(decipheredBlocks);
        }
Esempio n. 10
0
        private List <byte[]> DecipherBlocks(List <byte[]> blocks)
        {
            List <byte[]> decipheredBlocks = new List <byte[]>(blocks.Count);

            for (int i = 0; i < blocks.Count; i++)
            {
                var previousXor = CalculatePreviousXor(blocks, i, decipheredBlocks);
                decipheredBlocks.Add(rsa.Decrypt(blocks[i]));
                var decipheredBlock = BigIntegerExtensions.UnsignedFromBytes(decipheredBlocks[i]);
                decipheredBlock    ^= previousXor;
                decipheredBlocks[i] = BigIntegerExtensions.UnsignedToBytes(decipheredBlock).Take(BlockSize).ToArray();
                decipheredBlocks[i] = BlockCipherSupport.PadWithZeros(decipheredBlocks[i], BlockSize);
            }

            return(decipheredBlocks);
        }
Esempio n. 11
0
 private byte[] XorWithBlockSizePadding(BigInteger beforeXorBlock, BigInteger block)
 {
     byte[] xorResult = BigIntegerExtensions.UnsignedToBytes(beforeXorBlock ^ block);
     return(BlockCipherSupport.PadWithZeros(xorResult, BlockSize));
 }