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

RSA encrypted with dart doesn't match with PHP #42

Closed
zerocoll9 opened this issue May 24, 2019 · 4 comments
Closed

RSA encrypted with dart doesn't match with PHP #42

zerocoll9 opened this issue May 24, 2019 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@zerocoll9
Copy link

I tried to encrypt a string in dart with Encrypt, but, when i compared it with that openssl on php gave me, it didn't match. I tried every padding avaible.

@leocavalcante
Copy link
Owner

Can you share a snippet with this issue, please?

@zerocoll9
Copy link
Author

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.

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

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.

@zerocoll9
Copy link
Author

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

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