C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 5th, 2006, 02:37 PM
mozzer mozzer is offline
PHP 4EVER
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 84 mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 11 m 10 sec
Reputation Power: 7
C++ Encryption, Loop on each character

Hi, I think this is my first post in the C++ forum so I'll say Hi to everyone who doesn't know me from my brief time in the PHP forum.

Well I'd best start at the beginning. I am trying to create a program which "brute-forces" ceaser-shift encryptions. I would like this to be done by taking each character in the string and adding a set value to it and then returning it. I have tried several methods, but the code I have is very inefficient and I was hoping someone could help me to rewrite it from scratch.

Thanks for taking the time to read this, even if you don't respond I still appriciate it.

Other problems with my code. It adds 26 on regardless on whether that puts it passed the end of the alphabet.

mozzer

Code:
/************************************************************

Program:      CeaserShift

File:         ceaser.cpp

Function:     Main (complete program listing in this file)
              Change (Adds rot onto the chars)

Description:  CeaserShift Bruter

Author:       Simon Morris

Environment:  Dev C++ version 4.9.9.2, 486/66 1024mb RAM, WindowsXP

Notes:        None as yet

Revisions:    1.00  05/10/2006 (jl) First release

************************************************************/

// Initial Includes
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

string change(string input, int rot);

int main() {
    string text;
    cout << "Enter some text: ";
    std::getline(std::cin, text);
    string newtext;
    int rot;
    rot = 0;
    for(rot=0;rot<=26; rot++) {
       newtext = change(text, rot);
       cout << "Rot: " << rot << newtext << endl << endl;
    }
    system("PAUSE");
    return 0;
}



string change(string input, int rot) {
       int chr = 0;
       string current;
       string output;
       while (chr < input.length()) {
             if (input[chr] >= 'a' && input[chr] <= 'z') {
                current = input[chr] - 'a' + 'A' + rot;
                } else {
                current = input[chr];
             }
             output = output + current;
             chr ++;      
       }
       return output;      
}

Reply With Quote
  #2  
Old October 6th, 2006, 03:22 AM
mozzer mozzer is offline
PHP 4EVER
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 84 mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level)mozzer User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 3 h 11 m 10 sec
Reputation Power: 7
Hi, sorry for the double post but I have a different question on the same piece of code.

When I run this \/ loop I have an error that it does not wait for the user to enter any data before continuing. Any ideas on how to stop this

Code:
/************************************************************

Program:      CaesarEncrypt

File:         caesar.cpp

Function:     Main (complete program listing in this file)
              Change (Adds rot onto the chars)
              Brute (Runs throught all 26 rots)

Description:  CaesarShift Bruter

Author:       Simon Morris

Environment:  Dev C++ version 4.9.9.2, 486/66 1024mb RAM, WindowsXP
			  Notpad2.exe 1.0.12, 224mb RAM, WindowsXP

Notes:        None as yet

Revisions:    1.00  05/10/2006 (jl) First release
              1.01  05/10/2006 (jl) Improvement on alphabet error
              1.10  06/10/2006 (jl) Encryptor
              1.11  06/10/2006 (jl) Spelling Error

************************************************************/

// Initial Includes
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

string change(string input, int rot);
int brute (string inputLoop);
string caesarEncrypt;
int main() {
    string text;
    string eText;
    int uRot;
    int cinReturn;
    int x = 1;
    while (x > 0) {
	// Message Loop
	   cout << "*** Main Menu ***\nPress Corresponding number and press enter\n\n1. Brute Force Caesar Cipher\n2. Encrypt Caeser Cipher\n3. Exit\n";
	   cin >> cinReturn;
	   switch (cinReturn) {
			case (1):
				cout << "Enter you encrypted text: ";
				std::getline(std::cin, text);
				brute(text);
				break;
			case (2):
				cout << "Enter text to be encrypted";
				std::getline(std::cin, eText);
				cout << "Enter rot value: ";
				cin >> uRot;
				cout << "Your encrypted text is: " << change (eText, uRot) << endl;
				break;
			case (3):
				cout << "Exitting...";
				x++; // End Loop
				break;
			default:
				cout << "Unrecognised input";
		}

    }
    system("PAUSE");
    return 0;
}

int brute (string input) {
       string newtext;   
       int rot;
       for(rot=0;rot<=26; rot++) {
          newtext = change(input, rot);
          cout << "Rot " << rot << ": " << newtext << endl;
       }
       return 0;      
}

string change(string inputLoop, int rot) {
       int chr = 0;
       string current;
       string output;
       while (chr < inputLoop.length()) {
             if (inputLoop[chr] >= 'a' && inputLoop[chr] <= 'z') {
                current = inputLoop[chr] - 'a' + 'A' + rot;
                if (current[0] > 'Z') {
                   current[0] = current[0] - 'Z' + 'A';
                }
                } else {
                current = inputLoop[chr];
             }
             output = output + current;
             chr ++;      
       }
       return output;      
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++ Encryption, Loop on each character


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT