Exemple #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));
        }
Exemple #2
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);
     }
 }
Exemple #3
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);
     }
 }
Exemple #4
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)));
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
0
 private byte[] XorWithBlockSizePadding(BigInteger beforeXorBlock, BigInteger block)
 {
     byte[] xorResult = BigIntegerExtensions.UnsignedToBytes(beforeXorBlock ^ block);
     return(BlockCipherSupport.PadWithZeros(xorResult, BlockSize));
 }