Java Requires Chilkat v11.0.0+
Chilkat2 Java
RSA Encrypt with Modulus and Exponent
See more RSA Examples
Demonstrates how to RSA encrypt with a given modulus and exponent.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.Xml;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Rsa rsa = new Rsa();
// Assuming you already have a base64 modulus and exponent,
// wrap it in XML like this:
String modulus = "qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM=";
String exponent = "AQAB";
Xml xml = new Xml();
xml.setTag("RSAPublicKey");
xml.newChild2("Modulus", modulus);
xml.newChild2("Exponent", exponent);
PublicKey pubKey = new PublicKey();
try {
pubKey.loadFromString(xml.getXml());
rsa.usePublicKey(pubKey);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
boolean usePrivateKey = false;
String plainText = "message in a bottle";
rsa.setEncodingMode("base64");
String encryptedStrBase64 = rsa.encryptStringENC(plainText, usePrivateKey);
System.out.println(encryptedStrBase64);
}
}