Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need PHP version of this encoding. #87

Closed
rajeshjnair opened this issue Nov 4, 2019 · 5 comments
Closed

Need PHP version of this encoding. #87

rajeshjnair opened this issue Nov 4, 2019 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@rajeshjnair
Copy link

Can you please share the PHP version of the same encrypt and decrypt function, thus we can achieve the same process API vice-versa. We wrote the function it didn't get any chance to work

@leocavalcante leocavalcante self-assigned this Nov 4, 2019
@leocavalcante leocavalcante added the question Further information is requested label Nov 4, 2019
@leocavalcante
Copy link
Owner

Can you share the function you wrote? Is it giving any errors?

@rajeshjnair
Copy link
Author

`<?php
// DEFINE our cipher
define('AES_256_CBC', 'aes-256-cbc');
define('AES_128_CBC', 'aes-128-cbc');
// Generate a 256-bit encryption key
// This should be stored somewhere instead of recreating it each time
$encryption_key = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));

// Generate an initialization vector
// This MUST be available for decryption as well
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_128_CBC));
// Create some data to encrypt
$data = pkcs7pad("Encrypt me, please!",16);

// Encrypt $data using aes-256-cbc cipher with the given encryption key and
// our initialization vector. The 0 gives us the default options, but can
// be changed to OPENSSL_RAW_DATA or OPENSSL_ZERO_PADDING
$encrypted = openssl_encrypt($data, AES_256_CBC, $encryption_key, 0, $iv);

echo "Key: ".base64_encode($encryption_key).PHP_EOL;

echo "IV: ".base64_encode($iv).PHP_EOL;

echo "Encrypted: $encrypted\n";
?>`

@rajeshjnair
Copy link
Author

`<?php
// DEFINE our cipher
define('AES_256_CBC', 'aes-256-cbc');
define('AES_128_CBC', 'aes-128-cbc');

$encryption_key = 'rKnJQAMdfHa4jl+INHKi+w==';

$iv = 'YxSYgDGcVWzICnHLnCkXaw==';

$encrypted = "scLHHCHeDD4Oe6raVSLaD375c/4jLSAp/aaTgKLJgFaemTwDyKHMJIIFbUcYJfcS";

// These three value generated from the encrypt code.

//print_r($parts);
// $parts[0] = encrypted data
// $parts[1] = base-64 encoded initialization vector
// Don't forget to base64-decode the $iv before feeding it back to
//openssl_decrypt
$decrypted = openssl_decrypt($encrypted, AES_256_CBC, base64_decode($encryption_key), 0, base64_decode($iv));
echo "Decrypted: $decrypted\n";
?>`

@leocavalcante
Copy link
Owner

Take a look at this 4 working and fully interoperable encryption snippets and adapt to your needs:

encrypt

Dart encrypt

import 'package:encrypt/encrypt.dart';

const encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
const encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';

void main(List<String> args) {
 final key = Key.fromBase64(encodedKey);
 final iv = IV.fromBase64(encodedIv);
 final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
 final encrypted = encrypter.encrypt(args[0], iv: iv);

 print(encrypted.base64);
}

PHP decrypt

<?php declare(strict_types=1);

const encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
const encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';

function main(array $args) {
  $key = base64_decode(encodedKey);
  $iv = base64_decode(encodedIv);
  $encrypter = 'aes-256-cbc';
  $decrypted = openssl_decrypt($args[0], $encrypter, $key, 0, $iv);

  echo $decrypted;
};

main(array_slice($argv, 1));

Dart decrypt

import 'package:encrypt/encrypt.dart';

const encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
const encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';

void main(List<String> args) {
  final key = Key.fromBase64(encodedKey);
  final iv = IV.fromBase64(encodedIv);
  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
  final decrypted = encrypter.decrypt(Encrypted.fromBase64(args[0]), iv: iv);

  print(decrypted);
}

PHP encrypt

<?php declare(strict_types=1);

const encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
const encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';

function main(array $args) {
  $key = base64_decode(encodedKey);
  $iv = base64_decode(encodedIv);
  $encrypter = 'aes-256-cbc';
  $encrypted = openssl_encrypt($args[0], $encrypter, $key, 0, $iv);

  echo $encrypted;
};

main(array_slice($argv, 1));

@leocavalcante
Copy link
Owner

Closed due inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants