I'm working on this assignment where I'm taking a user input string, a key of the same or greater length, and using that to perform a Running Key cipher to encrypt and decrypt the text. Encryption is working, but decryption is not.
Looking manually at the Running Key table, I found that "ice" with a key of "did" would encrypt to "lkh" and that checks out. Looking back at the table, I found that for "lkh" to be turned into "ice", the key would have to change to "xsx" and for a moment, thought it'd be easy because I mistakenly thought it was "sxs" which is "did" with each letter shifted forward 15 letters. It's actually more arbitrary than that with the "d"'s shifting 20 letters and the "i" shifting only 10 letters to make "xsx".
I'm not sure what to put in my decrypt(), getDecryptedText(), or decryptionKey() functions to make this work, or if I was even on the right track trying to shift the letters. I imagine I must be, but my current thinking is that it may take some sort of loop to determine how many letters each character in the key should shift forward to.
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <cctype>
// Running Key Cipher Explanation
// http://practicalcryptography.com/ciphers/classical-era/running-key/
void encrypt(std::string&, std::string&, std::string&);
void decrypt(std::string&, std::string&, std::string&);
char getEncryptedText(char p, char k);
char getDecryptedText(char p, char k);
std::string decryptionKey(std::string&);
void getKeyIndex(int &i, std::string &key);
int main() {
// Initialization
std::string input;
std::string key;
std::string encryptedText;
std::string decryptedText;
// Assignment
std::cout << "Enter secret message: ";
std::getline(std::cin, input);
std::cout << "Enter key (longer than message): ";
std::getline(std::cin, key);
// Remove spaces
input.erase(remove_if(input.begin(), input.end(), isspace), input.end());
key.erase(remove_if(key.begin(), key.end(), isspace), key.end());
// Exit if key length < secret text
if (key.length() < input.length()) {
std::cout << "The encryption key must be longer than the message."
<< std::endl;
return 1;
}
// Encrypt the text
encrypt(input, key, encryptedText);
// Display encrypted text to user
std::cout << "\nThe encrypted text is:" << std::endl;
std::cout << encryptedText << std::endl;
// Decrypt the text
decrypt(encryptedText, key, decryptedText);
// Display decrypted text to user
std::cout << "\nThe decrypted text is:" << std::endl;
std::cout << decryptedText << std::endl;
return 0;
}
void encrypt(std::string &input, std::string &key, std::string &encryptedText) {
std::string::iterator i;
std::string::iterator j;
// Contains the encrypted version of the text
encryptedText = "";
// Encrypt every character in the input string
for(i = key.begin(), j = input.begin(); j < input.end();) {
// Remove non-letters from text
if (!isalpha(*j)) {
j++;
continue;
}
// get encrypted char
encryptedText += getEncryptedText(tolower(*j),tolower(*i));
i++;
j++;
}
}
char getEncryptedText(char p, char k) {
// Number to be converted into the nth letter in the alphabet
int encryptedText;
encryptedText = p + k;
if (encryptedText >= 219) {
return (char) (encryptedText - 123);
}
return (char)(encryptedText - 97);
}
void decrypt(std::string &encryptedText, std::string &key, std::string &decryptedText) {
std::string::iterator i;
std::string::iterator j;
// Contains the decrypted version of the text
decryptedText = "";
// Change key to decrypt
key = decryptionKey(key);
// Decrypt every character in the input string
for(i = key.begin(), j = encryptedText.begin(); j < encryptedText.end();) {
// get decrypted char
decryptedText += getDecryptedText(tolower(*j),tolower(*i));
i++;
j++;
}
}
char getDecryptedText(char p, char k) {
// Number to be converted into the nth letter in the alphabet
int decryptedText;
decryptedText = p + k;
// If it gets passed z, go back to a
if (decryptedText >= 219) {
return (char) (decryptedText - 123);
}
return (char)(decryptedText - 98);
}
std::string decryptionKey(std::string &key) {
for (int i = 0; i < key.length(); i++) {
// Store integer ASCII value of char
int asc = key[i];
int rem = asc - (26 - (key[i] - 'a'));
int m = rem % 26;
key[i] = (char)(key[i] + 15);
}
// Decryption Key cout for testing
std::cout << "Altered key: " << key;
return key;
}
getEncryptedText can be simplified to:
char getEncryptedText( char p, char k )
{
return ( ( p - 'a' ) + ( k - 'a' ) ) % 26 + 'a';
}
I've replaced magic numbers with the actual character values to make the code easier to read.
If we make sure that getDecryptedText is the exact reverse of getEncryptedText there is no need to modify the key.
char getDecryptedText( char p, char k )
{
return ( ( p - 'a' ) - ( k - 'a' ) + 26 ) % 26 + 'a';
}
The +26 is a fiddle to make sure the value is positive as the modulo won't produce the correct result for negative numbers.
Related
I am trying to find out the maximum number of words in a sentence (Separated by a dot) from a paragraph. and I am completely stuck into how to sort and output to stdout.
Eg:
Given a string S: {"Program to split strings. By using custom split function. In C++"};
The expected output should be : 5
#define max 8 // define the max string
string strings[max]; // define max string
string words[max];
int count = 0;
void split (string str, char seperator) // custom split() function
{
int currIndex = 0, i = 0;
int startIndex = 0, endIndex = 0;
while (i <= str.size())
{
if (str[i] == seperator || i == str.size())
{
endIndex = i;
string subStr = "";
subStr.append(str, startIndex, endIndex - startIndex);
strings[currIndex] = subStr;
currIndex += 1;
startIndex = endIndex + 1;
}
i++;
}
}
void countWords(string str) // Count The words
{
int count = 0, i;
for (i = 0; str[i] != '\0';i++)
{
if (str[i] == ' ')
count++;
}
cout << "\n- Number of words in the string are: " << count +1 <<" -";
}
//Sort the array in descending order by the number of words
void sortByWordNumber(int num[30])
{
/* CODE str::sort? std::*/
}
int main()
{
string str = "Program to split strings. By using custom split function. In C++";
char seperator = '.'; // dot
int numberOfWords;
split(str, seperator);
cout <<" The split string is: ";
for (int i = 0; i < max; i++)
{
cout << "\n initial array index: " << i << " " << strings[i];
countWords(strings[i]);
}
return 0;
}
Count + 1 in countWords() is giving the numbers correctly only on the first result then it adds the " " whitespace to the word count.
Please take into consideration answering with the easiest solution to understand first. (std::sort, making a new function, lambda)
Your code does not make a sense. For example the meaning of this declaration
string strings[max];
is unclear.
And to find the maximum number of words in sentences of a paragraph there is no need to sort the sentences themselves by the number of words.
If I have understood correctly what you need is something like the following.
#include <iostream>
#include <sstream>
#include <iterator>
int main()
{
std::string s;
std::cout << "Enter a paragraph of sentences: ";
std::getline( std::cin, s );
size_t max_words = 0;
std::istringstream is( s );
std::string sentence;
while ( std::getline( is, sentence, '.' ) )
{
std::istringstream iss( sentence );
auto n = std::distance( std::istream_iterator<std::string>( iss ),
std::istream_iterator<std::string>() );
if ( max_words < n ) max_words = n;
}
std::cout << "The maximum number of words in sentences is "
<< max_words << '\n';
return 0;
}
If to enter the paragraph
Here is a paragraph. It contains several sentences. For example, how to use string streams.
then the output will be
The maximum number of words in sentences is 7
If you are not yet familiar with string streams then you could use member functions find, find_first_of, find_first_not_of with objects of the type std::string to split a string into sentences and to count words in a sentence.
Your use case sounds like a reduction. Essentially you can have a state machine (parser) that goes through the string and updates some state (e.g. counters) when it encounters the word and sentence delimiters. Special care should be given for corner cases, e.g. when having continuous multiple white-spaces or >1 continous full stops (.). A reduction handling these cases is shown below:
int max_words_in(std::string const& str)
{
// p is the current and max word count.
auto parser = [in_space = false] (std::pair<int, int> p, char c) mutable {
switch (c) {
case '.': // Sentence ends.
if (!in_space && p.second <= p.first) p.second = p.first + 1;
p.first = 0;
in_space = true;
break;
case ' ': // Word ends.
if (!in_space) ++p.first;
in_space = true;
break;
default: // Other character encountered.
in_space = false;
}
return p; // Return the updated accumulation value.
};
return std::accumulate(
str.begin(), str.end(), std::make_pair(0, 0), parser).second;
}
Demo
The tricky part is deciding how to handle degenerate cases, e.g. what should the output be for "This is a , ,tricky .. .. string to count" where different types of delimiters alternate in arbitrary ways. Having a state machine implementation of the parsing logic allows you to easily adjust your solution (e.g. you can pass an "ignore list" to the parser and update the default case to not reset the in_space variable when c belongs to that list).
vector<string> split(string str, char seperator) // custom split() function
{
size_t i = 0;
size_t seperator_pos = 0;
vector<string> sentences;
int word_count = 0;
for (; i < str.size(); i++)
{
if (str[i] == seperator)
{
i++;
sentences.push_back(str.substr(seperator_pos, i - seperator_pos));
seperator_pos = i;
}
}
if (str[str.size() - 1] != seperator)
{
sentences.push_back(str.substr(seperator_pos + 1, str.size() - seperator_pos));
}
return sentences;
}
I have a trouble with this. I writing a code for the "vigenere cipher".
I have text and key. But i want to get alphabet from the user. what the user wants etc:"abcdfgh" or "sdgfjdgkfdsgs" just what the user wants.
So but i can't do it.
How do I get alphabet from the user?
Firstly, i want to do get alphabet from the user.
After, I want it to enter the word and encrypt it. But the words alphabet is user's alphabet.
Here is the codes:
#include <iostream>
#include <string>
using namespace std;
// Bu fonksiyon bir key oluşturur.
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size()) // eğer oluşturulan key boyutu girilen
metnin boyutuna eşitse fonksiyonu durdur.
break;
key.push_back(key[i]);
}
return key;
}
string cipherText(string str, string key) // "/Bu fonksiyon orjinal metni şifreler \"
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
int x = (str[i] + key[i]) % 26;
// alfabeyi ASCII kodlarina dönüştür:
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
// "/Bu fonksiyon şifreli metni orjinal hale getirir\"
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0; i < cipher_text.size(); i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] + 26) % 26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
int main()
{
cout << " Sifrelenmesini istediginiz kelimeyi/cumleyi giriniz" << endl;
string str;
getline(cin, str);
//string str = "METINBUGRA";
cout << "Anahtar kelimeyi giriniz." << endl;
string keyword;
getline(cin, keyword);
//string keyword = "ABC";
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
cout << "Sifrelenmis Kelime : "
<< cipher_text << "\n";
cout << "Cozumlenmis kelime : "
<< originalText(cipher_text, key);
system("pause");
return 0;
}
If I correctly understood your question, you want to use a custom alphabet instead of English alphabet. For instance you may add digits.
Instead of actual letters you must operate on numbers: 0, 1, 2, ... N-1, where N is the size of the alphabet. For English alphabet this means you must use 0 instead of A (0x41), 1 instead of B (0x42), ... 25 instead of Z.
If the size of the key is M, the encryption algorithm for letter at position i is:
( L[i] + K[i mod M] ) mod N
Once you have a functional algorithm that operates on numbers, all you have to do is map your input from letters to numbers and your output from numbers to letters.
Mapping numbers to letters is easy; you just have to store the alphabet into a string – this answers your question:
string n_to_letter; // alphabet
//...
int main()
{
//...
cin >> n_to_letter; // read the alphabet
//...
Mapping letters to numbers is probably beyond your current knowledge; you must use a map:
#include <map>
//...
string n_to_letter; // alphabet
map< char, int > letter_to_n;
void init_letter_to_n() //...
If you do not know how to use a map, there is a workaround: just search for the letter in the alphabet string, or use a 256 characters vector/string.
DEMO
This function is meant to remove all special characters, numbers, and whitespace from the char array.
// Michael E. Torres II
// Vigenere Cipher
// February 4, 2018
// C++ code to implement Vigenere Cipher
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <functional>
using namespace std;
// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
int x = (str[i] + key[i]) % 26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
// and returns the original text
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0; i < cipher_text.size(); i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] + 26) % 26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
transform(orig_text.begin(), orig_text.end(), orig_text.begin(), ::tolower);
}
return orig_text;
}
string removeNonAlpha(char *str)
{
unsigned long i = 0;
unsigned long j = 0;
char c;
while ((c = str[i++]) != '\0')
{
if (isalpha(c)) // this is where the breakpoint is automatically placed
{
str[j++] = c;
}
}
str[j] = '\0';
return str;
}
// Driver program to test the above function
int main(int argc, char *argv[])
{
string keyword = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
stringstream ss;
char a[] = "“I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right.” – Albert Einstein “Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.” – Albert Einstein";
int i = 0;
string str = removeNonAlpha(a);
str.append(512 - str.length(), 'X');
transform(str.begin(), str.end(), str.begin(), ::toupper);
transform(keyword.begin(), keyword.end(), keyword.begin(), ::toupper);
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
transform(cipher_text.begin(), cipher_text.end(), cipher_text.begin(), ::tolower);
transform(key.begin(), key.end(), key.begin(), ::tolower);
string orig = originalText(cipher_text, key);
cout << "Original/Decrypted Text : " << "\n";
for (int i = 0; i < orig.size(); i += 81)
orig.insert(i, "\n");
cout << orig;
cout << "\n\n" << "Ciphertext : " << "\n";
for (int i = 0; i < cipher_text.size(); i += 81)
cipher_text.insert(i, "\n");
cout << cipher_text;
cout << "\n\nPress ENTER key to Continue\n";
getchar();
return 0;
}
The char array works fine with this while loop, so long as there are no special characters [.,%$#!^]. As soon as there are any special characters in the char array, it gives me the debug assertion:
"Program: ...\Projects\ConsoleApplication17\Debug\ConsoleApplication17.exe
File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 42
Expression: c >= -1 && c <= 255
...
The program '[11048] ConsoleApplication17.exe' has exited with code 3 (0x3)."
If I run this on repl.it or cpp.sh, I get no issues though. I appreciate any help. Thank you.
It isn't done at all. It needs to be cleaned up a lot, but I'm just trying to test it as is.
see https://msdn.microsoft.com/en-us/library/xt82b8z8.aspx
isalpha expects a number between 0 and 0xFF:
The behavior of isalpha and _isalpha_l is undefined if c is not EOF or
in the range 0 through 0xFF, inclusive. When a debug CRT library is
used and c is not one of these values, the functions raise an
assertion.
You need to cast you char to an unsigned char before passing to isalpha.
I have implemented XOR encryption algorithm like so:
string XOR(string data, const char* key)
{
string xorstring = data; //initialize new variable for our xordata
for (int i = 0; i < xorstring.length(); i++) { //for loop for scrambling bits in the string
xorstring[i] = data[i] ^ key[i]; //scrambling the string/descrambling it
}
return xorstring;
}
Which works just fine, like: string ciphertext = XOR("test", "1234"); will return the ciphertext and when decrypting:string plaintext = XOR(ciphertext, "1234"); it will return 'test'.
So, I wanted to create an algorithm that would break the xor cipher by brute forcing, so basically trying to decrypt the ciphertext with every possible key combination.
It (should) work like this:
generate strings from an character array of alphabet
xor(decrypt) the given ciphertext with the given string to get plaintext
after that, xor(encrypt) the generated plaintext and compare it in if statement to see if it matches with the original ciphertext
if it matches, the correct key was found to decrypt the ciphertext.
Simple as that, but I found my self struggling with the algorithm:
const char Numbers[11] = "0123456789";
const char AlphabetUpper[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char AlphabetLower[27] = "abcdefghijklmnopqrstuvwxyz";
string XOR(string data, const char* key)
{
string xorstring = data; //initialize new variable for our xordata
for (int i = 0; i < xorstring.length(); i++) { //for loop for scrambling bits in the string
xorstring[i] = data[i] ^ key[i]; //scrambling the string/descrambling it
}
return xorstring;
}
string cipher, plain; //store the ORIGINAL ciphertext and plaintext
int main()
{
plain = "test"; //set the plaintext
cipher = XOR(plain, "1234"); //encrypt it with the xor function
cout << plain << endl; //output
cout << cipher << endl; //output
cout << "press enter to start bruteforcing!" << endl;
getchar();
while (true) //loop for bruteforcing
{
static int stringlength = 1; //the keylength starts from 1 and then
//expands to 2,3,4,5, etc...
BruteForce(stringlength, cipher, ""); //call the brute force function
stringlength++; //increment the keylength
}
return 0;
}
void BruteForce(int length, string ciphertext, string tempKey)
{
static int count = 0; // for counting how many times key was generated
string decipher, recipher; //for storing new XORed strings.
if (length == 0)
{
//decrypt the given ciphertext with the random key
decipher = XOR(ciphertext, tempKey.c_str());
//encrypt it again with the same key for comparison
recipher = XOR(decipher, tempKey.c_str());
cout << deciphered << endl; //output
//compare the two ciphertexts
if (ciphertext == recipher)
{
//....
cout << "Key found! It was: '" << tempKey << "'" << endl;
cout << "it took " << count << " iterations to find the key!";
getchar();
}
return;
}
count++;
//generate the keys.
for (int i = 0; i < 26; i++) {
std::string appended = tempKey + AlphabetLower[i];
BruteForce(length - 1, ciphertext, appended);
}
for (int i = 0; i < 26; i++) {
std::string appended = tempKey + AlphabetUpper[i];
BruteForce(length - 1, ciphertext, appended);
}
for (int i = 0; i < 10; i++) {
std::string appended = tempKey + Numbers[i];
BruteForce(length - 1, ciphertext, appended);
}
}
The algorithm does not work, for an unknown reason.
Very bizarre, in theory it should work. When the program is ran, it says the key was found on every execution of the bruteforce() function. Try it your self.
Could someone point out that what I am doing wrong here? Help is appreciated. Thanks.
It doesn't work because your task is impossible. The one time pad (xor) has a property known as perfect secrecy which means your cipher text can be decrypted to any plain text with equal probability. So it's impossible to know which plain text was the original.
For your scheme specifically (message XOR key) XOR key is always just the message back again.
This is the property of XOR operations: for every key you would find that
message XOR key XOR key == message
So your way to check that key is correct will indeed return all possible keys.
Moreover, say you have an encrypted message E. Then, for any possible plain text message M you will always find a key K so that
E = M xor K
(though such K can contain arbitrary chars, not just letters etc.) Therefore if you allow arbitrary chars in K, then the cypher is unbreakable even in theory (see one-time pad).
In case you allow only letters and digits in K, not every plain-text message can be received from a given encrypted message. However, you will need some more information on what the plain text could have been to detect "true" message from wrong ones (for example, knowing that plain text contains only letters, or that it is a valid English text, etc.), and you will need to check for this in your code. Anyway, I guess that you will get many possible plain texts even with this additional heuristics.
For an assignment, I am working on creating a word shifter in C++. I have little to no experience with C++ so it has been very difficult. I think I am really close but just missing some syntax that is part of C++. Any help would be appreciated greatly.
string s = phrase;
int length = s.length();
//find length of input to create a new string
string new_phrase[length];
//create a new string that will be filled by my for loop
for (int i=0; i<length; i++)
//for loop to go through and change the letter from the original to the new and then put into a string
{
int letter = int(s[i]);
int new_phrase[i] = letter + shift;
//this is where I am coming up with an error saying that new_phrase is not initialized
if (new_phrase[i] > 122)
//make sure that it goes back to a if shifting past z
{
new_phrase[i] = new_phrase[i] - 26;
}
}
cout << new_phrase<< endl;
Considering your syntax,I wrote an example for you.Besides,it is conventional
to write comment before it's relevant code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//test value;
int shift = 3;
string s = "hello string";
//find length of input to create a new string
int length = s.length();
//create a new string.it's length is same as 's' and initialized with ' ';
string new_phrase(length, ' ');
for (int i=0; i<length; i++)
{
//no need to cast explicitly.It will be done implicitly.
int letter = s[i];
//It's assignment, not declaration
new_phrase[i] = letter + shift;
//'z' is equal to 126.but it's more readable
if (new_phrase[i] > 'z')
{
new_phrase[i] = new_phrase[i] - ('z' - 'a' + 1);
}
}
cout << new_phrase<< endl;
}
This should work.
// must be unsigned char for overflow checking to work.
char Shifter(unsigned char letter)
{
letter = letter + shift;
if (letter > 'z')
letter = letter - 26;
return letter;
}
// :
// :
string new_phrase = phrase; // mainly just allocating a string the same size.
// Step throught each char in phrase, preform Shifter on the char, then
// store the result in new_phrase.
std::transform(phrase.begin(), phrase.end(), new_phrase.begin(), Shifter);
cout << new_phrase<< endl;
UPDATE: made letter unsigned, so the overflow check works.
Try and investigate this code
#include <iostream>
#include <string>
#include <cctype>
void ShiftRight( std::string &s, std::string::size_type n )
{
if ( n >= 'Z' - 'A' + 1 ) return;
for ( char &c : s )
{
bool lower_case = std::islower( c );
c = std::toupper( c );
c = ( c + n -'A' ) % ('Z' -'A' + 1 ) + 'A';
if ( lower_case ) c = std::tolower( c );
}
}
int main()
{
std::string s( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
std::cout << s << std::endl << std::endl;
for ( std::string::size_type i = 1; i <= 'Z' -'A' + 1; i++ )
{
std::str std::string s( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
ShiftRight( s, i );
std::cout << s << std::endl;
}
return 0;
}
The output is
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ
As for your code then it of course is wrong. You have not to define an array of strings. And do not use magic numbers as for example 122.
Also you may include in my code a check that a next symbol is an alpha symbol.