示例#1
0
        protected List <int> BytesToIndicesAndFill(byte[] plain, int columnCount, bool extraRow = false)
        {
            //we don't care about these values, it's probably better that
            // we don't use the key-stream to generate them to not leak them at all.
            // In some way, they will also act as pepper, so that's cool
            IRng       rng            = new SysRng();
            int        remainder      = plain.Length % columnCount;
            int        rowSize        = columnCount;
            int        missingEntries = rowSize - remainder; //remainder == 0 ? 0 : rowSize - remainder;
            List <int> plainIndices   = plain.Select(ByteToIndex).ToList();

            if (extraRow)
            {
                missingEntries += rowSize;
            }

            for (int i = 0; i < missingEntries - 1; ++i)
            {
                plainIndices.Add(rng.Next(_map.Count));
            }

            Debug.Assert(missingEntries < _map.Count);
            plainIndices.Add(missingEntries);

            return(plainIndices);
        }
示例#2
0
        public override byte[] Encrypt(byte[] plain, byte[] key)
        {
            //Let's encrypt 8x8 or 16x16 at a time
            //Insert padding amount at the beginning of the cypher so we know how much to remove
            _matrixDimension = 8;
            //if (plain.Length >= 16 * 16)
            //    _matrixDimension = 16;
            _matrixSize = _matrixDimension * _matrixDimension;
            //fix this
            List <int> plainIxs = BytesToIndices(plain).ToList();

            //calculate fill
            int fill  = _matrixDimension - plain.Length % _matrixDimension;
            var noise = new SysRng();

            for (int i = 0; i < fill; ++i)
            {
                plainIxs.Add(noise.Next(0, Alphabet.Count));
            }

            List <int> cypherIxs = new List <int>(plainIxs.Count + 4);

            cypherIxs.AddRange(EncodeFill(fill, Alphabet.Count));

            IRng rng = key.KeyToRand();

            for (int i = 0; i < plainIxs.Count; i += _matrixDimension)
            {
                cypherIxs.AddRange(Encrypt(new ListSpan <int>(plainIxs, i, i + _matrixDimension), rng));
            }

            return(IndicesToBytes(cypherIxs));
        }