• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    PHP
  • License
    BSD 3-Clause "New...
  • Created over 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PKI public/private RSA key encryption using the OpenSSL extension

Pikirasa

Build Status codecov

Easy PKI public/private RSA key encryption using the OpenSSL extension.

What's up with the name?

Pikirasa is just "PKI RSA" with a few exta vowels thrown in. Also, I created this project late on a Friday night, so I just couldn't bring myself to name it anything serious.

What is this for?

Pikirasa is very lightweight wrapper around PHP's OpenSSL extension for encrypting and decrypting data with a known public/private key pair. It requires that you have the OpenSSL extension installed, and that your certificates have already been generated.

Pikirasa is not a general purpose or all-encompassing encryption library. If you need more encryption options or maximum system compatability, take a look at phpseclib.

Installation

Just fire up Composer!

composer require vlucas/pikirasa

Example Usage

All you need is the full path to your public and/or private key files:

$rsa = new Pikirasa\RSA('path/to/public.pem', 'path/to/private.pem');

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

Under the hood, Pikirasa will make these paths file streams, and you may use any file stream directly instead :

$rsa = new Pikirasa\RSA('file:///absolute/path/to/public.pem', 'file://relative/path/to/private.pem');

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

You can also use the string contents of your public and private keys :

$publicKey = '
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7o9A47JuO3wgZ/lbOIOs
Xc6cVSiCMsrglvORM/54StFRvcrxMi7OjXD6FX5fQpUOQYZfIOFZZMs6kmNXk8xO
hgTmdMJcBWolQ85acfAdWpTpCW29YMvXNARUDb8uJKAApsISnttyCnbvp7zYMdQm
HiTG/+bYaegSXzV3YN+Ej+ZcocubUpLp8Rpzz+xmXep3BrjBycAE9z2IrrV2rlwg
TTxU/B8xmvMsToBQpA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Mq/6MCgYEA9qQj
7kb5uyU8nrXoDqKPpy6ijEpVilgy4VR7RuB2vMh74wKI1QQYED+PxfcHe5RP8WGF
Bl+7VnmrGka4xJWeN7GKW4GRx5gRAzg139DXkqwPlXyM3ZR3pLd8wtbxTmJrcPby
A6uNRhGPpuyhDs5hx9z6HvLoCs+O0A9gDaChM/0CgYEAycRguNPpA2cOFkS8l+mu
p8y4MM5eX/Qq34QiNo0ccu8rFbXb1lmQOV7/OK0Znnn+SPKITRX+1mTRPZidWx4F
aLuWSpXtEvwrad1ijuzTiVk0KWUTkKuEHrgyJplzcnvX3nTHnWXqk9kN9+v83CN/
0BVji7TT2YyUvPKEeyOlZxcCgYABFm42Icf+JEblKEYyslLR2OnMlpNT/dmTlszI
XjsH0BaDxMIXtmHoyG7434L/74J+vQBaK9fmpLi1b/RmoYZGFplWl/atm6UPj5Ll
PsWElw+miBsS6xGv/0MklNARmWuB3wToMTx5P6CTit2W9CAIQpgzxLxzN8EYd8jj
pn6vfQKBgQCHkDnpoNZc2m1JksDiuiRjZORKMYz8he8seoUMPQ+iQze66XSRp5JL
oGZrU7JzCxuyoeA/4z36UN5WXmeS3bqh6SinrPQKt7rMkK1NQYcDUijPBMt0afO+
LH0HIC1HAtS6Wztd2Taoqwe5Xm75YW0elo4OEqiAfubAC85Ec4zfxw==
-----END RSA PRIVATE KEY-----
';

$rsa = new Pikirasa\RSA($publicKey, $privateKey);

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

Creating keys

Don't have key files already? No problem - you can simply create new ones :

$rsa = new Pikirasa\RSA('path/to/nonexistent_public.pem', 'path/to/nonexistent_private.pem');
$rsa->create();  // creates new keys in the new key files

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

Need a key size other than the default of 2048 bits? Simply pass the size you need as the first parameter of $rsa->create().

Pikirasa won't overwrite existing key files unless you pass true to the second parameter of $rsa->create().

If you prefer to work with key strings over key files, you can create keys that way, too :

$rsa = new Pikirasa\RSA(null, null);
$rsa->create();  // creates new keys as strings

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted);
          // 'abc123'
var_dump($rsa->getPublicKeyFile());
          // -----BEGIN PUBLIC KEY-----
          // MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7o9A47JuO3wgZ/lbOIOs
          // Xc6cVSiCMsrglvORM/54StFRvcrxMi7OjXD6FX5fQpUOQYZfIOFZZMs6kmNXk8xO
          // hgTmdMJcBWolQ85acfAdWpTpCW29YMvXNARUDb8uJKAApsISnttyCnbvp7zYMdQm
          // HiTG/+bYaegSXzV3YN+Ej+ZcocubUpLp8Rpzz+xmXep3BrjBycAE9z2IrrV2rlwg
          // TTxU/B8xmvMsToBQpAbe+Cv130tEHsyW4UL9KZY1M9R+UHFPPmORjBKxSZvjJ1mS
          // UbUYN6PmMry35wCaFCfQoyTDUxBfxTGYqjaveQv4sxx0uvoiLXHt9cAm5Q8KJ+8d
          // FwIDAQAB
          // -----END PUBLIC KEY-----
