Jump to content

Before I Progress


Recommended Posts

Any errors so far?

 

#include <iostream>
#include <stdlib.h>

using namespace std;

int choice;

char[] stringToEncrypt;

char encrypt(char[] encryptionString); //Encryption function prototype

char decrypt(char[] encryptedString); //Decryptionm function prototype

int main()

{
    cout << "What would you like to do?\n 1: Encrypt a string\n2:Decrypt a string\n>"
    
    switch(choice) {
    case(1) :
        
    cout << "Enter the sentence/array you would like to be encrypted: ";
    cin >> stringToEncrypt;
    
    encrypt(stringToEncrypt);
    
    break
    
    case(2) :
    
    cout << "Enter the sentence you would like to be decrypted: ";
    
    decrypt(stringToDecrypt);
    
    break
    
    default : return 0;
    
   }


char encrypt(char[]encryptionString) {
   for(int i = 0;i<255;i++)
   
    {
        switch(encryptionString[i])
        case("A") : encryptionString[i] = "D"; break
        case("B") : encryptionString[i] = "C"; break
        case("C") : encryptionString[i] = "l"; break //That's a lowercase L
        case("D") : encryptionString[i] = "2"; break
        case("E") : encryptionString[i] = "1"; break //That's a one
        case("F") : encryptionString[i] = "m"; break
        case("G") : encryptionString[i] = "["; break
        case("H") : encryptionString[i] = "0"; break
        case("I") : encryptionString[i] = "t"; break
        case("J") : encryptionString[i] = "8"; break
        case("K") : encryptionString[i] = "c"; break
        case("L") : encryptionString[i] = ","; break

 

Should I use a strlen to replace the 255 in i<255?

Share this post


Link to post
Share on other sites

Should I use a strlen to replace the 255 in i<255?

484224[/snapback]

 

I would.

 

Also, here's an idea how you could encrypt your letters randomly but still stay fairly simple and easily decryptable (I have no idea if it'll actually work, but it'd be a project for you :P).

 

For a 'key' you could use srand() and rand(). rand(), from what I know, always generates the same sequence of numbers, based on the 'seed' .. which can be set by srand().

 

Soooo, you could use the current time as a seed, which could also act as a 'key' to decrypt your message (in which case make sure you print the key out along with the message!). Thennn... for each letter take the next number from rand() and subtract, or add, or whatever, from the current letter, like... message = message - rand();

 

To decrypt it all you should need to do is call srand() with your key (which should be the time printed out with your message), then go through each letter and do a reverse operation (if you subtracted to encrypt, then add to decrypt). So, should be something like message = message + rand();

 

If you don't try that, I am going to have to.. as I am now curious to see whether that would actually work :)

Share this post


Link to post
Share on other sites

Sure... once I can wake myself up enough to start coding again and understand what you're saying.

 

 

EDIT: Btw, looking in time.h, I noticed they didn't put anything after the time structure... so it's looking like:

 

/*
* A structure for storing all kinds of useful information about the
* current (or another) time.
*/
struct tm
{
int	tm_sec;  /* Seconds: 0-59 (K&R says 0-61?) */
int	tm_min;  /* Minutes: 0-59 */
int	tm_hour;	/* Hours since midnight: 0-23 */
int	tm_mday;	/* Day of the month: 1-31 */
int	tm_mon;  /* Months *since* january: 0-11 */
int	tm_year;	/* Years since 1900 */
int	tm_wday;	/* Days since Sunday (0-6) */
int	tm_yday;	/* Days since Jan. 1: 0-365 */
int	tm_isdst;	/* +1 Daylight Savings Time, 0 No DST,
    * -1 don't know */
};

 

Should I just use tm.tm_sec or something? Or, has this not got ANYTHING to do with the time?

Share this post


Link to post
Share on other sites

Sure... once I can wake myself up enough to start coding again and understand what you're saying.

484247[/snapback]

 

lol i'll be impressed if you can understand what I am trying to say. I think I may give it a go and see what happens anyway :)

 

EDIT: The seed could be any old number, I was just using a time stamp as an example :) You could ask the user for a key if you wanted. If you want to use a time thing, try

 

int key = time(NULL);

 

:)

Share this post


Link to post
Share on other sites

lol i'll be impressed if you can understand what I am trying to say. I think I may give it a go and see what happens anyway :)

 

EDIT: The seed could be any old number, I was just using a time stamp as an example :) You could ask the user for a key if you wanted. If you want to use a time thing, try

 

int key = time(NULL);

 

:)

484250[/snapback]

I understand what you're saying. That's a good idea. Too bad I lack the C/C++ programming skills to try it. I think that your idea should work fine, it's just a matter of coding it.

 

EDIT: And I'm pretty sure that you're right about the same number sequence with rand(). I know that's the case with VB .NET, and, if I remember correctly, it's the same for C++.

Share this post


Link to post
Share on other sites

I actually just coded it myself. It does work, but there is a small drawback. Windows doesn't appear to implement rand() the same as BSD (which at least follows a standard!!). So, if you're going to try and code my idea, you may want to use your own rand() function, just so it's a little more cross platform :P

 

Here's the rand() implementation from FreeBSD

 

STANDARDS

    The rand() and srand() functions conform to ISO/IEC 9899:1990

    (``ISO C90'').

 

/*-
* Copyright (c) 1990, 1993
*      The Regents of the University of California.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*      This product includes software developed by the University of
*      California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Posix rand_r function added May 1999 by Wes Peters <[email protected]>.
*
* $FreeBSD: src/lib/libc/stdlib/rand.c,v 1.2.2.1 2001/03/05 11:33:57 obrien Exp
$
*/

unsigned long int next = 1;

void srand ( unsigned int seed )
{
 next = seed;
}

int rand()
{
   return ((next = next * 1103515245 + 12345) % ((unsigned long)2147483647 + 1));
}

 

Other than that, seems to work pretty well! If you want to try and code it for yourself i'll gladly give hints or whatever. Don't wanna ruin your fun too much :rolleyes:

Share this post


Link to post
Share on other sites

heres another idea

 

use the time to start a random encrytion and then make it so it can only be decrypted if you know what time it was encrypted

 

like when its done encrypting the message it will desplay the encrypted message in one box then the time stamp in another box

Share this post


Link to post
Share on other sites

heres another idea

 

use the time to start a random encrytion and then make it so it can only be decrypted if you know what time it was encrypted

484274[/snapback]

 

That was pretty much my idea, explained 100x better.

 

Example run showing what happens when the encrypted message is decoded with the right key, and with a wrong key:

 

Encypting message
---
Key ( time(NULL) ) for this message is 1117385596

|cWsb%qxadQ5b8MKVgbef]`ie['#&9mStHm=2yyF}6zY,FvwS5clZp


Decrypting message with key 1117385596
---
Hello World, this is my very simple encryption thingy!


Decrypting message with key 1117385597
---
h|tT(cFa5@]qjJe?\{H',n*yK/%U,#0|#mwWw.ZfRLe_jlHXY[cSnX

Share this post


Link to post
Share on other sites

Now i've finished being caught up in that.

 

The only things I can see wrong from looking at your original code posty..

 

case("A") : encryptionString[i] = "D"; break

 

The " should be ' in all of those lines

 

Function prototypes don't need the variable name, just the type.

 

char encrypt(char[] encryptionString); //Encryption function prototype

char decrypt(char[] encryptedString); //Decryptionm function prototype

 

should beeeeeeeeee....

 

char encrypt(char[]); //Encryption function prototype

char decrypt(char[]); //Decryptionm function prototype

 

EDIT: Oh, and you're missing ;'s after the break's

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...