private static Func <IList <char>, Func <Dictionary <char, string>, Action <Dictionary <string, char> > > > FillMatrixFunc() => key => characterPositionsInMatrix => positionCharacterInMatrix => { var matrix = new char[5, 5]; var keyPosition = 0; var charPosition = 0; var alphabetPlayFair = AlphabetDictionaryGenerator.Generate().Keys.ToList(); alphabetPlayFair.Remove('j'); for (var i = 0; i < 5; i++) { for (var j = 0; j < 5; j++) { if (charPosition < key.Count) { matrix[i, j] = key[charPosition]; //fill matrix with key alphabetPlayFair.Remove(key[charPosition]); charPosition++; } else { //key finished...fill with rest of alphabet matrix[i, j] = alphabetPlayFair[keyPosition]; keyPosition++; } var position = i.ToString() + j.ToString(); //store character positions in dictionary to avoid searching everytime characterPositionsInMatrix.Add(matrix[i, j], position); positionCharacterInMatrix.Add(position, matrix[i, j]); } } };
private static Func <int[, ], Func <string, Func <CryptoMode, string> > > ProcessFunc() => key => message => mode => { var sbRet = new StringBuilder(); var matrix = new MatrixClass(key); var alphabet = AlphabetDictionaryGenerator.Generate(); if (mode == CryptoMode.Decrypt) { matrix = matrix.Inverse(); } var pos = 0; var matrixSize = key.GetLength(0); while (pos < message.Length) { for (var i = 0; i < matrixSize; i++) { var charPosition = 0; for (var j = 0; j < matrixSize; j++) { charPosition += (int)matrix[j, i].Numerator * alphabet[message.Substring(pos, matrixSize)[j]]; } sbRet.Append(alphabet.Keys.ElementAt(charPosition % 26)); } pos += matrixSize; } return(sbRet.ToString()); };
private void ShuffleAlphabet() { var r = new Random(DateTime.Now.Millisecond); var alphabetKeys = AlphabetDictionaryGenerator.Generate().Keys; var alphabetCopy = alphabetKeys.ToList(); foreach (var character in alphabetKeys) { var characterPosition = r.Next(0, alphabetCopy.Count); var randomCharacter = alphabetCopy[characterPosition]; AlphabetShuffled.Add(character, randomCharacter); AlphabetShuffledReverse.Add(randomCharacter, character); alphabetCopy.RemoveAt(characterPosition); } }
private static Func <int, Func <string, Func <CryptoMode, string> > > ProcessFunc() => key => message => mode => { var sbRet = new StringBuilder(); var alphabet = AlphabetDictionaryGenerator.Generate(); foreach (var c in message) { var res = AlgorithmUtils.GetAlphabetPositionFunc() (alphabet[c]) /*char position*/ (key) (mode); /*encryption algorithm mode*/ sbRet.Append(alphabet.Keys.ElementAt(res % 26)); } return(sbRet.ToString()); };
private static Func <string, Func <string, Func <CryptoMode, string> > > ProcessFunc() => key => message => mode => { key = key.ToString().ToLower().Replace(" ", ""); key = DuplicateKeyFunc()(key)(message); return(AlgorithmUtils.Shift(message, key, mode, AlphabetDictionaryGenerator.Generate())); };
private static Func <string, Func <string, Func <CryptoMode, string> > > ProcessFunc() => key => message => mode => { var k = DuplicateKeyFunc()(key)(message); return(AlgorithmUtils.Shift(message, k, mode, AlphabetDictionaryGenerator.Generate())); };