patternphpMinor
Alternated Caesar Cipher
Viewed 0 times
ciphercaesaralternated
Problem
I've recently picked up PHP, for the first assignment in my IT-security class I decided to code it in PHP.
The algorithm is an alternation of the Caesar cipher, instead of using a fixed number to push the letters a keyword is applied to rearrange a couple of letters and then move the rest of the letters accordingly.
I am pretty sure there can be improvements in string usage and probably also in the algorithm?
Any ideas and notes on programming style and such are very welcome.
Code:
https://github.com/TheLML/encrypt/blob/master/encrypt.php
or:
```
letters[$i][0] = chr(65+$i);
$this->letters[$i][1] = 0;
}
for($i=26;$iletters[$i][0] = chr(97+$i-26);
$this->letters[$i][1] = 0;
}
for($i=52;$iletters[$i][0] = $i-52;
$this->letters[$i][1] = 0;
}
$this->letters[62][0] = ' ';
$this->letters[62][1] = 0;
}
/**
* Encrypts the key using an alternation of the caesar cipher.
* The key is used to set a different alternation for the letters, instead
* of having it moved by a constant number.
*
* @throws Exception - if there is no key
* @return string - returns the encrypted alphabet as array
*/
private function _encrypt()
{
if(empty($this->key))
{
throw new Exception("Empty key: cannot create encryption without a key.");
}
$key = $this->key;
$letters = $this->letters;
$encryption = array_fill(0, 63, '');
$pos = 0;
//add key to encoding. each letter only once, though
for($i=0;$iletters[$i][1] = 0;
}
return $encryption;
}
/**
* Encrypts the given word.
*
* @throws Exception - if there is no unencrypted word
* @return string - Encrypted word
*/
public function encrypt()
{
if(empty($this->uword))
{
throw
The algorithm is an alternation of the Caesar cipher, instead of using a fixed number to push the letters a keyword is applied to rearrange a couple of letters and then move the rest of the letters accordingly.
I am pretty sure there can be improvements in string usage and probably also in the algorithm?
Any ideas and notes on programming style and such are very welcome.
Code:
https://github.com/TheLML/encrypt/blob/master/encrypt.php
or:
```
letters[$i][0] = chr(65+$i);
$this->letters[$i][1] = 0;
}
for($i=26;$iletters[$i][0] = chr(97+$i-26);
$this->letters[$i][1] = 0;
}
for($i=52;$iletters[$i][0] = $i-52;
$this->letters[$i][1] = 0;
}
$this->letters[62][0] = ' ';
$this->letters[62][1] = 0;
}
/**
* Encrypts the key using an alternation of the caesar cipher.
* The key is used to set a different alternation for the letters, instead
* of having it moved by a constant number.
*
* @throws Exception - if there is no key
* @return string - returns the encrypted alphabet as array
*/
private function _encrypt()
{
if(empty($this->key))
{
throw new Exception("Empty key: cannot create encryption without a key.");
}
$key = $this->key;
$letters = $this->letters;
$encryption = array_fill(0, 63, '');
$pos = 0;
//add key to encoding. each letter only once, though
for($i=0;$iletters[$i][1] = 0;
}
return $encryption;
}
/**
* Encrypts the given word.
*
* @throws Exception - if there is no unencrypted word
* @return string - Encrypted word
*/
public function encrypt()
{
if(empty($this->uword))
{
throw
Solution
Give the class a better name... a noun makes more sense than a verb.
Consider simply removing the
Do the same with decrypt:
Put a $key argument in the constructor:
With the $key invariant satisfied, no need to throw an exception if not set. Remove the setKey/getKey methods (unless you really need them).
Cleaner calling code example:
Consider simply removing the
$uword and $eword properties and applicable setter methods. Instead, your encrypt method signature could be:/**
* @param string $uword word to encrypt
* @return string encrypted word
*/
public function encrypt($uword)
...Do the same with decrypt:
/**
* @param string $eword word to decrypt
* @return string decrypted word
*/
public function decrypt($eword)
...Put a $key argument in the constructor:
public function __construct($key)
{
$this->key = $key;
...With the $key invariant satisfied, no need to throw an exception if not set. Remove the setKey/getKey methods (unless you really need them).
Cleaner calling code example:
$cipher = new CeaserCipher('my key, get this from where ever');
$encryptedValue = $cipher->encrypt('foo');
$decryptedValue = $cipher->decrypt($encryptedValue);Code Snippets
/**
* @param string $uword word to encrypt
* @return string encrypted word
*/
public function encrypt($uword)
.../**
* @param string $eword word to decrypt
* @return string decrypted word
*/
public function decrypt($eword)
...public function __construct($key)
{
$this->key = $key;
...$cipher = new CeaserCipher('my key, get this from where ever');
$encryptedValue = $cipher->encrypt('foo');
$decryptedValue = $cipher->decrypt($encryptedValue);Context
StackExchange Code Review Q#24529, answer score: 5
Revisions (0)
No revisions yet.