Coder Social home page Coder Social logo

Comments (19)

HimanshuSaikia avatar HimanshuSaikia commented on July 18, 2024 5

Bro, checkout encryptBytes and decryptBytes: https://pub.dev/documentation/encrypt/latest/encrypt/Encrypter/encryptBytes.html ;)

BRO, YOU ARE AMAZING. Thank you so much for the much needed support. I owe you.
Finally I did the php encryption and dart decryption successfully for a video file.
There was a slight trick though, and that was, openssl_encrypt() does a base64 encoding to the final output which is then saved to the file. So, in dart, before passing to decryptBytes we need to do a base64 decoding first for the string in the encrypted file. I am providing my code open here. Thanks once again Leo Cavalcante.

PHP Encryption code
I am using the following key and iv in php.
$key='FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
$iv='DB4gHxkcBQkKCxoRGBkaFA==';

The encryption& decryption functions:

function encryptFile($encKey, $encIV, $inPath, $outPath) {
        $sourceFile=file_get_contents($inPath);
        $key = base64_decode($encKey);
        $iv = base64_decode($encIV);
        $path_parts = pathinfo($inPath);
        $fileName=$path_parts['filename'];
        $outFile=$outPath.$fileName.'.himu';
        $encrypter = 'aes-256-cbc';
        $encryptedString = openssl_encrypt($sourceFile, $encrypter, $key, 0, $iv);
        if(file_put_contents($outFile, $encryptedString)!= false) return 1;
        else return 0;
    }

PHP Decryption

function decryptFile($encKey, $encIV, $inPath, $outPath) {
        $encryptedString=file_get_contents($inPath);
        $key = base64_decode($encKey);
        $iv = base64_decode($encIV);
	$path_parts = pathinfo($inPath);
        $fileName=$path_parts['filename'];
        $outFile=$outPath.$fileName.'.mp4';
        $encrypter = 'aes-256-cbc';
        $decrypted = openssl_decrypt($encryptedString, $encrypter, $key, 0, $iv);
        if(file_put_contents($outFile, $decrypted)!= false) return 1;
        else return 0;
    }

DART Decryption
import 'package:encrypt/encrypt.dart' as enc;
import 'package:path/path.dart' as p;
import 'dart:async';
import 'dart:io';

Future<String> decryptFile(filePath) async{  //'filePath' contains the php encrypted video file.
    var encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
    var encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';
    var encryptedBase64EncodedString = new File(filePath).readAsStringSync();
    var decoded = base64.decode(encryptedBase64EncodedString);
    final key1 = enc.Key.fromBase64(encodedKey);
    final iv = enc.IV.fromBase64(encodedIv);
    final encrypter = enc.Encrypter(enc.AES(key1, mode: enc.AESMode.cbc));
    final decrypted = encrypter.decryptBytes(enc.Encrypted(decoded), iv: iv);
    final filename = '${p.basenameWithoutExtension(filePath)}.mp4';
    final directoryName=p.dirname(filePath);
    final newFilePath=p.join(directoryName, filename);
    var newFile = new File(newFilePath);
    await newFile.writeAsBytes(decrypted);
    return newFilePath;
  }

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024 4

Hi @mmojbo
Is your decrypted string {"opr":1}?
Try:

final key = Key.fromUtf8('&5P483usHDhA32JUj55@CdcrbupasysB');
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
print(encrypter.decrypt64('k2EfYozdaJudTbKjPj2WmQ==', iv: IV.fromLength(16)));

from encrypt.

mmojbo avatar mmojbo commented on July 18, 2024

can anyone help me?
@yapcwed @shutup @Jethro87 @benittopremkumar @hasimyerlikaya

from encrypt.

mmojbo avatar mmojbo commented on July 18, 2024

Thank you so much, Leo, it's working!

from encrypt.

joumduk avatar joumduk commented on July 18, 2024

Can you share how to encrypt data in php ?

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

@joumduk see if this helps #87 (comment)

from encrypt.

HimanshuSaikia avatar HimanshuSaikia commented on July 18, 2024

Please help/guide me to decrypt an encrypted mp4 file in dart. The file was encrypted using PHP OpenSSL encrypt with AES-256-CBC mode.

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

Hi @HimanshuSaikia, it will be like in #87 (comment) but instead of using a argument from the command line input, you should use Dart's IO File reader.

from encrypt.

sandeepsoni25 avatar sandeepsoni25 commented on July 18, 2024

how can i encrypt/decrypt my pdf file?

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

@sandeepsoni25 load the file as bytes and use encrypt bytes.

from encrypt.

HimanshuSaikia avatar HimanshuSaikia commented on July 18, 2024

Hi @HimanshuSaikia, it will be like in #87 (comment) but instead of using a argument from the command line input, you should use Dart's IO File reader.

Thanks a lot for your instant reply. So, according to your logic, I successfully encrypted a file in php, decrypted the file in php too. Although text decryption is working in dart, But when i try to decrypt a video file in dart, it's not working. The output file is not recognised as any format by the player. also the file size is larger than the original as well as the encrypted file.
The source mp4 file was 253kb
The encrypted file was 337kb
The dart decrypted mp4 file was 448 kb and not read by the player
PHP decryption is working fine and generates a 253kb mp4 file.
Simple text decryption in Dart and output to a text file is working fine.

I kept same Key as yours
I kept same IV as yours

