|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;
}
|
|
#2
|
|||
|
|||
|
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;
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > C++ Encryption, Loop on each character |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|