PointyCastle / pointycastle

Moved into the Bouncy Castle project: https://github.com/bcgit/pc-dart
MIT License
270 stars 76 forks source link

PKCS1Encoding(RSAEngine()) throw error in flutter #151

Closed sidealice closed 6 years ago

sidealice commented 6 years ago

I have use import as:

import "package:pointycastle/export.dart" hide Padding; but still got following errors:

Bad state: Reflectable has not been initialized. Please make sure that the first action taken by your program in main is to call initializeReflectable().

sidealice commented 6 years ago

or how to use RSA encryption as RSA/ECB/PKCS1Padding

duncanhoggan commented 6 years ago

@sidealice there is a problem with PKCS1 specifically as the Random generator used for the padding uses reflection and that's a no go for flutter. I've made a small change check this branch for one that works with PKCS. pkcs1_flutter.

Here is a little code sample for PKCS1

String encrypt(String text, RSAPublicKey pubKey) {
    var cipher = PKCS1Encoding(RSAEngine());
    cipher.init(true, PublicKeyParameter<RSAPublicKey>(pubKey));
    Uint8List output1 = cipher.process(utf8.encode(text));
    return  base64Encode(output1);
}

String decrypt(String input, RSAPrivateKey privateKey) {
    var cipher = PKCS1Encoding(RSAEngine());
    cipher.init(false, PrivateKeyParameter<RSAPrivateKey>(privateKey));
    Uint8List output = cipher.process(base64Decode(input));
    return utf8.decode(output);
}

Un-Padded RSA would be the same except when initializing the cipher just use the following in both encrypt and decrypt.

var cipher = RSAEngine();

sidealice commented 6 years ago

thanks a lot!

sreeramu commented 6 years ago

@duncanhoggan @sidealice I am also getting same exception with simple AES example used in flutter, i wan to encrypt and decrypt using AES/CBC/PKCS5Padding is there is any example to do with this library.?

duncanhoggan commented 6 years ago

@sreeramu The issue might be the same as the RSA issue I was having, possibly linked to the same random seed, will have a look and feed back.

stevenroose commented 6 years ago

Look at this probably: https://github.com/PointyCastle/pointycastle/pull/122#issuecomment-392564593

Is anyone here using master? In master I migrated to the new BigInt type, but something doesn't work because of a bug in the implementation in Dart 2.

I really need to review that progress, test the unit tests again and publish to pub. Just really not having time for those things. PRs with at least a start of some progress are super welcome!