Merge branch 'rsa-none-1.0' into rsa-none-2.0

Conflicts:
	phpseclib/Crypt/RSA.php
This commit is contained in:
terrafrost 2015-05-25 22:36:10 -05:00
commit 30260f3321
2 changed files with 63 additions and 0 deletions

View File

@ -91,6 +91,13 @@ class RSA
* compatibility with protocols (like SSH-1) written before OAEP's introduction.
*/
const 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.
*/
const ENCRYPTION_NONE = 3;
/**#@-*/
/**#@+
@ -2377,6 +2384,22 @@ class 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
*
@ -2823,6 +2846,13 @@ class RSA
function encrypt($plaintext)
{
switch ($this->encryptionMode) {
case self::ENCRYPTION_NONE:
$plaintext = str_split($plaintext, $this->k);
$ciphertext = '';
foreach ($plaintext as $m) {
$ciphertext.= $this->_raw_encrypt($m);
}
return $ciphertext;
case self::ENCRYPTION_PKCS1:
$length = $this->k - 11;
if ($length <= 0) {
@ -2871,6 +2901,9 @@ class RSA
$plaintext = '';
switch ($this->encryptionMode) {
case self::ENCRYPTION_NONE:
$decrypt = '_raw_encrypt';
break;
case self::ENCRYPTION_PKCS1:
$decrypt = '_rsaes_pkcs1_v1_5_decrypt';
break;

View File

@ -0,0 +1,30 @@
<?php
/**
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2013 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use phpseclib\Crypt\RSA;
class Unit_Crypt_RSA_ModeTest extends PhpseclibTestCase
{
public function testEncryptionModeNone()
{
$plaintext = 'a';
$rsa = new RSA();
extract($rsa->createKey());
$rsa->loadKey($publickey);
$rsa->setEncryptionMode(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);
}
}