public string Decrypt(IEncryptionParameters encParams) { Console.WriteLine("Enter file location and name of text file containing encrypted text:"); string cipherTextFileName = encParams.GetEncryptedFileName(); //to take in and store input for file name string cipherText = System.IO.File.ReadAllText(cipherTextFileName); //Load the text contents to be decrypted Console.WriteLine(); Console.WriteLine("Enter key for decryption:"); string key = encParams.GetEncryptionKey(); int offsetFactor = CalculateOffsetFactor(key); //enter the key as 8 character string via user input and then calculate the offset value using it string text = ""; int charNum = 0; //to store the ascii values of each character char charVal = ' '; //to store each of the characters themselves constituting the actual string of deciphered text //initialize values for (int i = 0; i < cipherText.Length; i++) //looping through each character in the text to be encrypted { charNum = (int)cipherText[i]; //store the ASCII value of the current character if (charNum != 32) //check if it is a space character, which is to be ignored while encrypting { charNum -= offsetFactor; //minus offset factor if (charNum < 33) { charNum += 94; } //making sure the ASCII code does not go out of the given bounds for english characters } charVal = (char)charNum; //convert the offset ASCII value to a character text += charVal; //append this character to a string containing the encrypted text } return(text); }
public void Encrypt(IEncryptionParameters encParams) { Console.WriteLine("Enter file location and name for text to be encrypted:"); string fileName = encParams.GetDecryptedFileName(); //to take in and store input for file name string text = System.IO.File.ReadAllText(fileName); //load the text contents to be encrypted string key = GenerateKey(); //generate an 8 character key Console.WriteLine(); Console.WriteLine("Key:"); Console.WriteLine(); Console.WriteLine(key); int offsetFactor = CalculateOffsetFactor(key); //use the key generated earlier to get an offset factor string cipherText = ""; int charNum = 0; //to store the ascii values of each character char charVal = ' '; //to store each of the characters themselves constituting the actual string of ciphered text //initialize values to be used for (int i = 0; i < text.Length; i++) //looping through each character in the text to be encrypted { charNum = (int)text[i]; //store the ASCII value of the current character if (charNum != 32) //check if it is a space character, which is to be ignored while encrypting { charNum += offsetFactor; //add offset factor if (charNum > 126) { charNum -= 94; } //making sure the ASCII code does not go out of the given bounds for english characters } charVal = (char)charNum; //convert the offset ASCII value to a character cipherText += charVal; //append this character to a string containing the encypted text } Console.WriteLine(); Console.WriteLine("Enter a file name and location for a new text file to store encrypted text:"); string newFileName = encParams.GetEncryptedFileName(); System.IO.File.WriteAllText(newFileName, cipherText); //write the encrypted text to the given file location Console.WriteLine(); Console.WriteLine("File encrypted and stored."); }