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

How use public key which is in string format? #101

Open
oleg-ilechko opened this issue May 21, 2021 · 4 comments
Open

How use public key which is in string format? #101

oleg-ilechko opened this issue May 21, 2021 · 4 comments

Comments

@oleg-ilechko
Copy link

I have this Java code which encrypts data with public key:

publicKeyBytes = Base64.decode(encodedPublicKey, Base64.URL_SAFE);
KeyFactory factoryKey = KeyFactory.getInstance("RSA");
X509EncodedKeySpec encodedKey = new X509EncodedKeySpec(publicKeyBytes);
PublicKey keyPublic = factoryKey.generatePublic(encodedKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec paramSpec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(1, keyPublic, paramSpec);
byte[] abData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.encodeToString(abData, Base64.URL_SAFE + Base64.NO_WRAP + Base64.NO_PADDING);

Is it possible to implement such thing with PointyCastle? My main issue is how should I use my public key which I receive as string to perform encryption with PointyCastle API

@AKushWarrior
Copy link
Contributor

AKushWarrior commented May 22, 2021

Yes, it's possible. You need the dart:convert library:

// at the top of your file
import 'dart:convert';

// in your method or whatever
String encryptFromKey(String encodedPublicKey) {
    var bytes = base64Url.decode(encodedPublicKey);
    // then do the encryption with your bytes... you should be able to figure out the rest.
}

@oleg-ilechko
Copy link
Author

My bytes should be converted to RSAPublicKey object in some way? I see it is used in every example

@oleg-ilechko
Copy link
Author

So I don't understand how to convert this lines into Dart:

KeyFactory factoryKey = KeyFactory.getInstance("RSA");
X509EncodedKeySpec encodedKey = new X509EncodedKeySpec(publicKeyBytes);
PublicKey keyPublic = factoryKey.generatePublic(encodedKey);

@Justinjj10
Copy link

Justinjj10 commented Feb 8, 2022

@oleg-ilechko I am facing the same issue here. Did you get any solution? Appreciate it if you share the code here. I had tried the pointycastle code, but it won't work properly

String encrypt(String plaintext, String publicKey) {

 var pem =   '-----BEGIN RSA PUBLIC KEY-----\n$publickey\n-----END RSA PUBLIC KEY-----';
 var public = CryptoUtils.rsaPublicKeyFromPem(pem);

/// Initalizing Cipher
var cipher = PKCS1Encoding(RSAEngine());
cipher.init(true, PublicKeyParameter<RSAPublicKey>(public));

/// Converting into a [Unit8List] from List<int>
/// Then Encoding into Base64
Uint8List output =
    cipher.process(Uint8List.fromList(utf8.encode(plaintext)));
var base64EncodedText = base64Encode(output);

return base64EncodedText;
 }  

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

3 participants