Coder Social home page Coder Social logo

Comments (4)

leocavalcante avatar leocavalcante commented on July 18, 2024 1

Hi @zerocoll9

A just created 4 snippets:

encrypt.php
<?php
$key = file_get_contents('public.pem');
openssl_public_encrypt($argv[1], $encrypted, $key);
echo base64_encode($encrypted);
?>
decrypt.php
<?php
$key = file_get_contents('private.pem');
openssl_private_decrypt(base64_decode($argv[1]), $decrypted, $key);
echo $decrypted;
encrypt.dart
import 'dart:io';
import 'package:encrypt/encrypt.dart';

void main(List<String> args) {
  final publicKey = RSAKeyParser().parse(File('public.pem').readAsStringSync());
  final encrypter = Encrypter(RSA(publicKey: publicKey));
  final encrypted = encrypter.encrypt(args[0]);
  print(encrypted.base64);
}
decrypt.dart
import 'dart:io';
import 'package:encrypt/encrypt.dart';

void main(List<String> args) {
  final privKey = RSAKeyParser().parse(File('private.pem').readAsStringSync());
  final encrypted = Encrypted.fromBase64(args[0]);
  final encrypter = Encrypter(RSA(privateKey: privKey));
  final decrypted = encrypter.decrypt(encrypted);
  print(decrypted);
}

And they are fully interoperable:

Terminal
$ dart .\decrypt.dart (php .\encrypt.php "encrypt on php and decrypt on dart")
encrypt on php and decrypt on dart

$ php .\decrypt.php (dart .\encrypt.dart "encrypt on dart and decrypt on php")
encrypt on dart and decrypt on php

I think you are missing two points here:

  1. Because of padding the resulting binary will never be the same, no matter if is being encrypted on Dart or PHP.
  2. The binary representation should be interchangeable, for example I used base64 on the snippets.

from encrypt.

leocavalcante avatar leocavalcante commented on July 18, 2024

Can you share a snippet with this issue, please?

from encrypt.

zerocoll9 avatar zerocoll9 commented on July 18, 2024

Well, th Dart code was in example, and i used the same key since my keys doesn't worked , i dunno why, i guess it was the bit length. The code was provided here https://github.com/leocavalcante/encrypt/blob/master/example/rsa.dart i just changed the text for my name.
On the other hand, the php code was this:

<?php
 $key = file_get_contents('public.pem');
 $string = "joao paulo";
 openssl_public_encrypt($string,$cripted,$key, ***);
var_dump(unpack('C*',$cripted ));
echo  $cripted;

?>

where *** means that i tried every padding avaible.

from encrypt.

zerocoll9 avatar zerocoll9 commented on July 18, 2024

Yes, sorry, i just compared they bits and assumed that is a problem. It'll save me, thank you very much.

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.