コード例 #1
0
        public char[] Decrypt(char[] cipherText, List <dynamic> key)
        {
            char[,,] grid;
            char[]    plainText;
            char      character;
            int       i, j;
            GridShape gridShape;

            gridShape = new GridShape(key[0], key[1]);

            grid = new char[(cipherText.Length - 1) / gridShape.Area + 1, gridShape.Width, gridShape.Height];

            //Filling up the grid by columns from the cipher text
            using (var myGrid = new GridByColumns(grid))
            {
                for (i = 0; i < cipherText.Length; i++)
                {
                    myGrid[i] = cipherText[i];
                }
            }

            //Reading from grid row by row to fill plainText, ignoring underscore fillers
            using (var myGrid = new GridByRows(grid))
            {
                plainText = new char[myGrid.Size];
                for (i = 0, j = 0; i < myGrid.Size; i++)
                {
                    character = myGrid[i];
                    if ('_' != character)
                    {
                        plainText[j] = character;
                        j++;
                    }
                }
            }

            return(plainText);
        }
コード例 #2
0
        public char[] Encrypt(char[] plainText, List <dynamic> key)
        {
            char[,,] grid;
            char[]    cipherText;
            int       i;
            GridShape gridShape;

            gridShape = new GridShape(key[0], key[1]);

            //Array of 2D grids, size as given by gridShape
            grid = new char[(plainText.Length - 1) / gridShape.Area + 1, gridShape.Width, gridShape.Height];

            //Filling up the grid by rows, first with the plain text then underscores as fillers
            using (var myGrid = new GridByRows(grid))
            {
                for (i = 0; i < plainText.Length; i++)
                {
                    myGrid[i] = plainText[i];
                }
                for (; i < myGrid.Size; i++)
                {
                    myGrid[i] = '_';
                }
            }

            //Reading from grid column by column to fill cipherText
            using (var myGrid = new GridByColumns(grid))
            {
                cipherText = new char[myGrid.Size];
                for (i = 0; i < myGrid.Size; i++)
                {
                    cipherText[i] = myGrid[i];
                }
            }

            return(cipherText);
        }