var_dump($rsa->getPrivateKeyFile());
          // -----BEGIN RSA PRIVATE KEY-----
          // MIIEpAIBAAKCAQEA7o9A47JuO3wgZ/lbOIOsXc6cVSiCMsrglvORM/54StFRvcrx
          // Mi7OjXD6FX5fQpUOQYZfIOFZZMs6kmNXk8xOhgTmdMJcBWolQ85acfAdWpTpCW29
          // YMvXNARUDb8uJKAApsISnttyCnbvp7zYMdQmHiTG/+bYaegSXzV3YN+Ej+Zcocub
          // UpLp8Rpzz+xmXep3BrjBycAE9z2IrrV2rlwgTTxU/B8xmvMsToBQpAbe+Cv130tE
          // HsyW4UL9KZY1M9R+UHFPPmORjBKxSZvjJ1mSUbUYN6PmMry35wCaFCfQoyTDUxBf
          // xTGYqjaveQv4sxx0uvoiLXHt9cAm5Q8KJ+8dFwIDAQABAoIBAHkWS3iHy/3zjjtY
          // TV4NL8NZqO5splGDuqXEMbKzenl3b8cnKHAxY/RVIQsh3tZb9CV8P/Lfj1Fi+nLt
          // a7mAXWcXO6aONMkmzI1zQ2NL3opoxTRc+GAWd0BW5hcoMBK1CD+ciHkLqAH5xsFc
          // UFxSc5qfTkb79GMlQZYD/Hk2WwHyj7hAkyxip4ye1EOnH5h8H7vIUjwp+H6Rmt5w
          // FTiVJbokhzwiczChUJVWgnowegL/qFV+yNfHGGKqVdIQfKdCsHR6jAuKCww5QniN
          // qDEi/M2Az0R4qfVmf38uMvOJTWaxp08JV4qRyNdh6hhbj+nY1EZ8haOiC7tjz2mJ
          // XqqKQfkCgYEA95yb5ezTBF4Pbr589OnU6VFdM88BCrKKvSWE8D1fzZZTsXur5k/x
          // cOwfio4RkmJwMnjuzZN6nvL5QddfcmPWQAoepHR8eA9yhIz57YWgrqE9ZXI8DgMy
          // SFuy5EkV5vudjDIr7kBXaGuUh3ErZfglyrV/rUfydGdTWyY8phMq/6MCgYEA9qQj
          // 7kb5uyU8nrXoDqKPpy6ijEpVilgy4VR7RuB2vMh74wKI1QQYED+PxfcHe5RP8WGF
          // Bl+7VnmrGka4xJWeN7GKW4GRx5gRAzg139DXkqwPlXyM3ZR3pLd8wtbxTmJrcPby
          // A6uNRhGPpuyhDs5hx9z6HvLoCs+O0A9gDaChM/0CgYEAycRguNPpA2cOFkS8l+mu
          // p8y4MM5eX/Qq34QiNo0ccu8rFbXb1lmQOV7/OK0Znnn+SPKITRX+1mTRPZidWx4F
          // aLuWSpXtEvwrad1ijuzTiVk0KWUTkKuEHrgyJplzcnvX3nTHnWXqk9kN9+v83CN/
          // 0BVji7TT2YyUvPKEeyOlZxcCgYABFm42Icf+JEblKEYyslLR2OnMlpNT/dmTlszI
          // XjsH0BaDxMIXtmHoyG7434L/74J+vQBaK9fmpLi1b/RmoYZGFplWl/atm6UPj5Ll
          // PsWElw+miBsS6xGv/0MklNARmWuB3wToMTx5P6CTit2W9CAIQpgzxLxzN8EYd8jj
          // pn6vfQKBgQCHkDnpoNZc2m1JksDiuiRjZORKMYz8he8seoUMPQ+iQze66XSRp5JL
          // oGZrU7JzCxuyoeA/4z36UN5WXmeS3bqh6SinrPQKt7rMkK1NQYcDUijPBMt0afO+
          // LH0HIC1HAtS6Wztd2Taoqwe5Xm75YW0elo4OEqiAfubAC85Ec4zfxw==
          // -----END RSA PRIVATE KEY-----

Using Keys with a Passphrase

The Pikirasa\RSA class constructor accepts an optional 3rd parameter if your private key is protected with a password.

$rsa = new Pikirasa\RSA($publicKey, $privateKey, 'certificate_password');

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

This approach also works when creating new keys that should be password protected :

$rsa = new Pikirasa\RSA($publicKey, $privateKey, 'certificate_password');
$rsa->create(); // creates new keys, with the private key password-protected

$data = 'abc123';
$encrypted = $rsa->encrypt($data);
$decrypted = $rsa->decrypt($encrypted);
var_dump($decrypted); // 'abc123'

