Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

RSA Encrypt with Modulus and Exponent

See more RSA Examples

Demonstrates how to RSA encrypt with a given modulus and exponent.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final rsa = CkRsa();

  // Assuming you already have a base64 modulus and exponent,
  // wrap it in XML like this:
  final modulus = 'qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM=';
  final exponent = 'AQAB';

  final xml = CkXml();
  xml.tag = 'RSAPublicKey';
  xml.newChild2('Modulus', modulus);
  xml.newChild2('Exponent', exponent);

  final pubKey = CkPublicKey();
  try {
    pubKey.loadFromString(xml.getXml());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    rsa.usePublicKey(pubKey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final usePrivateKey = false;
  final plainText = 'message in a bottle';

  rsa.encodingMode = 'base64';
  final encryptedStrBase64 = rsa.encryptStringENC(plainText, usePrivateKey);
  print(encryptedStrBase64);
}