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 support for Russian characters #153

Open
d1ksim opened this issue Jan 30, 2022 · 1 comment
Open

RSA support for Russian characters #153

d1ksim opened this issue Jan 30, 2022 · 1 comment

Comments

@d1ksim
Copy link

d1ksim commented Jan 30, 2022

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
Copy link
Author

d1ksim commented Jan 30, 2022

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)}");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant