AKushWarrior / steel_crypt

A collection of high-level API's exposing PointyCastle to perform hashing and encrypting in popular/secure algorithms.
https://pub.dev/packages/steel_crypt
Mozilla Public License 2.0
40 stars 10 forks source link

When encrypting on windows and decrypting on linux, something went wrong #30

Closed AugustToko closed 3 years ago

AugustToko commented 4 years ago

I do n’t know why this happens, can someone help me?

On Win

  var publicKeyFile = File('D:\\Web\\api_test\\files\\rsa_public_key.pem');

  var publicStr = await publicKeyFile.readAsString();
  publicKey = rsaC.parseKeyFromString(publicStr);

  var eData = rsaC.encrypt('123456789', publicKey);
  var result = await Dio().post(url, data: {'data': eData});
  print(result);

On Linux

    final RsaCrypt rsaC = RsaCrypt();
    RSAPrivateKey rsaPrivateKek;

    final pemFile =
        File('/var/www/lingyun_server/keys/rsa_private_key_pkcs8.pem');
    final str = await pemFile.readAsString();
    rsaPrivateKeyWk = rsaC.parseKeyFromString(str)
    // data is passed by post (application/json) request
    print(rsaC.decrypt(data.eData, rsaPrivateKeyWk));
 
[SEVERE] aqueduct: POST /toolspack/sead 7ms 500 {user-agent : Dart/2.7 (dart:io)\ncontent-type : application/json; charset=utf-8\nconnection : close\naccept-encoding : gzip\ncontent-length : 201\nhost : 127.0.0.1:8888\n}  Invalid argument(s): data wrong #0      OAEPEncoding._decodeBlock (package:steel_crypt/PointyCastleN/asymmetric/oaep.dart:232:7)
#1      OAEPEncoding.processBlock (package:steel_crypt/PointyCastleN/asymmetric/oaep.dart:107:14)
#2      BaseAsymmetricBlockCipher.process (package:steel_crypt/PointyCastleN/src/impl/base_asymmetric_block_cipher.dart:21:15)
#3      RsaCrypt.decrypt (package:steel_crypt/src/rsa.dart:148:27)
#4      ToolsPackController.getEnterAppData (package:lingyun_server/controller/tools_pack_controller.dart:106:16)
#5      ToolsPackController.httpPost (package:lingyun_server/controller/tools_pack_controller.dart:68:14)
#6      _InstanceMirror._invoke (dart:mirrors-patch/mirrors_impl.dart:327:37)
#7      _InstanceMirror.invoke (dart:mirrors-patch/mirrors_impl.dart:323:25)
#8      BoundOperation.invoke (package:aqueduct/src/http/resource_controller_internal/controller.dart:24:10)
#9      ResourceController._process (package:aqueduct/src/http/resource_controller.dart:371:36)

#10     ResourceController.handle (package:aqueduct/src/http/resource_controller.dart:138:14)

#11     Controller.receive (package:aqueduct/src/http/controller.dart:174:30)
#12     _ControllerRecycler.receive (package:aqueduct/src/http/controller.dart:402:17)
#13     Controller.receive (package:aqueduct/src/http/controller.dart:206:28)

#14     Router.receive (package:aqueduct/src/http/router.dart:139:18)
#15     _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
#16     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#17     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
#18     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:134:11)
#19     _MapStream._handleData (dart:async/stream_pipe.dart:234:10)
#20     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)
#21     _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
#22     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#23     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
#24     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
#25     _StreamController._add (dart:async/stream_controller.dart:642:7)
#26     _StreamController.add (dart:async/stream_controller.dart:588:5)
#27     _HttpServer._handleRequest (dart:_http/http_impl.dart:2828:19)
#28     new _HttpConnection. (dart:_http/http_impl.dart:2586:19)
#29     _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
#30     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#31     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
#32     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
#33     _StreamController._add (dart:async/stream_controller.dart:642:7)
#34     _StreamController.add (dart:async/stream_controller.dart:588:5)
#35     _HttpParser._headersEnd (dart:_http/http_parser.dart:388:17)
#36     _HttpParser._doParse (dart:_http/http_parser.dart:703:15)
#37     _HttpParser._parse (dart:_http/http_parser.dart:320:7)
#38     _HttpParser._onData (dart:_http/http_parser.dart:812:5)
#39     _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
#40     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#41     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
#42     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
#43     _StreamController._add (dart:async/stream_controller.dart:642:7)
#44     _StreamController.add (dart:async/stream_controller.dart:588:5)
#45     _Socket._onData (dart:io-patch/socket_patch.dart:1831:41)
#46     _RootZone.runUnaryGuarded (dart:async/zone.dart:1316:10)
#47     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
#48     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
#49     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
#50     _StreamController._add (dart:async/stream_controller.dart:642:7)
#51     _StreamController.add (dart:async/stream_controller.dart:588:5)
#52     new _RawSocket. (dart:io-patch/socket_patch.dart:1379:33)
#53     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:899:14)
#54     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#55     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#56     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#57     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:175:5)

AKushWarrior commented 4 years ago

I'm on mobile rn, will take a look later.

AKushWarrior commented 4 years ago

Fist comment: instead of using parseKeyFromString on the stringified file, you should use parseKeyFromFile and pass the filename.

AKushWarrior commented 4 years ago

Can you try that and see if it changes anything?

AugustToko commented 4 years ago

Sorry for not being able to reply in time. Even if I use parseKeyFromFile, the effect is the same. After testing, encryption and decryption are normal on the same platform. In addition, when I tried parseKeyFromString, the newline character (\n) was unified, but it didn't work. By the way, now I use encrypt: ^ 3.3.1 and it works well.

AKushWarrior commented 4 years ago

Okay, thanks for the info. Will take a look!

AKushWarrior commented 4 years ago

This is very strange, but I don't have a Windows machine or a Linux machine. I'll have to shelve this for now, sorry.

AKushWarrior commented 3 years ago

Since RSA is being deprecated (see README), this is irrelevant.