jybtx/rsa-crypt-aes

This is an extension of RSA and AES encryption and decryption for API interface, which is suitable for encryption and decryption with high requirements for encrypted data

dev-master 2025-03-07 09:11 UTC

This package is auto-updated.

Last update: 2025-04-07 09:18:42 UTC


README

A developing toolkit for RSA and AES encryption and decryption under test

Installation

Composer

Execute the following command to get the latest version of the package:

composer require jybtx/rsa-crypt-aes

Laravel

>= laravel5.5

ServiceProvider will be attached automatically

Other

In your config/app.php add Jybtx\RsaCryptAes\Providers\CryptServiceProvider::class to the end of the providers array:

'providers' => [
    ...
    Jybtx\RsaCryptAes\Providers\CryptServiceProvider::class,
],
'aliases'  => [
    ...
    "RsaCryptAes" => Jybtx\RsaCryptAes\Faceds\RsaCryptAesFaced::class,
]

Generate secret key

I have included a helper command to generate a key for you:

php artisan jybtx:secret

This will update your .env file with something like HEX_IV=foobar

It is the key that will be used to sign your tokens. How that happens exactly will depend on the algorithm that you choose to use.

Publish Configuration

php artisan vendor:publish --provider "Jybtx\RsaCryptAes\Providers\CryptServiceProvider"

OR

php artisan vendor:publish --tag=crypt

Usage

get a public key

use RsaCryptAes;
$public = RsaCryptAes::getThePublicKey();

decrypt Random String

$random = RsaCryptAes::decryptRandomString($obj,$md5PublicKey);

decrypt Encrypted Data

$data = RsaCryptAes::getDecryptEncryptedData($random,$pubKeyMd5,$data);
if ( $data == FALSE ) return respone()->json(['status'=>100,'message'=>'Public key invalidation, retrieve']);

return data for api

return RsaCryptAes::getReturnEncryptDataForApi($status,$msg,$data='');
   // Return data format
return [
    'status' => $status,
    'msg'    => $msg,
    'data'   => $data,
    'sign'   => $encrypt_aes_key,
];

encrypt Data

$restart = RsaCryptAes::getEncryptedDataAndRandomStrings($status,$msg,$data);
return [
    'status'       => $status,
    'msg'          => $msg,
    'data'         => $string,
    'random'       => $encrypt_aes_key,
    'md5public'    => md5($pubKey)
];

Signature operation

$result = RsaCryptAes::getSign(string $attributes,$private_key);

return  (string) Signature;

Signature verification

$result = RsaCryptAes::getVerify(string $attributes, string $sign, $publicKey);

return boolean true | false;

Last

Tips:The encryption and decryption of RSA and AES still need to be improved, but it does not affect the normal use

License

MIT