Jump to content
  • Advertisement
Sign in to follow this  
ThinkingsHard

Beale Cipher

This topic is 1591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble getting the Beale Cipher to work. For anyone unfamiliar, it's basically the ceasar cipher, only a bit cooler.

 

 

 

Take the decrypted message from the first cryptogram, and  have it be the key to solving the next cipher. How this cipher works

0123456789 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 (These values can be anything that you really want, I set a to zero instead of

abcde fgh i j    k   l    m  n   o  p    q   r   s    t   u    v   w  x   y  z    1 just for the hell of it)

Key:                “STRINGOFWORDSSTRINGOFWORDSSTRINGOFWORDS...”

Message         “THEPROBLEMISNOTTHEPROBLEMTHEPROBLEMISYOURATTITUDEABOUTTHEPROBLEM,DOYOUUNDERSTAND”

Encryption:  S = 18. S applies to the T of the first letter of the message. Shift T 18 letters

T becomes L

“LAVXFU…”

 

I have two problems. Right now, it's not properly encrypting. And the second problem, is, as you can see, the key isn't always going to be the same length of the message, but I'm failing to think of a loop that will take the key message, and continue to add it onto itsself until it reaches the length of the message you want to encrypt.

	public void encrypt2(String str, String key)
	{
		/*for(int z = 0;  z < str.length(); z++)
		{
			char kCh = key.charAt(z);
			key += kCh;
		}*/	

		for(int i= 0; i < str.length(); i++)
		{	
			// the Character in the string
			char baelleCh = str.charAt(i);

			for(int k=0; k < key.length(); k++)
			{
				// The character in the key
				char ch = key.charAt(i);
				// if the character in the key is a space
				if(ch == ' ')
				{
					// crypted  = the character
					char crypted = ch;
					cryptedString2 += crypted; // the character is added onto the string
				}
				
				else if(ch == 'a')
				{			
					shift = 0; // shift is nothing
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a'); // crypted is set to the character in the string minus value of a, plus the shift
					cryptedString2 += crypted;								   // mod 26 for the alphabet, plus the value of a
				}
				else if(ch == 'b')
				{			
					shift = 1;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'c')
				{			
					shift = 2;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'd')
				{			
					shift = 3;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'e')
				{			
					shift = 4;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'f')
				{			
					shift = 5;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'g')
				{			
					shift = 6;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'h')
				{			
					shift = 7;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'i')
				{			
					shift = 8;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'j')
				{			
					shift = 9;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'k')
				{			
					shift = 10;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'l')
				{			
					shift = 11;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'm')
				{			
					shift = 12;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'n')
				{			
					shift = 13;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'o')
				{			
					shift = 14;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'p')
				{			
					shift = 15;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'q')
				{			
					shift = 16;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'r')
				{			
					shift = 17;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 's')
				{			
					shift = 18;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 't')
				{			
					shift = 19;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'u')
				{			
					shift = 20;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'v')
				{			
					shift = 21;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'w')
				{			
					shift = 22;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'x')
				{			
					shift = 23;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'y')
				{			
					shift = 24;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else if(ch == 'z')
				{			
					shift = 25;
					char crypted = (char) (((baelleCh -'a') + shift)%26 +'a');
					cryptedString2 += crypted;
				}
				else
				{
					System.out.println("Bug; did not reach a letter");
				}
			}

		}
	}

I will be available to answer any questions in regards to this through this post or private message. I'm hoping to have this completed ASAP as it's what I'm working on for my final project this semester.

Share this post


Link to post
Share on other sites
Advertisement
You should never have repetitive chunks of code like that. This is how I would do it in C++ (although it should be easy to port to Java, since I am not using anything special about C++):
#include <iostream>
#include <string>

std::string key = "STRINGOFWORDS";
std::string message = "THEPROBLEMISNOTTHEPROBLEMTHEPROBLEMISYOURATTITUDEABOUTTHEPROBLEMDOYOUUNDERSTAND";

int main() {
  for (int i = 0, end = message.length(); i != end; ++i) {
    char encoding = 'A' + ((message[i] - 'A' + key[i % key.length()] - 'A') % 26);
    std::cout << encoding;
  }
  std::cout << '\n';
}

Share this post


Link to post
Share on other sites

Attempting to convert that over to Java.

 

It keeps getting annoyed saying key needs to be an int.

for (int i = 0; i <  str.length();  i++) 
   {
	char baelleCh = str.charAt(i);
	char ch = key.charAt(i);
			                            
        char encoding = (char)('A' + baelleCh - 'A' + key(i % key.length()) - 'A') % 26);
        cryptedString2 += encoding;
   }

