(Java) Use Base64 RSA Key to Encrypt
Loads a Base64 RSA key and uses it to encrypt a string, returning the result in base64.
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkPublicKey pubkey = new CkPublicKey();
boolean success = pubkey.LoadBase64("MIICdgIBADA ... A9PXLk+j5A==");
if (success != true) {
System.out.println(pubkey.lastErrorText());
return;
}
CkRsa rsa = new CkRsa();
success = rsa.ImportPublicKeyObj(pubkey);
if (success != true) {
System.out.println(rsa.lastErrorText());
return;
}
rsa.put_EncodingMode("base64");
String encryptedStr = rsa.encryptStringENC("12345678",false);
System.out.println(encryptedStr);
}
}
|