{"id":19324908,"url":"https://github.com/spatie/crypto","last_synced_at":"2025-05-14T10:06:20.180Z","repository":{"id":41974030,"uuid":"310623861","full_name":"spatie/crypto","owner":"spatie","description":"Encrypt and decrypt data using private/public keys","archived":false,"fork":false,"pushed_at":"2025-05-12T05:16:36.000Z","size":99,"stargazers_count":469,"open_issues_count":0,"forks_count":59,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-12T06:26:45.370Z","etag":null,"topics":["php","security"],"latest_commit_sha":null,"homepage":"https://freek.dev/1829-encrypting-and-signing-data-using-privatepublic-keys-in-php","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2020-11-06T14:46:03.000Z","updated_at":"2025-05-12T05:16:32.000Z","dependencies_parsed_at":"2024-04-29T06:25:46.440Z","dependency_job_id":"6ad5c363-8d0d-4513-9dd9-726ba2880e33","html_url":"https://github.com/spatie/crypto","commit_stats":{"total_commits":68,"total_committers":12,"mean_commits":5.666666666666667,"dds":"0.38235294117647056","last_synced_commit":"956a12c8c1d96cc9a242a2f48cfa88baeed50185"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcrypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcrypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcrypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcrypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/crypto/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254119471,"owners_count":22017951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","security"],"created_at":"2024-11-10T02:07:29.297Z","updated_at":"2025-05-14T10:06:20.128Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":[],"sub_categories":[],"readme":"# Encrypting and signing data using private/public keys\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/crypto.svg?style=flat-square)](https://packagist.org/packages/spatie/crypto)\n![Tests](https://github.com/spatie/crypto/workflows/Tests/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/crypto.svg?style=flat-square)](https://packagist.org/packages/spatie/crypto)\n\n\nThis package allows you to easily generate a private/public key pairs, and encrypt/decrypt messages using those keys.\n\n```php\nuse Spatie\\Crypto\\Rsa\\KeyPair;\nuse Spatie\\Crypto\\Rsa\\PrivateKey;\nuse Spatie\\Crypto\\Rsa\\PublicKey;\n\n// generating an RSA key pair\n[$privateKey, $publicKey] = (new KeyPair())-\u003egenerate();\n\n// when passing paths, the generated keys will be written those paths\n(new KeyPair())-\u003egenerate($pathToPrivateKey, $pathToPublicKey);\n\n$data = 'my secret data';\n\n$privateKey = PrivateKey::fromFile($pathToPrivateKey);\n$encryptedData = $privateKey-\u003eencrypt($data); // returns something unreadable\n\n$publicKey = PublicKey::fromFile($pathToPublicKey);\n$decryptedData = $publicKey-\u003edecrypt($encryptedData); // returns 'my secret data'\n```\n\nMost functions in this package are wrappers around `openssl_*` functions to improve DX.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/package-skeleton-php.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/crypto)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/crypto\n```\n\n## Usage\n\nYou can generate a key pair using the `generate` function on the `KeyPair` class.\n\n```php\nuse Spatie\\Crypto\\Rsa\\KeyPair;\n\n[$privateKey, $publicKey] = (new KeyPair())-\u003egenerate();\n```\n\nYou can write the keys to disk, by passing paths to the `generate` function. \n\n```php\n// when passing paths, the generate keys will to those paths\n(new KeyPair())-\u003egenerate($pathToPrivateKey, $pathToPublicKey)\n```\n\nYou can protect the private key with a password by using the `password` method:\n\n```php\n[$passwordProtectedPrivateKey, $publicKey] = (new KeyPair())-\u003epassword('my-password')-\u003egenerate();\n```\n\nWhen using a password to generating a private key, you will need that password when instantiating the `PrivateKey` class.\n\n### Loading keys\n\nTo load a key from a file use the `fromFile` static method.\n\n```php\nSpatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey);\nSpatie\\Crypto\\Rsa\\PublicKey::fromFile($pathToPublicKey);\n```\n\nAlternatively, you can also create a key object using a string.\n\n```php\nSpatie\\Crypto\\Rsa\\PrivateKey::fromString($privateKeyString);\nSpatie\\Crypto\\Rsa\\PublicKey::fromString($publicKeyString);\n```\n\nIf the private key is password protected, you need to pass the password as the second argument.\n\n```php\nSpatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey, $password);\nSpatie\\Crypto\\Rsa\\PrivateKey::fromString($privateKeyString, $password);\n```\n\nIf you do not specify the right password, a `Spatie\\Crypto\\Exceptions\\InvalidPrivateKey` exception will be thrown.\n\n### Encrypting a message with a private key, decrypting with the public key\n\nHere's how you can encrypt data using the private key, and how to decrypt it using the public key.\n\n```php\n$data = 'my secret data';\n\n$privateKey = Spatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey);\n$encryptedData = $privateKey-\u003eencrypt($data); // encrypted data contains something unreadable\n\n$publicKey = Spatie\\Crypto\\Rsa\\PublicKey::fromFile($pathToPublicKey);\n$decryptedData = $publicKey-\u003edecrypt($encryptedData); // decrypted data contains 'my secret data'\n```\n\nIf `decrypt` cannot decrypt the given data (maybe a non-matching private key was used to encrypt the data, or maybe tampered with the data), an exception of class `Spatie\\Crypto\\Exceptions\\CouldNotDecryptData` will be thrown.\n\n### Encrypting a message with a public key, decrypting with the private key\n\nHere's how you can encrypt data using the public key, and how to decrypt it using the private key.\n\n```php\n$data = 'my secret data';\n\n$publicKey = Spatie\\Crypto\\Rsa\\PublicKey::fromFile($pathToPublicKey);\n$encryptedData = $publicKey-\u003eencrypt($data); // encrypted data contains something unreadable\n\n$privateKey = Spatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey);\n$decryptedData = $privateKey-\u003edecrypt($encryptedData); // decrypted data contains 'my secret data'\n```\n\nIf `decrypt` cannot decrypt the given data (maybe a non-matching public key was used to encrypt the data, or maybe tampered with the data), an exception of class `Spatie\\Crypto\\Exceptions\\CouldNotDecryptData` will be thrown.\n\n### Determining if the data can be decrypted\n\nBoth the `PublicKey` and `PrivateKey` class have a `canDecrypt` method to determine if given data can be decrypted.\n\n```php\nSpatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey)-\u003ecanDecrypt($data); // returns a boolean;\nSpatie\\Crypto\\Rsa\\PublicKey::fromFile($pathToPublicKey)-\u003ecanDecrypt($data); // returns a boolean;\n```\n\n### Signing and verifying data\n\nThe `PrivateKey` class has a method `sign` to generate a signature for the given data. The `verify` method on the `PublicKey` class can be used to verify if a signature is valid for the given data.\n\nIf `verify` returns `true`, you know for certain that the holder of the private key signed the message, and that it was not tampered with.\n\n```php\n$signature = Spatie\\Crypto\\Rsa\\PrivateKey::fromFile($pathToPrivateKey)-\u003esign('my message'); // returns a string\n\n$publicKey = Spatie\\Crypto\\Rsa\\PublicKey::fromFile($pathToPublicKey);\n\n$publicKey-\u003everify('my message', $signature) // returns true;\n$publicKey-\u003everify('my modified message', $signature) // returns false;\n```\n\n## Alternatives\n\nThis package aims to be very lightweight and easy to use. If you need more features, consider using of one these alternatives:\n\n- [paragonie/halite](https://github.com/paragonie/halite)\n- [vlucas/pikirasa](https://github.com/vlucas/pikirasa)\n- [laminas/crypt](https://docs.laminas.dev/laminas-crypt/)\n- [phpseclib/phpseclib](https://github.com/phpseclib/phpseclib)\n\n## A word on the usage of RSA\n\nAt the time of writing, RSA is secure enough for the use case we've built this package for.\n\nTo know more about why RSA might not be good enough for you, read [this post on public-key encryption at Paragonie.com](https://paragonie.com/blog/2016/12/everything-you-know-about-public-key-encryption-in-php-is-wrong#php-openssl-rsa-bad-default)\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fcrypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fcrypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fcrypto/lists"}