Share this post


Link to post
Share on other sites

Attempting to convert that over to Java.

 

It keeps getting annoyed saying key needs to be an int.

for (int i = 0; i <  str.length();  i++) 
   {
	char baelleCh = str.charAt(i);
	char ch = key.charAt(i);
			                            
        char encoding = (char)('A' + baelleCh - 'A' + key(i % key.length()) - 'A') % 26);
        cryptedString2 += encoding;
   }

 

"key(i % key.length())" doesn't make sense as far as the java compiler is concerned.

 

Try to take a look at that line in Alvaro's version and see what's going on.

 

First, you get the appropriate index into the key.  That's "key[i % key.length()].  The % is used to loop if the message is longer than the key.

 

Then, subtract 'A' (or 'a' depending on your case) from it.  That gets you the offset from 0.  Do this to the message character as well.

 

Then, sum them and mod 26 to get the appropriate encrypted character.

 

Finally, add your appropriate base ('A' or 'a') back in, and voila, you have your string.

 

In fact, whether you're using C++ or Java, that line should be exactly the same, parentheses and all, except for maybe a cast at the very end.

Edited by SeraphLance

Share this post


Link to post
Share on other sites
	public void encrypt2(String str, String key)
	{
		
		for (int i = 0; i <  str.length();  i++) 
			{
			
			char baelleCh = str.charAt(i);
			char ch = key.charAt(i % key.length());
			
			if(baelleCh == ' ')
			{
				cryptedString2 += baelleCh;	
			}
			
		    char encoding = (char) (((baelleCh - 'a') + (ch - 'a')) % 26 + 'a');
		    cryptedString2 += encoding;
			}

It works now, the only problem is, it adds characters like \ and _. Is there a way to make it not have these when it's encrypting the string? I can't really think of anything that would remove them without messing with the encryption. editting this to add that If I change it to

 

 char encoding = (char) (((baelleCh - 'a') + (Character.getNumericValue(ch)%26 + 'a')) % 26 + 'a');

 

I get a neater string with no odd symbols, however I then run into a problem of, I'm having it not align the words properly. There are spaces, and it's spaced out and looks nice, however there is a 3 letter encrypted string where a 2 letter string should be, same for a 4 and a few 6's. It's quite strange. I'm not sure why.

Edited by ThinkingsHard

Share this post


Link to post
Share on other sites

I'm not sure why you're getting those symbols in the cyphertext.  Is it coming from capital letters?  Your cypher assumes everything is lowercase (as Alvaro's assumes everything is uppercase).  If you want to handle both cases you need some extra logic.

 

As for your new problem, can you give an example?

Share this post


Link to post
Share on other sites

I fixed it by using this code

	public  void encrypt2(String str, String key)
	{

		for (int i = 0; i <  str.length();  i++) 
		{
			char baelleCh = str.charAt(i);
			char ch = key.charAt(i % key.length());
			// allows spaces
			if(baelleCh == ' ')
			{
				cryptedString2 += baelleCh;    
			}
			else
			{		
				// like the Ceasar Cipher, takes the char in the string, minus 'a' plus the numerical value of the character of the key, mod 26, plus 'a'
				char encoding = (char) (((baelleCh - 'a') + (ch-'a')) % 26 + 'a');
				cryptedString2 += encoding;
			}
		}
	}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!