473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Null cipher question

25
Hi everyone,

I'm trying to figure out how to encrypt something with a null cipher such as if someone typed "hi", it would put 1 random character in front of 'h' and inbetween 'h' and 'i' and after 'i'.

so I could do something like this:

Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char *argv[], char **env)
  2. {
  3.     int c , count = 1;//atoi(argv[1]) ;
  4.     srand(time(NULL)) ;
  5.     c = rand() % 94 ;
  6.     c += 32 ;
  7.     char b;
  8.  
  9.     cout.put( c = (32 + rand() % 94) ) ;
  10.     b = cin.get();
  11.     cout.put(b);
  12.     cout.put( c = (32 + rand() % 94) ) ;
  13.     b = cin.get();
  14.     cout.put(b);
  15.     cout.put( c = (32 + rand() % 94) ) ;
  16.  
  17. }
but I want the user to be able to specify how many random characters will go before, in between and after each 'real' character. I always want the person to be able to type whatever they want and it'll be encrypted by whatever specified number of random characters.

I thought I could do something like this (see below), but I know my condition for the loop is wrong and I have no idea what condition I should put in the do while loop.

Expand|Select|Wrap|Line Numbers
  1. int main (int argc, char *argv[], char **env)
  2. {
  3.     int c , count = atoi(argv[1]) ;
  4.     srand(time(NULL)) ;
  5.     c = rand() % 94 ;
  6.     c += 32 ;
  7.     char b;
  8.  
  9.     do
  10.     {
  11.         for (int i = 1; i <= count; i++)
  12.             cout.put( c = (32 + rand() % 94) ) ;
  13.  
  14.         b = cin.get();
  15.         cout.put(b);
  16.     } while (!cin.eof());
  17. }
If someone can help me with this that would be greatly appreciated. Thanks.
May 14 '09 #1
6 3018
milk242
25
okay I figured it out by using !cin.eof() for the while condition.

I'm wondering how I can end the program after the user enters in their sentence in terminal. Right now, when I press enter after a sentence, it doesn't end the program and just waits for me to type more.

should I put some sentinel condition in the while loop to end program?
May 14 '09 #2
manontheedge
175 100+
if you want it to end when the user presses enter, you need to check for a carriage return. For example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if( enteredCharacter == '/r' )  // if the enter button is pressed on the keyboard
  3. {
  4.     // end while loop
  5. }
  6.  
  7.  
... something like that

or, for your while loop, you can loop WHILE != '/r'
May 15 '09 #3
RRick
463 Expert 256MB
I agree with Manoth. You're going to have to check for <cr> in the cin.get values. Your cin.eof check is doing nothing and you'll never exit the loop.

Usually, a blank line (or two) is a good indicator that the user is finished. In this case, you count the number of consecutive <cr> until you're sure the user wants to quit.
May 15 '09 #4
milk242
25
Thanks everyone, I'm pretty lost on how to check for two carriage returns in a row.
May 15 '09 #5
RRick
463 Expert 256MB
Keep a counter for the <cr>. When you find a <cr> increment the counter and for every other character found, set the counter to zero.

When the <cr> counter == 2, you have one blank line, when the <cr> counter == 3, you have two blanks lines, etc.
May 16 '09 #6
JosAH
11,448 Expert 8TB
Why not simply type ^D (on a Unix machine) or ^Z (on a Windows machine) to indicate the end of the input stream?

kind regards,

Jos
May 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
7
by: Piotr Turkowski | last post by:
Hi! Here you can get some notes about Vigenere Cipher: http://raphael.math.uic.edu/~jeremy/crypt/vignere.html Here's whole code of my program, function stats() is in polish, so you can omit...
9
by: Piotr Turkowski | last post by:
Hi, The code is here: http://free.ud.pl/~piotr/data/vigenere.zip Its my program for decrypting and encrypting text. Shot polish course: szyfrowanie (szyfruj) - encrypting (text i want to code...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
1
by: beetle17 | last post by:
Plaintext: a  n i c e  d a y Key: -3 Ciphertext: X  k f Z b  a X v Cipher will accept commands from the user and perform the operations required by the commands. There are three different...
4
by: wagn31 | last post by:
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:
2
by: yeruvavasavi | last post by:
Algorithm 1: Write python programs to translate the following algorithm so that the computer can execute it. Algorithm for ceaser cipher. Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ<br> Cipher: ...
0
by: ntuyen01 | last post by:
Hi all, I want to use the SSLStream with the cipher (TLS_RSA_WITH_AES_128_CBC_SHA) to get the handshake with my server, but I not sure where I can start. I do it in C# 2.0 Here is my code: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.