Question

Right now I am developing a cryptography app and I have a problem with this one

$stralphabet=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

As you can see the string above is a string of alphabet. I am making a caesar shift encryption, the algorithm is simple.

Count the number of string and then substitute each letter based from the position, so for example:

string is "AB" after encryption it would be "CD"

The problem is if I have the string YZ" it gives me an error

Notice: Undefined offset: 26 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19

Notice: Undefined offset: 27 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19

Can you help me with this one?

Était-ce utile?

La solution

You apparently are accessing $stralphabel without checking for edge case (i.e. $stralp[$i+1]). You should add some checks or use modulo operator, so instead of just:

$stralp[$i+1]

you'd have

$stralp[($i+1) % XXX]

where XXX is number of entries in your stralp array.

PS: this is not even close to cryptography.

Autres conseils

Your array has 26 letters indexed from 0 to 25, so something is wrong with your cycle end condition.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top