<RSAKeyValue><Modulus>D4ZblrI8NsZgnZOQbHJuEv7Lg0pCOuhwmGIS10rwlJ9Z/hGbes=</Modulus><Exponent>ABCD</Exponent><P>86TWhp553PrrlQ/xdOpGDbGy8/h3/5vg+P==</P><Q>D33zQmGtBnoO8OaQhCwbwqai1w==</Q><DP>i0RwPcLPh+WQKt5L75SR1PQ==</DP><DQ>3vayrPOJ1WMFxPzCpqMaUDWwWsBJ/c0PRwRhvrSw4/TR+6BlEkU/5b17/==</DQ><InverseQ>CbuGb8ZNvjCkSHqvSTJvFpFfqxxQjH09ABxBZx3K7SNvw3+6+LfKeRzoz7==</InverseQ><D>p6etDZ1ghROHdHWAauTUe6zn/RQzm7HE5aCWVOgMqcg9VkFMMZ3H+R1rec=</D></RSAKeyValue>
above you can see the sample format of my publicKey.xml file , i need to convert this into pem format for rsa encryption , mainly need to encrypt the username & password.
import 'dart:convert';
import 'dart:typed_data';
import 'package:pointycastle/export.dart';
import 'package:xml/xml.dart' as xml;
void encryptRSa() {
String publicKeyXML = '''
<RSAKeyValue><Modulus>D4ZblrI8NsZgnZOQbHJuEv7Lg0pCOuhwmGIS10rwlJ9Z/hGbes=</Modulus><Exponent>ABCD</Exponent><P>86TWhp553PrrlQ/xdOpGDbGy8/h3/5vg+P==</P><Q>D33zQmGtBnoO8OaQhCwbwqai1w==</Q><DP>i0RwPcLPh+WQKt5L75SR1PQ==</DP><DQ>3vayrPOJ1WMFxPzCpqMaUDWwWsBJ/c0PRwRhvrSw4/TR+6BlEkU/5b17/==</DQ><InverseQ>CbuGb8ZNvjCkSHqvSTJvFpFfqxxQjH09ABxBZx3K7SNvw3+6+LfKeRzoz7==</InverseQ><D>p6etDZ1ghROHdHWAauTUe6zn/RQzm7HE5aCWVOgMqcg9VkFMMZ3H+R1rec=</D></RSAKeyValue>
''';
var document = xml.XmlDocument.parse(publicKeyXML);
var modulusBase64 = document.findAllElements('Modulus').single.text;
var exponentBase64 = document.findAllElements('Exponent').single.text;
var modulusBytes = Uint8List.fromList(base64.decode(modulusBase64));
var exponentBytes = Uint8List.fromList(base64.decode(exponentBase64));
var modulus = decodeBigIntFromBytes(modulusBytes);
var exponent = decodeBigIntFromBytes(exponentBytes);
var publicKey = RSAPublicKey(modulus, exponent);
var encryptor = OAEPEncoding(RSAEngine())
..init(true, PublicKeyParameter<RSAPublicKey>(publicKey));
String message = 'Hello, World!';
var encrypted = encryptor.process(Uint8List.fromList(utf8.encode(message)));
print('EncryptedOne: ${base64.encode(encrypted)}');
}
BigInt decodeBigIntFromBytes(Uint8List bytes) {
return BigInt.parse(base64.encode(bytes), radix: 16);
}
i am using pointcastle & xml library for encryption , when i run this code i am getting the issue FormatException: Could not parse BigInt
The bug is that in the function
decodeBigIntFromBytes()the data inBigInt.parse()is incorrectly Base64 encoded. Correct is a hex encoding, corresponding to the radix 16 used by you:With this change, the code works, both the import of the public key and the encryption itself.
Additional, but minor: Instead of the expired
text,innerTextshould be used, e.g:If you want to export/import the key in PEM format, the basic_utils package provides suitable functions, e.g.
CryptoUtils.encodeRSAPublicKeyToPem()can be used to export the public key PEM encoded in X.509/SPKI format:Note that the posted key, as already noted in the comments, is indeed a private RSA key. A public key only contains
ModulusandExponentfield (all other fields belong to the private key and must not be disclosed). However, since this is obviously a test key, disclosure is probably not critical.Complete code with valid test key:
Edit: Regarding the question from the comment about importing the private key. The import of the private key is analogous. The elements
Modulus,D(private exponent),P(prime 1) andQ(prime 2) are required. These values are used to instantiateRSAPrivateKey, which can then be applied for decryption.Complete code with valid private key (associated with the public key from the encryption code):