• Post Reply Bookmark Topic Watch Topic
  • New Topic

Caesar Box Encryption Code  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on a project where I have to encrypt a string using the Caesar box encryption and also be able to decrypt a string.

Caesar Box Encryption:

Take a string "I love lamp"
Remove spaces "Ilovelamp"
Remove Capitals "ilovelamp"
Put in Square formation, must be perfect square, so if there were 10 letters, then the square would have to be the next highest perfect square, 16, and the last few spots would be blank.

i l o
v e l
a m p

Take letters down each column and print "ivalemolp"

Decrypt the same way:
i v a
l e m
o l p
"ilovelamp"

The decrypted string does not need any captial letters or spacing. (It's not that advanced)...

Anyways, I have pretty much finished it, and the encryption part works perfectly, but the decryption seems to print everything backwards or mix it up somewhat, I'm not sure what I have wrong here, because as far as I can tell, the encryption and decryption code should be the same, with different final variables.

 
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that your encryption does not undo itself, because when you elide the blanks, you change the locations of the encrypted characters, which maps them into the wrong locations when you try to decrypt.

Consider this input string: abcde

You get this 3x3 matrix in your code:

a b c
d e _
_ _ _

Reading down the columns, you get this (with the blanks in place):

ad_be_c__

If you ran the encryption again, your matrix would be this:

a d _
b e _
c _ _

which, reading down the columns, does what you want:

abcde____

But, by eliding the blanks from your encrypted string, the matrix you build on the second (decrypting) pass puts some of the encrypted letters into the wrong place:

a d b
e c _
_ _ _

Which doesn't decode the string:

adbec____

One solution would be to populate the decryption matrix with markers, to show where the blanks will go in the decryption pass. Start by filling the whole matrix with blanks:

_ _ _
_ _ _
_ _ _

Then mark the locations in the matrix that will receive cyphertext characters by working your way down the rows first, by columns second, for as many matrix locations as there are characters in the cyphertext:

x x _
x x _
x _ _

In the order you mark the cells, it would be this:

1 4 _
2 5 _
3 _ _

Then, work your way across the columns first, rows second, filling in the marked cells with characters from the cyphertext, skipping the unmarked cells:

a d _
b e _
c _ _

In the order you fill them in, it would be this:

1 2 _
3 4 _
5 _ _

Reading the matrix row first, column second, you back your cleartext:

abcde____

I'm sure there's a more direct way to handle it, but the problem basically is that, by padding with blanks to reach a square encryption matrix, then dropping those blanks from the cyphertext, your decryption matrix is no longer a transpose of the encryption matrix. You need to put the cyphertext blanks back into the cyphertext before buildling your matrix. What I've shown above is one way to do that.
 
Casey J. Buresh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! You're right! I just realized what you mean. It messed up only in cases where there were blank spaces.... That makes a lot of sense now. I don't know why I didn't see if before....

I will have to add some sort of placeholder instead of just adding blank spaces. Perhaps a random character that's not going to by typed in or even a variable itself?

I'm thinking if I just put a variable itself in, then that might fix it...

The only problem I think I'm having now, is that the keys to decrypt are completely separate from the keys to encrypt. They are run separately, from what I can tell by the professors instructions. And if that's the case, then there might not be a way to truly fix it... I'll have to see....
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it will mess up even when the cleartext has no blanks. The problem appears when the cyphertext acquires blanks because the encryption matrix is bigger than the number of characters (that is, when the length of the cleartext is not a perfect square). Try it with "abcde" as your input string and you'll see what I mean.
 
Casey J. Buresh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I totally see now. Thanks for the help.

I'm going to have to talk to the professor, because his assignment is flawed because of that fact. Here are the results from my submission, and from what I can tell, it appears the encrypt entries and decrypt entries are completely different each time.

PASS    complex4CharEncrypt    0.006    
-
FAILURE complex7CharDecrypt 0.002    
junit.framework.AssertionFailedError - expected:<ab[c~d9!]> but was:<ab[9~d!c]>
PASS    medium4CharEncrypt    0.001    
-
FAILURE medium6CharDecrypt 0.001    
junit.framework.AssertionFailedError - expected:<a[bcd9]!> but was:<a[9dcb]!>
PASS simple4CharDecrypt 0.001    
-
PASS simple4CharEncrypt 0.0    
-
FAILURE    simple5CharDecrypt    0.008    
junit.framework.AssertionFailedError - expected:<a[bcde]> but was:<a[edcb]>
PASS    simple5CharEncrypt    0.0    
-
FAILURE    sonnetTestDecrypt    0.008  
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!