From 1d924cfc7ba2626d811846ec751992f3c9c1a79b Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 25 May 2015 22:30:38 -0500 Subject: [PATCH] RSA: add "none" encryption mode --- phpseclib/Crypt/RSA.php | 33 +++++++++++++++++++++++++++++++ tests/Unit/Crypt/RSA/ModeTest.php | 30 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/Unit/Crypt/RSA/ModeTest.php diff --git a/phpseclib/Crypt/RSA.php b/phpseclib/Crypt/RSA.php index 3146ca14..85477e89 100644 --- a/phpseclib/Crypt/RSA.php +++ b/phpseclib/Crypt/RSA.php @@ -107,6 +107,13 @@ define('CRYPT_RSA_ENCRYPTION_OAEP', 1); * compatibility with protocols (like SSH-1) written before OAEP's introduction. */ define('CRYPT_RSA_ENCRYPTION_PKCS1', 2); +/** + * Do not use any padding + * + * Although this method is not recommended it can none-the-less sometimes be useful if you're trying to decrypt some legacy + * stuff, if you're trying to diagnose why an encrypted message isn't decrypting, etc. + */ +define('CRYPT_RSA_ENCRYPTION_NONE', 3); /**#@-*/ /**#@+ @@ -2444,6 +2451,22 @@ class Crypt_RSA return substr($m, 1); } + /** + * Raw Encryption / Decryption + * + * Doesn't use padding and is not recommended. + * + * @access private + * @param String $m + * @return String + */ + function _raw_encrypt($m) + { + $temp = $this->_os2ip($m); + $temp = $this->_rsaep($temp); + return $this->_i2osp($temp, $this->k); + } + /** * RSAES-PKCS1-V1_5-ENCRYPT * @@ -2890,6 +2913,13 @@ class Crypt_RSA function encrypt($plaintext) { switch ($this->encryptionMode) { + case CRYPT_RSA_ENCRYPTION_NONE: + $plaintext = str_split($plaintext, $this->k); + $ciphertext = ''; + foreach ($plaintext as $m) { + $ciphertext.= $this->_raw_encrypt($m); + } + return $ciphertext; case CRYPT_RSA_ENCRYPTION_PKCS1: $length = $this->k - 11; if ($length <= 0) { @@ -2938,6 +2968,9 @@ class Crypt_RSA $plaintext = ''; switch ($this->encryptionMode) { + case CRYPT_RSA_ENCRYPTION_NONE: + $decrypt = '_raw_encrypt'; + break; case CRYPT_RSA_ENCRYPTION_PKCS1: $decrypt = '_rsaes_pkcs1_v1_5_decrypt'; break; diff --git a/tests/Unit/Crypt/RSA/ModeTest.php b/tests/Unit/Crypt/RSA/ModeTest.php new file mode 100644 index 00000000..113488cf --- /dev/null +++ b/tests/Unit/Crypt/RSA/ModeTest.php @@ -0,0 +1,30 @@ + + * @copyright 2013 Jim Wigginton + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +require_once 'Crypt/RSA.php' ; + +class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase +{ + public function testEncryptionModeNone() + { + $plaintext = 'a'; + + $rsa = new Crypt_RSA(); + + extract($rsa->createKey()); + $rsa->loadKey($publickey); + + $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_NONE); + $a = $rsa->encrypt($plaintext); + $b = $rsa->encrypt($plaintext); + + $this->assertEquals($a, $b); + + $rsa->loadKey($privatekey); + $this->assertEquals(trim($rsa->decrypt($a), "\0"), $plaintext); + } +}