Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,391 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,140 people online right now. Registration is fast and FREE... Join Now!




Basic Caesar Cipher for C++

2 Pages V  1 2 >  
Reply to this topicStart new topic

Basic Caesar Cipher for C++, I need help on caesar cipher

jeisma
14 Sep, 2006 - 09:52 PM
Post #1

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
I'm working on a caesar cipher and I'm not exactly sure on how to go about it. I'm confused on how using the char type only stores one letter at a time and how to evaluate each letter, conver it into an interger?, then encrypt it, and display the new encrypted message.

This is what I have..

CODE

#include <iostream>
using namespace std;

int main () {
  char ch;
  int k;
  int pass;

  cout << "What password do you want to encrypt?";
  cin >> ch;
  cout << "What integer would you like to use to shift?";
  cin >> k;

  while (ch != 27) {
    pass = ch + k;
    cin >> pass;  
  }
}


Obviously, this is terribly wrong. It doesn't even work after it displays "What interger would you like to use to shift?" It just ends the program. How do I do this exactly?
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Basic Caesar Cipher For C++
15 Sep, 2006 - 01:22 PM
Post #2

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
Oh this is completly wrong, the idea is there but it's not that simple, do not check my contributions if you're doing this as a solo project and learning C++(look it up when you're finished)

What you need is to have a copy of the ASCII table first of all :
ASCII table I use

The basic idea is to add the offset onto the character. The problem is that using the declaration char is going to be a problem since it holds a definite (if not one) letter. Your code converts the first letter.
*hint : use string.h*

Now for the encryption part :
My method was to use a different variable declared as int hence crypted
CODE
crypted = ch + offset;
cout<<char(crypted);

so I could use the original string in case I needed too or manipulate spaces (will be needed if the string includes it)

Now for the loop :
Use a for loop instead (will make the job easier) and use the
ch.length();. This value is the number of letters in the string (here ch). Make a separate variable and initialize to 0 then increment until the variable has reached ch.length();
CODE
for(a = 0; a != ch.length(); ++a)
//Encryption

Note : Once inside the loop, make sure you use ch[a]; and not ch; alone.

This way, the loop will process the whole string.

What if : x + 4 = | ?
You need to limit the alphabet inside the ASCII table :
The values you need are :
a = 97
z = 122
This part is more mathematical :
CODE
if(ch[a] + offset > 122)
crypted = ((ch[a] + offset) - 122) + 9;


The basic idea of this is to get the overvalue, substract 'z' then add the result to 'a'.

I hope I haven't talked too much and perhaps gave you a headache but the answer is there ^^
I could post the code right now but I'd rather like you understand what I said. smile.gif
User is offlineProfile CardPM
+Quote Post

jeisma
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 03:27 PM
Post #3

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
I'd rather not do a string. I'd like to use the char type and be able to encrypt each individual letter of the input. Thank you though for the alternative! I still can't comprehend how to encrypt each letter into numeric value and back again by using a while loop. (I want to use a while loop..) It's just what I'm trying to figure out. Please help! Thanks.

CODE

#include <iostream>
using namespace std;

int main()
{

  char ch;
  int offset;
  int crypted;

    cout << "What do you want your password to be?\n";
    cin >> ch;
    cout << "What do you want your offset number to be?\n";
    cin >> offset;

    while (ch != ch.length()) {
      crypted = ch + offset;
      cout << char(crypted);
    }
}


It won't recognize the ch.length() function.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 03:39 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
It won't recognize the ch.length() function because it does not exist...the .length() method is a method of the string class, which you have elected not to use.

Your ch variable has been declared as a single character...do you want it to be an array of characters?

To get the length of an array of characters, use the strlen() function.

If you do switch to the string class, you can still access each individual element for encryption.
User is offlineProfile CardPM
+Quote Post

jeisma
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 04:11 PM
Post #5

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
How do I go about implementing that? Keep in mind, I honestly don't have any clue on how to get this code together. I'm looking for some basic code to convert and encrypt to get me started if that could be possible. Otherwise, more help in the right direction is always appreciative too. Thanks guys.

Append:

So, I've figured out how to encrypt a single letter in the Caesar Cipher, but I want to be able to do a whole word rather than a single letter.

CODE

#include <iostream>
using namespace std;

int main()
{

  char ch;
  int i = 65;
  int offset;
  int crypted;
  
    cout << "What do you want your password to be?\n";
    cin >> ch;
    cout << "What do you want your offset number to be?\n";
    cin >> offset;
  
    crypted = int(ch) + offset;
    
    cout << char(crypted);
}

Anyway, this works as long as you enter one letter. What type of loop would I need to use to evaluate an entire word, still using the char type for my variable that stores my password.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 04:51 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
As mentioned, you can use a string object (or array of characters), and loop through it, encrypting as you go...below is a very quick and dirty example that demonstrates the functionality:
CODE

#include <iostream>
#include <string>
using namespace std;

int main()
{

  string str1;
  int i = 0;
  int offset;
  int crypted;
  
    cout << "What do you want your password to be?\n";
    cin >> str1;
    cout << "What do you want your offset number to be?\n";
    cin >> offset;
    while(i<str1.length()){
       str1[i] = int(str1[i]) + offset;
       i++;
    }
    
    cout << str1<<endl;
    return 0;
}

Please note this code is for demonstration purposes only...there is no error checking of any sort, nor does it take into account that you will want to ensure that you do not pass the final alphabetical character without beginning again.

To answer your other question, you can use almost any type of loop.

If you are absolutely intent on using the char type, then you need to realize that you require an array of characters...not a single char. See below...same caveats on the code - example of functionality only:
CODE

#include <iostream>
#include <string.h>
using namespace std;

int main()
{

  char str1[80];
  int i = 0;
  int offset;
  
    cout << "What do you want your password to be?\n";
    cin >> str1;
    cout << "What do you want your offset number to be?\n";
    cin >> offset;
    while(i<strlen(str1)){
       str1[i] = int(str1[i]) + offset;
       i++;
    }
    
    cout << str1<<endl;
    return 0;
}

User is offlineProfile CardPM
+Quote Post

jeisma
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 04:59 PM
Post #7

New D.I.C Head
*

Joined: 14 Sep, 2006
Posts: 17


My Contributions
That code helped immensely. I still want to know how I can loop through a char variable and do the same thing with that. Is it possible?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Basic Caesar Cipher For C++
17 Sep, 2006 - 05:00 PM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I'm not sure I explained myself properly...a char variable is a single, meaning one, character...it holds ony one character. If you want to use several, you will require an array of characters, as per my second example...see above, and sorry for the confusion.
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Basic Caesar Cipher For C++
18 Sep, 2006 - 02:51 AM
Post #9

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
In other words
CODE
char ch; //This is only ONE character therefore it will input the first letter of a string.

char ch[80]; //This is an array of characters where [80] defines the number of arrays.


You can preferably use the for loop to initialize the "i" to 0, post the condition and increment althogether.

Amadeus's example uses the strlen() to record the number of characters inputted in the char array to set a condition (limit if you prefer)
(This prevents going through all the 80 spaces even 20 characters were inputted).
Since "i" starts out as 0, it will add the offset to all the characters in the array of char.
User is offlineProfile CardPM
+Quote Post

henok
RE: Basic Caesar Cipher For C++
27 Sep, 2006 - 08:39 AM
Post #10

New D.I.C Head
*

Joined: 27 Sep, 2006
Posts: 7


My Contributions
Now, what if you wanted to do this for two words? That is using string.
User is offlineProfile CardPM
+Quote Post

Dark_Nexus
RE: Basic Caesar Cipher For C++
27 Sep, 2006 - 09:04 AM
Post #11

or something bad...real bad.
Group Icon

Joined: 2 May, 2004
Posts: 1,309



Thanked: 3 times
Dream Kudos: 625
My Contributions
just keep running through the character array letter by letter encrypting each letter you come upon, and of course skipping spaces.
User is offlineProfile CardPM
+Quote Post

henok
RE: Basic Caesar Cipher For C++
27 Sep, 2006 - 09:30 AM
Post #12

New D.I.C Head
*

Joined: 27 Sep, 2006
Posts: 7


My Contributions
QUOTE(Dark_Nexus @ 27 Sep, 2006 - 10:04 AM) *

just keep running through the character array letter by letter encrypting each letter you come upon, and of course skipping spaces.


how do i skip the spaces?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/5/08 02:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month