Comments (19)
Bro, checkout
encryptBytes
anddecryptBytes
: 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.
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.
can anyone help me?
@yapcwed @shutup @Jethro87 @benittopremkumar @hasimyerlikaya
from encrypt.
Thank you so much, Leo, it's working!
from encrypt.
Can you share how to encrypt data in php ?
from encrypt.
@joumduk see if this helps #87 (comment)
from encrypt.
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.
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.
how can i encrypt/decrypt my pdf file?
from encrypt.
@sandeepsoni25 load the file as bytes and use encrypt bytes.
from encrypt.
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.
@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
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.
Bro, checkout encryptBytes
and decryptBytes
: https://pub.dev/documentation/encrypt/latest/encrypt/Encrypter/encryptBytes.html ;)
from encrypt.
@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.
@leocavalcante @HimanshuSaikia
Kudos to both you guys. This thread has really been helpful and save me after quite some headache.
from encrypt.
@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.
Hii @HimanshuSaikia
can you please share a complete code it helps me a lot
from encrypt.
hi, can anyone explain that how can encode in flutter and decode in php?
from encrypt.
Related Issues (20)
- AES 128 CFB decryption
- I can't decrypt the text in 5.0.3 😕 HOT 11
- Output double in size, AES in CTR
- After RSA public key encryption, I am unable to decrypt it. It shows an error message: 'Error: Expected a value of type 'ASN1Integer', but got one of type 'ASN1Sequence'.
- Getting Private Key Issue HOT 1
- Invalid argument(s): Initialization vector must be the same length as block size
- You can add an onProgress
- Invalid argument(s): Invalid or corrupted pad block HOT 6
- Different encrypted output
- Encryption between platforms
- Web release Invalid or corrupted pad block HOT 3
- '(x >= 0) && (x <= _MASK_32)': is not true
- Exception has occurred. ArgumentError (Invalid argument(s): Invalid or corrupted pad block) HOT 11
- About Privacy Manifest HOT 1
- RSAKeyParser is not defined in poiintycastle
- [v5.0.3][Web] decrypt incorrect HOT 6
- crypto dependency issue
- Insecure Random on ios
- Invalid argument(s): Invalid or corrupted pad block HOT 7
- (mistake) `Key.stretch` should be a factory constructor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from encrypt.