Home » Php » encryption – openssl_enrypt in php alternatives for dart

encryption – openssl_enrypt in php alternatives for dart

Posted by: admin February 25, 2020 Leave a comment

Questions:

is there any alternatives packages for dart to create the exact result like openssl_encrypt ?

currently im using

$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);

there’s this package https://pub.dev/packages/encrypt that use similar parameter , but when comparing the encode result and try to decode it using openssl_decrypt it return always return false

is there any other recommendation package ?

my PHP code

    $output = false;
    $string = "admin";
    $method = "AES-256-CBC";
    $key = 'mykey';
    $iv = 'myiv';
    $key = hash('sha256', $secret_key);

    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    if ( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if( $action == 'decrypt' ) {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

and here’s my dart code

final plainText = 'admin';
final key = Key.fromUtf8('mykey');
final iv = IV.fromLength(16);

final encrypter = Encrypter(AES(key));

final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
How to&Answers: