mirror of
https://github.com/phpseclib/phpseclib.git
synced 2024-11-17 18:55:13 +00:00
Merge pull request #693 from terrafrost/rsa-none-2.0
add "none" encryption mode for 2.0 branch * terrafrost/rsa-none-2.0: Tests/RSA: pack expected result Tests/RSA: pack expected result Tests/RSA: re-added accidentally removed $rsa initialization Tests/RSA: use correct constant Tests/RSA: fix error Tests/RSA: hard-coded key and result RSA: add "none" encryption mode
This commit is contained in:
commit
902e41a2a4
@ -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;
|
||||
|
46
tests/Unit/Crypt/RSA/ModeTest.php
Normal file
46
tests/Unit/Crypt/RSA/ModeTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?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();
|
||||
|
||||
$privatekey = '-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp
|
||||
wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5
|
||||
1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh
|
||||
3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2
|
||||
pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX
|
||||
GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il
|
||||
AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF
|
||||
L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k
|
||||
X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl
|
||||
U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ
|
||||
37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=
|
||||
-----END RSA PRIVATE KEY-----';
|
||||
$rsa->loadKey($privatekey);
|
||||
$rsa->loadKey($rsa->getPublicKey());
|
||||
|
||||
$rsa->setEncryptionMode(RSA::ENCRYPTION_NONE);
|
||||
$expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' .
|
||||
'5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' .
|
||||
'4d04bd467f6f1e7c';
|
||||
$expected = pack('H*', $expected);
|
||||
$result = $rsa->encrypt($plaintext);
|
||||
|
||||
$this->assertEquals($result, $expected);
|
||||
|
||||
$rsa->loadKey($privatekey);
|
||||
$this->assertEquals(trim($rsa->decrypt($result), "\0"), $plaintext);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user