PHP Classes

Cryptonita: Encrypt and decrypt data with symmetric encryption

Recommend this page to a friend!
  Info   Example   Videos   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-10-26 (11 months ago) RSS 2.0 feedNot enough user ratingsTotal: 54 All time: 10,613 This week: 46Up
Version License PHP version Categories
cryptonita 1.0.0MIT/X Consortium ...7.4Cryptography, PHP 7
Description 

Author

Encrypt and decrypt data with symmetric encryption.

It can encrypt a string of data using a given cryptography method, information vector, and hash. It returns a base64 encoded string of the encrypted data.

The class can also decrypt a base64 encoded version of the encrypted data using the same cryptography method, information vector, and hash.

The cryptography method, information vector, and hash are parameters that are read by the class from a configuration script.

Innovation Award
PHP Programming Innovation award nominee
October 2023
Number 6
Some applications must encrypt and decrypt data to protect it before sending it to an external system.

PHP supports several ways of encrypting data. The most robust methods include multiple additional parameters defining encryption methods, information vectors, and hashes.

This class can encrypt and decrypt data using the OpenSSL PHP extension.

Since this extension requires several additional parameters besides the actual data that it needs to encrypt and decrypt, the package can simplify the setting of those parameters by reading the respective values for an external configuration script.

This way, this package can simplify the encryption and data decryption process.

Manuel Lemos
Picture of Rodrigo Faustino
  Performance   Level  
Name: Rodrigo Faustino <contact>
Classes: 27 packages by
Country: Brazil Brazil
Age: 41
All time rank: 2508173 in Brazil Brazil
Week rank: 3 Up1 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 17x

Winner: 2x

Instructions

Encrypt Method ($this->encrypt_method):

The type of encryption method (e.g., "AES-256-CBC").

The exact method is defined in the external configuration file as METHOD.

Initialization Vector ($this->secret_iv):

An Initialization Vector (IV) is crucial for block cipher modes. In this mode, the IV is hashed to generate a derived value, and then the derived IV is trimmed to 16 bytes.

The exact value for the IV is defined in the external configuration file as SECRETIV.

Hash Algorithm ($this->hash):

The hashing algorithm derives the encryption key and IV from the secret IV.

The exact hash algorithm is defined in the external configuration file as HASH.

Encryption and Decryption:

The hidden function checks for an action parameter:

If $action is 1, it encrypts the input string using the specified encryption method, key, and IV.

The encrypted data is then encoded with base64 to provide a string easily stored or transmitted without special encoding.

If $action is 2, it first decodes the input string using base64, then decrypts the data using the specified encryption method, key, and IV.

Usage of openssl_encrypt and openssl_decrypt Functions:

These functions are from PHP's OpenSSL extension, which provides a way to use the OpenSSL library for data encryption.

Example

<?php
require __DIR__ ."/vendor/autoload.php";

use
App\Cryptonita\Crypto;

$cripto = new Crypto();
$nome="XXXXXXX faustino";
$email="XYZZZ@gmail.com";
$likedin="https://www.linkedin.com/in/XXXXXXXX/";
$site="https:XXXXXXXX.com";
$data=[$nome,$email,$likedin,$site];
$criptografado=[];
// resultado criptografado hidden(string, 1=criptgrafa)
foreach ($data as $key => $value) {
   
$criptografado []= $cripto->hidden($value);
}
echo
"-------Resultado Criptografia---------------------";
var_dump($criptografado);
echo
"--------------------------------------------------";
// resultado Descriptografado hidden(string, 2=descriptgrafa)
$descriptografado=[];
foreach (
$criptografado as $key => $value) {
   
$descriptografado[]= $cripto->show($value);
}
echo
"------Resultado Descriptografia-------------------";
var_dump($descriptografado);
echo
"--------------------------------------------------";


Details

CryptoPHP Documentação

Indice

Descrição

A classe Crypto é responsável por encriptar e descriptar informações usando a criptografia OpenSSL. (criptografia simétrica) como o proposito exclusivo de esconder as informações no banco de dados e usar a mesma chave para mostrar os dados na aplicação

Início Rápido

  • clone o repositório (git clone https://github.com/faustinopsy/criptonita)
  • composer install

Pré-requisitos

  • Composer e autoloader configurado.
  • Arquivo `config.php` com as constantes `METHOD`, `SECRETIV` e `HASH` definidas.

Instalação

require __DIR__ ."/vendor/autoload.php";
use App\Cryptonita\Crypto;

Uso

  • Criptografar Informações
  • Para criptografar informações, instancie a classe Crypto e utilize o método hidden passando o valor 1 como segundo argumento:
    $cripto = new Crypto();
    $nome = "XXXXXXX faustino";
    $criptografado = $cripto->hidden($nome, 1);
    
  • Descriptografar Informações
  • Para descriptografar, utilize o método hidden passando o valor 2 como segundo argumento:
    $nomeDescriptografado = $cripto->hidden($criptografado, 2);
    
    ### Exemplo Completo
    
    require __DIR__ ."/vendor/autoload.php";

use App\Cryptonita\Crypto;

$cripto = new Crypto(); $nome="XXXXXXX faustino"; $email="XYZZZ@gmail.com"; $likedin="https://www.linkedin.com/in/XXXXXXXX/"; $site="https:XXXXXXXX.com"; $data=[$nome,$email,$likedin,$site]; $criptografado=[]; // resultado criptografado hidden(string) foreach ($data as $key => $value) {

$criptografado []= $cripto->hidden($value);

} echo "-------Resultado Criptografia---------------------"; var_dump($criptografado); echo "--------------------------------------------------"; // resultado Descriptografado show(string) $descriptografado=[]; foreach ($criptografado as $key => $value) {

$descriptografado[]= $cripto->show($value);

} echo "------Resultado Descriptografia-------------------"; var_dump($descriptografado); echo "--------------------------------------------------";

### Contribuindo
- Contribuições são bem-vindas! Sinta-se à vontade para abrir uma issue ou um pull request.

### Licença
O Cripto é licenciado sob a licença MIT. 

Videos (1)  
  • Criptografia Avançada em PHP: Esconda os dados das pessoas!
  Files folder image Files (7)  
File Role Description
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file show.php Example Example script

  Files folder image Files (7)  /  src  
File Role Description
Files folder imageCryptonita (2 files)

  Files folder image Files (7)  /  src  /  Cryptonita  
File Role Description
  Accessible without login Plain text file config.php Appl. Configuration script
  Plain text file Crypto.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:0
All time:10,613
This week:46Up
User Comments (1)
This package does NOT encrypt data.
11 months ago (John Conde)
12%Star