$rsa2 = new Pikirasa\RSA($publicKey, $privateKey);
$decrypted = $rsa2->decrypt($encrypted); // Throws `Pikirasa\Exception` for bad/missing password

Working with base64-encoded strings

A common pattern if you want to deal with plain strings rather than binary data is to encode encryped data with base64. If you need to do that, both encrypt and decrypt have a base64 counterpart you can use :

$rsa = new Pikirasa\RSA($publicKey, $privateKey);

$data = 'abc123';
$encrypted = $rsa->base64Encrypt($data);
$decrypted = $rsa->base64Decrypt($encrypted);
var_dump($decrypted); // 'abc123'

More Repositories

1

phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
PHP
13,033
star
2

valitron

Valitron is a simple, elegant, stand-alone validation library with NO dependencies
PHP
1,567
star
3

frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
JavaScript
1,524
star
4

bulletphp

A resource-oriented micro PHP framework
PHP
418
star
5

phpDataMapper

Object-Oriented PHP5 DataMapper ORM
PHP
118
star
6

sheetquery

Query Builder/ORM for Google Sheets
TypeScript
78
star
7

Spot

[DEPRECATED - use v2] Simple DataMapper ORM for PHP 5.3+
PHP
75
star
8

devdata.io

The Data You Need, The Programming Language You Want
HTML
75
star
9

vlid

Lightweight validation library with NO dependencies. A nice Joi alternative with a similar API.
JavaScript
65
star
10

universal-react-helloworld

Simplest possible starting point for using universal/isomorphic React.js + Node.js + Express.js
JavaScript
42
star
11

dos-css

DOS-Style CSS for retro fun
HTML
24
star
12

gasmask

Mocks for Google Apps Script libraries, specifically around Spreadsheets
TypeScript
22
star
13

hyperspan-php

Build a Hypermedia API response once and return it in multiple formats
PHP
18
star
14

toystore

Lightweight central store of state with the ability to watch for and react to specific property changes
JavaScript
16
star
15

bulletphp-skeleton

Skeleton example app using Bullet with templates and error handling
PHP
9
star
16

jsx-tmpl

Stop transpiling React components for Node. Use native ES6 template literals that output JSX instead!
JavaScript
8
star
17

turbolinks-demo-node

Copy of the Ruby Turbolinks Demo app in Node.js
HTML
6
star
18

echotag

Super simple ES6 tagged template function for printing an HTML string
JavaScript
6
star
19

toystore-react

React bindings for toystore (central store of state)
JavaScript
5
star
20

nudos

Heavily retro/DOS-inspired CSS while still modern enough to use
HTML
4
star
21

brightbudget-web

Demo web app API for a Hypermedia API presentation that powers brightbudget-app
JavaScript
4
star
22

presentation-slides-you-dont-know-nodejs

Presentation Slides for "You Don't Know Node.js"
CSS
4
star
23

jTreePlus

jTree with modifications to allow clickable links and form elements within tree nodes
JavaScript
3
star
24

jsx-native

Build JSX using native ES6 templates. No transpiling required for Node.js and modern browsers.
JavaScript
3
star
25

dotfiles-old

Personal dotfiles - mainly for ZSH and Vim configurations
Vim Script
3
star
26

phpunit-gearman-testing

Exploratory project on possible ways to test Gearman jobs using PHPUnit
PHP
3
star
27

brightbudget-app

Demo app for Hypermedia API presentation
JavaScript
3
star
28

spot-site

Spot DataMapper Website
HTML
2
star
29

example-browser-debugging

JavaScript
2
star
30

frisby-site

Main website for Frisby.js with documentation
2
star
31

startuporpharma.com

Is it a startup, or a pharmaceutical drug?
JavaScript
1
star
32

titanium-example-basic-counter

Example Titanium app with simple single-model binding to increment a counter
JavaScript
1
star
33

dotfiles-old-fresh

Vance's dotfiles managed by fresh
Shell
1
star
34

twicebreaker

Icebreaker game with the Twilio API
PHP
1
star
35

skycap

Drop-in authentication and authoriztion framework for Express.js
JavaScript
1
star
36

titemplate

Appcelerator Titanium template I use to start new projects - with coffeescript, Jade, a TSS reset, and other goodies
JavaScript
1
star
37

presentation-slides-js-browser-debugging

Presentation Slides for "Effective JavaScript Browser Debugging"
CSS
1
star
38

bulletphp-site

Main BulletPHP Website
PHP
1
star
39

budgetsheet-website-static-html

BudgetSheet Website
HTML
1
star
40

swagjs-site

Initial splash page signup site for JavaScript blocks
HTML
1
star
41

openx-oauth-client

Modern PHP client for working with the OpenX v4 oAuth API
PHP
1
star
42

titanium-chartjs-example

Example App with Chart.js Example in WebView
JavaScript
1
star
43

bulletphp-blog-example

Obligatory Blog Example built with the Bullet PHP Microframework
PHP
1
star