Uses mcrypt, if available, and an internal implementation, otherwise. Operates in the EDE3 mode (encrypt-decrypt-encrypt).
PHP versions 4 and 5
Here's a short example of how to use this library:
setKey('abcdefghijklmnopqrstuvwx');
$size = 10 * 1024;
$plaintext = '';
for ($i = 0; $i < $size; $i++) {
$plaintext.= 'a';
}
echo $des->decrypt($des->encrypt($plaintext));
?>
LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Constants
CRYPT_DES_MODE_3CBC
= -2
Encrypt / decrypt using inner chaining Inner chaining is used by SSH-1 and is generally considered to be less secure then outer chaining (CRYPT_DES_MODE_CBC3).
CRYPT_DES_MODE_CBC3
= CRYPT_DES_MODE_CBC
Encrypt / decrypt using outer chaining Outer chaining is used by SSH-2 and when the mode is set to CRYPT_DES_MODE_CBC.
Default Constructor. Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
Generate CTR XOR encryption key Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
plaintext / ciphertext in CTR mode.
Arguments
Name
Type
Description
Default
$length
Integer
$iv
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
see
access
private
_pad(
$text,
)
:
n/a
Description
Pads a string Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
and padding will, hence forth, be enabled.
Unpads a string If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
and false will be returned.
Arguments
Name
Type
Description
Default
$text
n/a
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
private
decrypt(
String
$ciphertext,
)
:
n/a
Description
Decrypts a message.
Arguments
Name
Type
Description
Default
$ciphertext
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
access
public
disableContinuousBuffer(
)
:
n/a
Description
Treat consecutive packets as if they are a discontinuous buffer. The default behavior.
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
disablePadding(
)
:
n/a
Description
Do not pad packets.
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
enableContinuousBuffer(
)
:
n/a
Description
Treat consecutive "packets" as if they are a continuous buffer. Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
will yield different outputs:
echo $des->encrypt(substr($plaintext, 0, 8));
echo $des->encrypt(substr($plaintext, 8, 8));
echo $des->encrypt($plaintext);
The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
another, as demonstrated with the following:
$des->encrypt(substr($plaintext, 0, 8));
echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
outputs. The reason is due to the fact that the initialization vector's change after every encryption /
decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
however, they are also less intuitive and more likely to cause you problems.
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
enablePadding(
)
:
n/a
Description
Pad "packets". DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
transmitted separately)
Return value
Type
Description
n/a
n/a
Tags
Name
Description
see
access
public
encrypt(
String
$plaintext,
)
:
n/a
Description
Encrypts a message.
Arguments
Name
Type
Description
Default
$plaintext
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
access
public
setIV(
String
$iv,
)
:
n/a
Description
Sets the initialization vector. (optional) SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
to be all zero's.
Arguments
Name
Type
Description
Default
$iv
String
Return value
Type
Description
n/a
n/a
Tags
Name
Description
access
public
setKey(
String
$key,
)
:
n/a
Description
Sets the key. Keys can be of any length. Triple DES, itself, can use 128-bit (eg. strlen($key) == 16) or
192-bit (eg. strlen($key) == 24) keys. This function pads and truncates $key as appropriate.
DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
If the key is not explicitly set, it'll be assumed to be all zero's.
Does the demcrypt resource need to be (re)initialized?
String
public
$decryptIV
=
"\0\0\0\0\0\0\0\0"
A "sliding" Initialization Vector
String
public
$demcrypt
=
mcrypt resource for decryption The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
Array
public
$des
=
The Crypt_DES objects
String
public
$ecb
=
mcrypt resource for CFB mode
String
public
$enbuffer
=
''
Encryption buffer for CTR, OFB and CFB modes
Boolean
public
$enchanged
=
true
Does the enmcrypt resource need to be (re)initialized?
String
public
$encryptIV
=
"\0\0\0\0\0\0\0\0"
A "sliding" Initialization Vector
String
public
$enmcrypt
=
mcrypt resource for encryption The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.