Hello im trying to make a encrypter so far and im somewhat of a beginner i like to go for challenges that interest my needs and this is one of them...so my goal is to make the program identify the message a person puts in and turn it into a number sequence like say
a = 1
h = 2
person says ha it reads it and puts the output as 21 so im not sure how to go about this ive used google to a certain extent...also ive tried different methods and so on so can someone plz give me a basic example ty
I'm not sure if the example you gave would really count as a decent encryption. You could acheive this by simply getting the user to input a string, create a lot of if statements eahc on saying "if this letter is present, output this letter..." and then cycling through the string.
You want something more random than that and something that will change depending on letter combination. This is a common one I found before:
Cryptography is imho one of the most demanding parts of programming. It is very difficult to create a good cipher without the necessary mathematical background.
If yuo want to play around a little for yourself you could maybe start with a simple, yet quite effective stream cipher called RC4. an example you can find here: http://www-staff.lboro.ac.uk/~lbjlc/Publications/Tutorials/rc4.txt
You find a list of the most known stream cyphers on wikipedia. Three examples of a little more advanced, but also recommendable stream ciphers are AES, twofish and serpent.
#include <cctype>
#include <iostream>
#include <string>
usingnamespace std;
char crypt_lookup[ 26 + 1 ] = "qwertyuiopasdfghjklzxcvbnm";
char e( char c )
{
if (isalpha( c ))
c = crypt_lookup[ toupper( c ) - 'A' ];
return c;
}
int main()
{
string s;
cout << "Please enter a phrase to encrypt> " << flush;
getline( cin, s );
for (unsigned n = 0; n < s.length(); n++)
s[ n ] = e( s[ n ] );
cout << "The encrypted phrase is> " << s << endl;
return 0;
}
You can use the same lookup table to decrypt, except now you are first looking for the letter then returning the (index + 'a') when found.
Keep in mind that a substitution cipher is very easy to break. If you encrypt to more than one letter per source character, then you will have to make sure that there aren't any collisions in the output letter stream -- or just do something like put a space between letters...
I must say I am a cryptofreak. I've been working on various programs as a hobby for some time now. I was a little perplexed by the above sample however and when I gave it a shot I could not get it to decrypt at first, even when changing the -A to a +A. Its a bit counterintuitive I think, the code could be a lot simpler.
I think this code might be a little more easier to understand for a beginner:
You should have at least a "lookup table" of 256 elements if you are working with characters. Also a dynamic S-Box is much more effective though likewise easy to understand. Additionally you avoid having this loads of iterations since you have to "look up" only one value.