Crypt_RSA

# Class: Crypt_RSA

Source Location: /lib/3rdParty/phpseclib/Crypt/RSA.php

### Class Overview

Pure-PHP PKCS#1 compliant implementation of RSA.

#### Author(s):

- Jim Wigginton <terrafrost@php.net>

#### Version:

- 0.1.0

### Variables

- $publicKeyFormat

### Methods

- Crypt_RSA
- createKey
- decrypt
- encrypt
- getPublicKey
- loadKey
- setEncryptionMode
- setHash
- setMGFHash
- setPassword
- setPrivateKeyFormat
- setPublicKey
- setPublicKeyFormat
- setSaltLength
- setSignatureMode
- sign
- verify
- _random

### Class Details

[line 229]
Pure-PHP PKCS#1 compliant implementation of RSA.

#### Tags:

author: | Jim Wigginton <terrafrost@php.net>
version: | 0.1.0
access: | public

[ Top ]

### Class Variables

#### $publicKeyFormat = CRYPT_RSA_PUBLIC_FORMAT_PKCS1

[line 260]

Public Key Format

#### Tags:

access: | public

Type: | Integer

[ Top ]

### Class Methods

### constructor Crypt_RSA [line 424]

`Crypt_RSA Crypt_RSA( )`

The constructor

If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason Crypt_RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late.

#### Tags:

access: | public

[ Top ]

### method createKey [line 460]

`void createKey( [optional $bits = 1024], [optional $timeout = false], [optional $partial = array()])`

Create public / private key pair

Returns an array with the following three elements:

- 'privatekey': The private key.
- 'publickey': The public key.
- 'partialkey': A partially computed key (if the execution time exceeded $timeout). Will need to be passed back to Crypt_RSA::createKey() as the third parameter for further processing.

#### Tags:

access: | public

#### Parameters:

optional | $bits | Integer $bits
optional | $timeout | Integer $timeout
optional | $partial | Math_BigInteger $p

[ Top ]

### method decrypt [line 2281]

`String decrypt( $ciphertext, String $plaintext)`

Decryption

#### Tags:

see: | Crypt_RSA::encrypt()
access: | public

#### Parameters:

String | $plaintext |
| $ciphertext |

[ Top ]

### method encrypt [line 2242]

`String encrypt( String $plaintext)`

Encryption

Both CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_ENCRYPTION_PKCS1 both place limits on how long $plaintext can be. If $plaintext exceeds those limits it will be broken up so that it does and the resultant ciphertext's will be concatenated together.

#### Tags:

see: | Crypt_RSA::decrypt()
access: | public

#### Parameters:

String | $plaintext |

[ Top ]

### method getPublicKey [line 1260]

`void getPublicKey( [Integer $type = CRYPT_RSA_PUBLIC_FORMAT_PKCS1], String $key)`

Returns the public key

The public key is only returned under two circumstances - if the private key had the public key embedded within it or if the public key was set via setPublicKey(). If the currently loaded key is supposed to be the public key this function won't return it since this library, for the most part, doesn't distinguish between public and private keys.

#### Tags:

see: | Crypt_RSA::getPublicKey()
access: | public

#### Parameters:

String | $key |
Integer | $type | optional

[ Top ]

### method loadKey [line 1156]

`void loadKey( String $key, [Integer $type = false])`

Loads a public or private key

Returns true on success and false on failure (ie. an incorrect password was provided or the key was malformed)

#### Tags:

access: | public

#### Parameters:

String | $key |
Integer | $type | optional

[ Top ]

### method setEncryptionMode [line 2212]

`void setEncryptionMode( Integer $mode)`

Set Encryption Mode

Valid values include CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_ENCRYPTION_PKCS1.

#### Tags:

access: | public

#### Parameters:

Integer | $mode |

[ Top ]

### method setHash [line 1390]

`void setHash( String $hash)`

Determines which hashing function should be used

