Caesar Cipher

Using simple Caesar cipher

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 59226 Views - Last Post: 08 September 2008 - 03:33 AM Rate Topic: -----

#1 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Caesar Cipher

Posted 31 March 2005 - 05:12 PM

Does anyone know how to create a simple caesar cipher (encryption) in C++? Im new to C++ and was wondering how you could do that in C++.

Thanx
Graig
Is This A Good Question/Topic? 1
  • +

Replies To: Caesar Cipher

#2 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4427
  • View blog
  • Posts: 28,356
  • Joined: 21-March 01

Re: Caesar Cipher

Posted 31 March 2005 - 05:23 PM

from what i understand of the caesar cipher the easiest implementation would be to take the ascii value of the character's in your array or whatever type of container you are using and then add whatever your shift key value is to each value and then print them out normally again.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Caesar Cipher

Posted 31 March 2005 - 05:56 PM

Sloth, as always, is right on the money. Once you have the string to encode and the shift value, you just need to traverse the string, and for each character get the ASCII value, add the shift (you should modulus the result by ('z' - 'a'), to keep yourself within the bounds of the alphabet), and reconvert.
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Caesar Cipher

Posted 01 April 2005 - 01:47 AM

yes...but originally the shift was 13.
So Any Phrase encrypted twice with the Caesar cipher would give the same phrase again...
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Caesar Cipher

Posted 01 April 2005 - 07:12 AM

I'm not sure exactly what you're talking about, born2code...both responses so far have indicated that the shift would be specified by the user, the post originator did not mention a shift of 13...as far as the modulus is concerned to keep it inside the alphabet, that's a standard cipher procedure.

If you're referring to the relative ease with which a straight Caesar cipher can be cracked, then I'd advise a simple extension of it, like the Beale cipher or the further extension of one time pad. A short explanation of both is available here.
Was This Post Helpful? 0
  • +
  • -

#6 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4427
  • View blog
  • Posts: 28,356
  • Joined: 21-March 01

Re: Caesar Cipher

Posted 01 April 2005 - 08:13 AM

born2c0de, on Apr 1 2005, 01:47 AM, said:

yes...but originally the shift was 13.
So Any Phrase encrypted twice with the Caesar cipher would give the same phrase again...

i'm pretty sure thats not the original shift but just the most common shift used because of the reasons you stated. most people who use caesar ciphers really aren't that concerned with security as much as just wanting to hide the text from those who happen to glimpse upon it.
Was This Post Helpful? 1

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Caesar Cipher

Posted 01 April 2005 - 10:23 AM

Amadeus, on Apr 1 2005, 07:42 PM, said:

I'm not sure exactly what you're talking about, born2code...both responses so far have indicated that the shift would be specified by the user, the post originator did not mention a shift of 13...as far as the modulus is concerned to keep it inside the alphabet, that's a standard cipher procedure.

If you're referring to the relative ease with which a straight Caesar cipher can be cracked, then I'd advise a simple extension of it, like the Beale cipher or the further extension of one time pad. A short explanation of both is available here.

When I meant "originally" i didnt refer to guy who made the original post.
I meant that one of the first Caesar Ciphers used used a Shift of 13.
You can of-course change the shift.
What supersloth said is true too.

Quote

Caesar ciphers really aren't that concerned with security as much as just wanting to hide the text from those who happen to glimpse upon it.


Simply scrambling Text.
Was This Post Helpful? 0
  • +
  • -

#8 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Caesar Cipher

Posted 01 April 2005 - 08:59 PM

[B][B]This is my Caesar Cipher code so far...

Could you guys suggest or point out where there are other options to make this code clean and short?





-----------------------------------------------------------------------------------
#include <stdio.h>

int main(void)
{
char messageClear[BUFSIZ];
char messageCrypt[BUFSIZ];
char buf[5];
int shift, i;

printf("This program encrypts a string using the Caesar Cipher \n\n");

printf("Enter alphabetic string: ", BUFSIZ);

fgets(messageClear, BUFSIZ, stdin);
fflush(stdin);

for (;;){
printf("\n\nEnter encryption key (1-4): ");
fgets(buf, 5, stdin);//the use of scanf is problematical, this is MUCH safer
fflush(stdin);
shift = buf[0] - 48;
if ((buf[1] != '\n') || (shift < 1 || shift > 4))
printf("Bad value for shift!\n");
else
break;
}
i=0;
while (messageClear[i]){
messageCrypt[i] = messageClear[i] + shift;
i++;
}
messageCrypt[i] = '\0';

printf("Your Message:\n%s", messageClear);
printf("Encrypted:\n%s", messageCrypt);
printf("\n\nType return to exit.\n");
getchar();

return 0;
}
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Caesar Cipher

Posted 02 April 2005 - 10:20 AM

You mentioned earlier that you were using c++, but you seem to have coded this in C...if you do wish to use c++ you can take advantage of the cin and cout objects to cut down on some of the buffer checking code you've got. Also, if you print the original message at the top of the code then you can get away without having to use two char buffers, and just modify the original (you can always return it to the original form by applying the corresponding shift).
Was This Post Helpful? 0
  • +
  • -

#10 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Caesar Cipher

Posted 03 April 2005 - 05:17 PM

Hey Amadeus

Im not sure wot u meant by:

"Also, if you print the original message at the top of the code then you can get away without having to use two char buffers"
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Caesar Cipher

Posted 03 April 2005 - 05:46 PM

Oh, it's nothing important...you mentioned that you might like some suggestions on cutting down on the code, I was just suggesting that two character arrays were not really required...if you want to print out the original message, you could print it out before you apply the shift, then apply the shift to the original character array (so that the array now holds the ciphered message), and print it out again. It cuts the memory that needs to be allocated to your program, and cuts the lines of code as well.
Was This Post Helpful? 0
  • +
  • -

#12 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Caesar Cipher

Posted 05 April 2005 - 03:58 AM

Choice Armadeus...Got ya...Thanx heaps....
Was This Post Helpful? 0
  • +
  • -

#13 ruthless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 05

Re: Caesar Cipher

Posted 12 April 2005 - 03:54 AM

uman_dude2
i am in the same boat as you, my first encounter with C, C++ and i am trying to write a code for ceaser cipher, can you assit me whilst your on the same code, thanks
Was This Post Helpful? 0
  • +
  • -

#14 uman_dude2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 31-March 05

Re: Caesar Cipher

Posted 27 April 2005 - 05:41 PM

Ruthless...

of course I can assist u....

wot do u wana know in particular?
Was This Post Helpful? 0
  • +
  • -

#15 ruthless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 05

Re: Caesar Cipher

Posted 28 April 2005 - 05:13 AM

uman_dude2
Thanks for coming back to me, ur email was a life saving tread for me, im under a week to hand in my work, i want to know the code to decrypt a particular file after it has been encripted, also i want the result to be in a different text file, can u please help me, its very URGENT, u can also email on my add for_psycho@hotmail.com
thanks buddy ur a star....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2