Help with encrypt/decrypt program
Author |
Message |
Thuged_Out_G
|
Posted: Sun May 21, 2006 12:52 am Post subject: Help with encrypt/decrypt program |
|
|
Im making a program that will take input, encrypt it and store it to a file. Which will later be decrypted and shown to the user. Im using a very basid encryption method, Ive already written the code to encrypt/decrypt the input. Im using ord to encrypt each letter, and then multiplying that by a given value and then storing each letter in an array. I want to make every other element of the array a letter like such
input is :test
i take the first letter and go ord(test(i)) and then store that in the first element in the array ... I want the next element in the array to be a random vowel, I've got an array with 5 elements each containing one vowel. I choose the vowel randomly by using extra(Rand.Int(1,5)) ... IM having trouble in my for loop string the encrypted value in one element in the random vowel in the next. Any help i would greatly appreciate.
Here is my code so far:
code: |
var ePass : flexible array 1 .. 255 of string
var extra : array 1 .. 5 of string := init ("a", "e", "i", "o", "u")
var phrase : string
var count := 1
proc GetPass
put "Enter phrase to encrypt: " ..
get phrase
new ePass, length (phrase)
end GetPass
fcn encrypt (letter : string) : string
var temps : int
put "encrypted: " ..
for i : 1 .. upper (ePass)
ePass (i) := letter (i)
temps := ord (ePass (i))
temps *= 32
ePass (i) := intstr (temps)
if i < upper (ePass) then
put ePass (i) ..
else
put ePass (i)
end if
count += 1
end for
result " Encryption Succesful!"
end encrypt
fcn decrypt : string
var temps : int
put "Decrypted: " ..
for i : 1 .. upper (ePass)
temps := strint (ePass (i))
temps := temps div 32
ePass (i) := chr (temps)
if i < upper (ePass) then
put ePass (i) ..
else
put ePass (i)
end if
end for
result " Decryption Successful"
end decrypt
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Thuged_Out_G
|
Posted: Sun May 21, 2006 1:18 am Post subject: (No subject) |
|
|
Ok, so ive figured out how to see if the i is even or odd, so i know when to store what in the for loop, but yet im still getting errors
code: |
fcn encrypt (letter : string) : string
var temps : int
put "encrypted: " ..
for i : 1 .. upper (ePass) * 2
if i mod 2 = 0 then
ePass (i) := letter (i)
temps := ord (ePass (i))
temps *= 32
ePass (i) := intstr (temps)
else
ePass (i) := letter (i)
temps := ord (ePass (i))
temps *= 32
ePass (i) := extra (Rand.Int (1, 5))
end if
end for
result " Encryption Succesful!"
end encrypt
|
That is the updated encryption code... Im getting the error "substring index is greater then length of string .... Its referring to:
"ePass (i) := letter (i)"
Any help? |
|
|
|
|
|
TheOneTrueGod
|
Posted: Sun May 21, 2006 8:52 am Post subject: (No subject) |
|
|
Erm, why are you using it as a function in this way... The result should be the encrypted info, not "encrypting successful"...
If you just wanted it to output "encryption successful" when the encryption was done, then you should have just used a procedure...
Also, as a general rule, functions shouldn't output anything to the screen. (This is circumstantial, but if you wanted to use your function in some other program, and it spontaneously erased your entire background to output the code... well, its not that great...)
By reading through your code, it looks like it should work (with the exception of the result thing), but your order is kinda funky... Why not have encrypt accept one string parameter, and decrypt accept a string parameter as well, then work ONLY with those locally declared variables. You should find it will work a lot better. (Oh, one other problem you may be having is when you encrypt the letter "a", it becomes "65", and then when you try and decrypt it, you try and decrypt one character at a time of the new one, so it will decrypt 6, and then decrypt 5... this doesn't work so well, for obvious reasons...) |
|
|
|
|
|
TheOneTrueGod
|
Posted: Sun May 21, 2006 8:58 am Post subject: (No subject) |
|
|
Er, sorry for the double post, but I forgot to address your problem in the second post, and the edit button seems to be afraid of me recently (Hint hint, what happened to it mods? )
Allright, think about it for a second... your going in the for loop from 1 to upper(ePass) * 2. This translates into 255*2 = 510. You are then trying to access element 510 of both ePass and letter, which obviously doesn't exist because neither of them go past 255 (and in letters case, with your test variable, it doesn't go past 5...) This is where your error is spawning from. |
|
|
|
|
|
Delos
|
Posted: Sun May 21, 2006 10:44 am Post subject: (No subject) |
|
|
TheOneTrueGod wrote: Er, sorry for the double post, but I forgot to address your problem in the second post, and the edit button seems to be afraid of me recently (Hint hint, what happened to it mods? )
The Edit button is evil, and hence only Mods should have access to it - since we have proven our ability to curb our inner evilness...now don't ask about this again...
As for this problem, multiplication is generally a bad idea, for reasons outlined by TOTG. I would stick to a simply Caesar Cypher approach for now (in which case you'll be adding an amount to each value as opposed to multiplying it). This means that if you get to the instance where your result is over 255, you can just 'wrap around'...a lot easier to accomplish with addition than with multiplication (think about it, if you were multiplying a number, and it ended up being 720 values greater, you'd have to wrap around twice. When you tried to decrypt this, how would you know to wrap backwards once or twice?). |
|
|
|
|
|
Thuged_Out_G
|
Posted: Sun May 21, 2006 12:29 pm Post subject: (No subject) |
|
|
TheOneTrueGod wrote:
Allright, think about it for a second... your going in the for loop from 1 to upper(ePass) * 2. This translates into 255*2 = 510.
Well, the array is initially going to 255, i believe that is the max length of a string in turing???
Put once the input is gotten from the user, the upper element of array is changed
code: |
new ePass, length (phrase)
|
|
|
|
|
|
|
TheOneTrueGod
|
Posted: Sun May 21, 2006 5:16 pm Post subject: (No subject) |
|
|
Your still going to the upper bound * 2, which will result in an error, because its greater than the upper bound |
|
|
|
|
|
|
|