Used with signature production / verification and (if the encryption mode is CRYPT_RSA_ENCRYPTION_OAEP) encryption and decryption. If $hash isn't supported, sha1 is used.

#### Tags:

access: | public

#### Parameters:

String | $hash |

[ Top ]

### method setMGFHash [line 1419]

`void setMGFHash( String $hash)`

Determines which hashing function should be used for the mask generation function

The mask generation function is used by CRYPT_RSA_ENCRYPTION_OAEP and CRYPT_RSA_SIGNATURE_PSS and although it's best if Hash and MGFHash are set to the same thing this is not a requirement.

#### Tags:

access: | public

#### Parameters:

String | $hash |

[ Top ]

### method setPassword [line 1210]

`void setPassword( String $password)`

Sets the password

Private keys can be encrypted with a password. To unset the password, pass in the empty string or false. Or rather, pass in $password such that empty($password) is true.

#### Tags:

see: | Crypt_RSA::createKey()
see: | Crypt_RSA::loadKey()
access: | public

#### Parameters:

String | $password |

[ Top ]

### method setPrivateKeyFormat [line 1364]

`void setPrivateKeyFormat( Integer $format)`

Determines the private key format

#### Tags:

see: | Crypt_RSA::createKey()
access: | public

#### Parameters:

Integer | $format |

[ Top ]

### method setPublicKey [line 1234]

`Boolean setPublicKey( String $key, [Integer $type = CRYPT_RSA_PUBLIC_FORMAT_PKCS1])`

Defines the public key

Some private key formats define the public exponent and some don't. Those that don't define it are problematic when used in certain contexts. For example, in SSH-2, RSA authentication works by sending the public key along with a message signed by the private key to the server. The SSH-2 server looks the public key up in an index of public keys and if it's present then proceeds to verify the signature. Problem is, if your private key doesn't include the public exponent this won't work unless you manually add the public exponent.

Do note that when a new key is loaded the index will be cleared.

Returns true on success, false on failure

#### Tags:

see: | Crypt_RSA::getPublicKey()
access: | public

#### Parameters:

String | $key |
Integer | $type | optional

[ Top ]

### method setPublicKeyFormat [line 1376]

`void setPublicKeyFormat( Integer $format)`

Determines the public key format

#### Tags:

see: | Crypt_RSA::createKey()
access: | public

#### Parameters:

Integer | $format |

[ Top ]

### method setSaltLength [line 1448]

`void setSaltLength( $sLen, Integer $format)`

Determines the salt length

To quote from RFC3447#page-38:

Typical salt lengths in octets are hLen (the length of the output of the hash function Hash) and 0.

#### Tags:

access: | public

#### Parameters:

Integer | $format |
| $sLen |

[ Top ]

### method setSignatureMode [line 2225]

`void setSignatureMode( Integer $mode)`

Set Signature Mode

Valid values include CRYPT_RSA_SIGNATURE_PSS and CRYPT_RSA_SIGNATURE_PKCS1

#### Tags:

access: | public

#### Parameters:

Integer | $mode |

[ Top ]

### method sign [line 2318]

`String sign( String $message)`

Create a signature

#### Tags:

see: | Crypt_RSA::verify()
access: | public

#### Parameters:

String | $message |

[ Top ]

### method verify [line 2342]

`Boolean verify( String $message, String $signature)`

Verifies a signature

#### Tags:

see: | Crypt_RSA::sign()
access: | public

#### Parameters:

String | $message |
String | $signature |

[ Top ]

### method _random [line 1461]

`String _random( Integer $bytes, [optional $nonzero = false])`

Generates a random string x bytes long

#### Tags:

access: | public

#### Parameters:

Integer | $bytes |
optional | $nonzero | Integer $nonzero

[ Top ]

Documentation generated on Tue, 22 Nov 2011 16:30:05 +0100 by phpDocumentor 1.4.1