Android™
Android™
Decrypt a JWE with a Private Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded JWE.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkJwe jwe = new CkJwe();
// Load the JWE compact serialization to be decrypted.
String jweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
success = jwe.LoadJwe(jweCompact);
if (success == false) {
Log.i(TAG, jwe.lastErrorText());
return;
}
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the recipient's private key.
CkPrivateKey privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/recipient_privkey.pem");
if (success == false) {
Log.i(TAG, privKey.lastErrorText());
return;
}
// Set the private key used to decrypt recipient 0.
success = jwe.SetPrivateKey(0,privKey);
if (success == false) {
Log.i(TAG, jwe.lastErrorText());
return;
}
String content = jwe.decrypt(0,"utf-8");
if (jwe.get_LastMethodSuccess() == false) {
Log.i(TAG, jwe.lastErrorText());
return;
}
Log.i(TAG, content);
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}