bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
241 stars 125 forks source link

RSA support for Russian characters #153

Open d1ksim opened 2 years ago

d1ksim commented 2 years ago

Hello everyone, I'm using this code from the example, and there is a problem that Russian characters are displayed like this: "Привет - @825B".

var keyParams = RSAKeyGeneratorParameters(BigInt.from(65537), 2048, 5);

  var secureRandom = FortunaRandom();
  var random = Random.secure();

  List<int> seeds = [];
  for (int i = 0; i < 32; i++) {
    seeds.add(random.nextInt(255));
  }

  secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));

  var rngParams = ParametersWithRandom(keyParams, secureRandom);
  var k = RSAKeyGenerator();
  k.init(rngParams);

  dynamic keyPair = k.generateKeyPair();

  print(RsaKeyHelper().encodePublicKeyToPem(keyPair.publicKey));
  print(RsaKeyHelper().encodePrivateKeyToPem(keyPair.privateKey));

  AsymmetricKeyParameter<RSAPublicKey> keyParametersPublic =
      PublicKeyParameter(keyPair.publicKey);
  var cipher = RSAEngine()..init(true, keyParametersPublic);

  var cipherText = cipher.process(Uint8List.fromList("hello".codeUnits));
  print(cipherText);
  print("Encrypted: ${String.fromCharCodes(cipherText)}");

  AsymmetricKeyParameter<RSAPrivateKey> keyParametersPrivate =
      PrivateKeyParameter(keyPair.privateKey);

  cipher.init(false, keyParametersPrivate);
  var decrypted = cipher.process(cipherText);
  print("Decrypted: ${String.fromCharCodes(decrypted)}");

I've been racking my head for several hours, does anyone have a ready-made implementation?

d1ksim commented 2 years ago

Fixed.

  final chars = "Привет! Hello! 🤘🏻😂😌😄🤣✅".codeUnits;
  // ...........................................................................

  var keyParams = RSAKeyGeneratorParameters(BigInt.from(65537), 2048, 5);

  var secureRandom = FortunaRandom();
  var random = Random.secure();

  List<int> seeds = [];
  for (int i = 0; i < 32; i++) {
    seeds.add(random.nextInt(255));
  }

  secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));

  var rngParams = ParametersWithRandom(keyParams, secureRandom);
  var k = RSAKeyGenerator();
  k.init(rngParams);

  dynamic keyPair = k.generateKeyPair();

  print(RsaKeyHelper().encodePublicKeyToPem(keyPair.publicKey));
  print(RsaKeyHelper().encodePrivateKeyToPem(keyPair.privateKey));

  AsymmetricKeyParameter<RSAPublicKey> keyParametersPublic =
      PublicKeyParameter(keyPair.publicKey);
  var cipher = RSAEngine()..init(true, keyParametersPublic);

  var cipherText = cipher.process(Uint8List.fromList([
    ...chars.map<List<int>>((ch) => [ch & 0xff, ch >> 8]).expand((i) => i)
  ]));
  print("Encrypted: ${String.fromCharCodes(cipherText)}");

  AsymmetricKeyParameter<RSAPrivateKey> keyParametersPrivate =
      PrivateKeyParameter(keyPair.privateKey);

  cipher.init(false, keyParametersPrivate);
  var decrypted = cipher.process(cipherText);
  final original = [
    for (var i = 0; i < decrypted.length; i += 2) decrypted[i] | decrypted[i + 1] << 8
  ];
  print(decrypted);
  print("Decrypted: ${String.fromCharCodes(original)}");