Exemplo 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));
        }
Exemplo 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));
        }
Exemplo 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);
        }
Exemplo 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)));
        }
Exemplo 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);
     }
 }
Exemplo 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));
        }
Exemplo 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);
     }
 }
Exemplo 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)));
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo n.º 11
0
 private byte[] XorWithBlockSizePadding(BigInteger beforeXorBlock, BigInteger block)
 {
     byte[] xorResult = BigIntegerExtensions.UnsignedToBytes(beforeXorBlock ^ block);
     return(BlockCipherSupport.PadWithZeros(xorResult, BlockSize));
 }