My decryption code is:
//***********************
import 'package:encrypt/encrypt.dart' as enc;
import 'package:path/path.dart' as p;
import 'dart:async';
import 'dart:io';

//**********************

Future<String> decryptFile(filePath) async{  //'filePath' contains the php encrypted video file.
    var encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXHgAFDgY=';
    var encodedIv = 'DB4gHxkcBQkKCxoRGBkaFA==';
    var text = new File(filePath).readAsStringSync();
    final key1 = enc.Key.fromBase64(encodedKey);
    final iv = enc.IV.fromBase64(encodedIv);
    final encrypter = enc.Encrypter(enc.AES(key1, mode: enc.AESMode.cbc));
    final decrypted = encrypter.decrypt(enc.Encrypted.fromBase64(text), iv: iv);
    final filename = '${p.basenameWithoutExtension(filePath)}.mp4';
    final directoryName=p.dirname(filePath);
    final newFilePath=p.join(directoryName, filename);
    List<int> bytes = utf8.encode(decrypted);print(bytes);
    var newFile = new File(newFilePath);
    await newFile.writeAsBytes(bytes);
    return newFilePath;
  }

My working file encryption and decryption code in PHP is:

function encryptFile($encKey, $encIV, $inPath, $outPath) {
        $text=file_get_contents($inPath);
        $key = base64_decode($encKey);
        $iv = base64_decode($encIV);
        $path_parts = pathinfo($inPath);
        $fileName=$path_parts['filename'];
        $outFile=$outPath.$fileName.'.himu';
        $encrypter = 'aes-256-cbc';
        $encrypted = openssl_encrypt($text, $encrypter, $key, 0, $iv);
        if(file_put_contents($outFile,$encrypted)!= false) return 1;
        else return 0;
    }
function decryptFile($encKey, $encIV, $inPath, $outPath) {
        $text=file_get_contents($inPath);
        $key = base64_decode($encKey);
        $iv = base64_decode($encIV);
	$path_parts = pathinfo($inPath);
        $fileName=$path_parts['filename'];
        $outFile=$outPath.$fileName.'.mp4';
        $encrypter = 'aes-256-cbc';
        $decrypted = openssl_decrypt($text, $encrypter, $key, 0, $iv);
        if(file_put_contents($outFile,$decrypted)!= false) return 1;
        else return 0;
    }

Can you Please help me out of this problem. I am badly stuck

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

@HimanshuSaikia
Read the file as binary and do not pass its content through base 64 decoding (i.e.: enc.Encrypted.fromBase64(text)) it's not saved as base 64, its plain binary.

from encrypt.

HimanshuSaikia avatar HimanshuSaikia commented on July 18, 2024

@HimanshuSaikia
Read the file as binary and do not pass its content through base 64 decoding (i.e.: enc.Encrypted.fromBase64(text)) it's not saved as base 64, its plain binary.

Bro, But decrypt function encrypter.decrypt() returns a string!!
Also, the function is not running on passing a byte array.

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

Bro, checkout encryptBytes and decryptBytes: https://pub.dev/documentation/encrypt/latest/encrypt/Encrypter/encryptBytes.html ;)

from encrypt.

armandohackcode avatar armandohackcode commented on July 18, 2024

@HimanshuSaikia thanks, it worked very well for me, I used it as a base to make my function to decrypt a text based on the same encryption logic done with php and Lumen

 import 'dart:convert';
 import 'package:encrypt/encrypt.dart' as enc;
 import 'dart:async';
 import 'package:php_serializer/php_serializer.dart';

 Future<String> decryptInfo(String data) async {
       var encodedKey = 'FCAcEA0HBAoRGyALBQIeCAcaDxYWEQQPBxcXH****** example';
       var decoded = base64.decode(data);
      var payload = json.decode(String.fromCharCodes(decoded));
      String encodedIv = payload["iv"]?? "";
      String value = payload["value"] ?? "";
      print(decoded);
      print(payload);
      print (encodedIv);
      final key1 = enc.Key.fromBase64(encodedKey);
      final iv = enc.IV.fromBase64(encodedIv);
     final encrypter = enc.Encrypter(enc.AES(key1, mode: enc.AESMode.cbc));
     final decrypted = encrypter.decrypt(enc.Encrypted.fromBase64(value), iv: iv);
     print(phpDeserialize(decrypted));
   return decrypted;
  } 

from encrypt.

udoy-touhid avatar udoy-touhid commented on July 18, 2024

@leocavalcante @HimanshuSaikia
Kudos to both you guys. This thread has really been helpful and save me after quite some headache.

from encrypt.

udoy-touhid avatar udoy-touhid commented on July 18, 2024

@HimanshuSaikia I'm just confused in one line.

$encrypted = openssl_encrypt($text, $encrypter, $key, 0, $iv);

if $encrypted contains only binary data, why we need to base64 decode it first on dart?

encryptedBase64EncodedString = new File(filePath).readAsStringSync();
var decoded = base64.decode(encryptedBase64EncodedString);

Could you please clarify?

from encrypt.

Vidwath23 avatar Vidwath23 commented on July 18, 2024

Hii @HimanshuSaikia

can you please share a complete code it helps me a lot

from encrypt.

mohamaddoost avatar mohamaddoost commented on July 18, 2024

hi, can anyone explain that how can encode in flutter and decode in php?

from encrypt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.