The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsI need to code a really simple algorithm, but my brain has apparently shut off for the night.
It's going to be an encryption key, so if my key is:
"My brain has shut off"
and I type "hello world" into the program, it would add h+M=...whatever... then e+y =.. whatever. not a problem...but
The FINAL number must fall in the standard ASCII chars (32-126)
I was thinking of dividing everything by 4 and adding a number, but I'm not sure what to do with numbers that don't divide into an integer.... everything has to stay the way it was, so that it can be decrypted. Example....
If I did (( j + E )-126) = the number 1
((106+69)-126) = 49
doesn't work for the lowest characters though... that's just an example. Any thoughts?
Perpetually looking for time to return to the arts.
Hmmm, may we see the source to look further into the issue?
I really just need the psuedocode for it. I'm currently doing it in C#... but haven't gotten to coding it yet because I haven't figured out how to add any 2 ASCII characters together (standard chars, between 32-126) and create and algorithm that only returns numbers between 32-126 back to me.
Like adding 65 + 110 = 175 (175 doesn't fall between 32-126....)
so we subract 80... now it's 95 (that works... it falls between...)
But, if we do 32 + 32 = 64 (the lowest possible solution)
and subtract 80... we get -24 (NOT between 32-126)
See my problem?
Perpetually looking for time to return to the arts.
Sort if a cheap way out but couldn't use use some if(..) statements?
if(tehInput <= 31){
tehInput += 10;
}
if(tehInput >= 127){
tehInput -= 10;
}
or even a do{ }whil() loops?
do{
...stuff here
}while(tehInput >= 32 && <= 126);
Now I know both examples are buggy just modify to your needs i'm not getting your question exactlly but from what i understand you don't want the final number to calulate below 32 or above 126 correct? If not sorry for the misunderstanding :o
Yeah, it's bugging me too because it seems like it would be simple. Pretty much encrypting text... if I enter this:
"Hello there!"
and the encryption key I was using was:
"Screw you"
It would have to take "screw you" and use that to modify the inputted text. It would add
S and H. Then c + e. Then r + l. Then e + l. Then w + o. Then <space>+<space> then the last 3 letters the same way.
The algorithm would have to take... say the H and S and manipulate them somehow so the ASCII value of them stayed within 32-126 and give a new letter. Basically a scrambler or encrypter.... but only using standard ASCII chars.
I was thinking of an if{}, but I'm not sure how that would decrypt... basically, I need to be able to type "weripaosij oaisierj akwjerao9siu" into the decrypter and have it know what a "w" turns into, what an "e" is supposed to be... and so on.
I've been messing aound with negative numbers and that's not working...same with a few other things... hmmmm. I'll keep checking this post and I'm working in notepad and visual net 2003 as we speak.
Perpetually looking for time to return to the arts.
Dunno if this function exists in C; str_replace()
So you just type str_replace('w','e','heh'); and it would return hwh :)
"My software never has bugs. It just develops random features. " - Unknown
[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]
I tried doing something very similar over a year ago. I got bored after about a day of thinking about it. Anyway, with my now l33t knowledge of algorithms and the such, you could try something like (I'm using brackets instead of tabulation, just so you know):
Get message from user
Get key from user
a := 0
b := 0
while a <= length of message do
{
// You could put this if statement into a C-style for loop in the form of "condition ? then" if you wanted to
if (b > length of key) or (b > length of message)
{
b := 0
}
// assuming that ascii(message[n]) returns the ascii code for the Nth character of message:
encryption[a] := (ascii(message[a]) / 2) + (ascii(message[a]) /2)
a := a + 1
b := b + 1
}
Or you could have a char variable in which its value would be the collective XOR of the string and then use that to xor every letter in the string you're trying to encrypt. XOR'ing is great for creating simple encryptions, The only problem is in the end your going to have to XOR the encrypted string with I believe 0x40 in order for it to be an alphanumeric character. Don't quote me on that though. I'll try to work out some C++ code for that today...
I think there is an underlying problem in what you are trying to do. From reading it seems like you are adding two numbers within a range of a to b, and want the result to always be between a and b. This, however, is impossible.
You have to think about what you are trying to accomplish. You want to make a program that takes data to be encrypted and an encryption string. The program should output the encrypted string in a readable (ascii correct) format.
I think that you can use modulos to achive a solution to this problem. The first thing I would to is define a constant that holds the range of ascii values, as well as the highest and lowest ascii value.
(Pseudo code in C)
#define RANGE 116-32
#define LOW_ASCII 32
#define HIGH_ASCII 116
To get an output number, take the ascii value of the two characters to add, and subtract the minimum value from both of them. So if the data value was 103 and the encryption value was 113, the code would look somethin like this:
int a = 103, b = 113;
a -= LOW_ASCII;
b -= LOW_ASCII;
You would then calculate the value using a modulo and the RANGE. This will always make the answer between 0 and range. Then add LOW_ASCII to keep the value between the low and high ascii values.
int c = (a+b)%RANGE + LOW_ASCII;
So, a function that would take the data charcater and the encryption character and output the new character would look like this:
char encrypt( char data, char key )
{
return ((data-LOW_ASCII)+(key-LOW_ASCII))%RANGE + LOW_ASCII;
}
With this example, if the numbers above were used, you would get 100 as an answer.
Now, you should also have a function that unencrypts the data. To do this, you would first subtract the LOW_ASCII, then take the absolute value of the modulo with the RANGE, and finally subtract that from the HIGH_ASCII (sounds complicated). Here what it would look like uncondenced into one sentence:
int data = 100, key = 113;
data -= LOW_ASCII;
key -= LOW_ASCII;
int c = abs( data-key );
c = c % RANGE;
c = HIGH_ASCII - c;
Now, c would be 103, the number you had in the first place! Now, we can write a function for this.
char unencrypt( char data, char key )
{
return HIGH_ASCII - abs((data-32) - (key-32))%RANGE;
}
Lastly, you want to make an algorithm to either encrypt or decrypt using these funcitons. You would basically go through all the data characters while also going through all of the key characters. When the key string character == '\0', you set it back to the first character. Heres some pseudo code;
void algorithm( char *datastring, char *keystring )
{
int i = 0, j = 0;
while( datastring[i] != 0 )
{
if( keystring[j] == '\0' ) j = 0;
datastring[i] = encrypt( datastring[i], keystring[j] );
i++, j++;
}
}
And there you have it. I hope this helps you in what you are trying to accomplish. However, there are no guarantees to the accuracy of this code. I haven't actually tested it out in a program yet (but maybe I will later).
FPS
I tried the program out on my computer and encountered a few problems. Luckily, I fixed them, and below are the changes.
1. In the define:
#define RANGE HIGH_ASCII-LOW_ASCII
should become:
#define RANGE (HIGH_ASCII-LOW_ASCII)
The parentheses caused problesm when doing the modulo.
2. I split up the decryption to multiple lines becuse it would be very complicated as a single line. The inside of the decryption function now looks like this:
int a = ((data-LOW_ASCII)+(RANGE+LOW_ASCII))-key;
if( a >= RANGE ) a -= RANGE;
return a + LOW_ASCII;
After I changed these things, the program worked fine. Still no guarentees, I haven't used every possible combination yet, but now I think the algorithm is sound.