# OpenPGP API library
The OpenPGP API provides methods to execute OpenPGP operations, such as sign, encrypt, decrypt, verify, and more without user interaction from background threads. This is done by connecting your client application to a remote service provided by [OpenKeychain](http://www.openkeychain.org) or other OpenPGP providers.
### News
#### Version 10
* Retrieve whole public key via ACTION_GET_KEY
[Full changelog here…](https://github.com/open-keychain/openpgp-api/blob/master/CHANGELOG.md)
### License
While OpenKeychain itself is GPLv3+, the API library is licensed under Apache License v2.
Thus, you are allowed to also use it in closed source applications as long as you respect the [Apache License v2](https://github.com/open-keychain/openpgp-api/blob/master/LICENSE).
### Add the API library to your project
Add this to your build.gradle:
```gradle
repositories {
jcenter()
}
dependencies {
compile 'org.sufficientlysecure:openpgp-api:10.0'
}
```
### Full example
A full working example is available in the [example project](https://github.com/open-keychain/openpgp-api/blob/master/example). The [``OpenPgpApiActivity.java``](https://github.com/open-keychain/openpgp-api/blob/master/example/src/main/java/org/openintents/openpgp/example/OpenPgpApiActivity.java) contains most relevant sourcecode.
### API
[OpenPgpApi](https://github.com/open-keychain/openpgp-api/blob/master/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java) contains all possible Intents and available extras.
### Short tutorial
**This tutorial only covers the basics, please consult the full example for a complete overview over all methods**
The API is **not** designed around ``Intents`` which are started via ``startActivityForResult``. These Intent actions typically start an activity for user interaction, so they are not suitable for background tasks. Most API design decisions are explained at [the bottom of this wiki page](https://github.com/open-keychain/open-keychain/wiki/OpenPGP-API#internal-design-decisions).
We will go through the basic steps to understand how this API works, following this (greatly simplified) sequence diagram:
![](https://github.com/open-keychain/open-keychain/raw/master/Resources/docs/openpgp_api_1.jpg)
In this diagram the client app is depicted on the left side, the OpenPGP provider (in this case OpenKeychain) is depicted on the right.
The remote service is defined via the [AIDL](http://developer.android.com/guide/components/aidl.html) file [``IOpenPgpService``](https://github.com/open-keychain/openpgp-api/blob/master/openpgp-api/src/main/aidl/org/openintents/openpgp/IOpenPgpService.aidl).
It contains only one exposed method which can be invoked remotely:
```java
interface IOpenPgpService {
Intent execute(in Intent data, in ParcelFileDescriptor input, in ParcelFileDescriptor output);
}
```
The interaction between the apps is done by binding from your client app to the remote service of OpenKeychain.
``OpenPgpServiceConnection`` is a helper class from the library to ease this step:
```java
OpenPgpServiceConnection mServiceConnection;
public void onCreate(Bundle savedInstance) {
[...]
mServiceConnection = new OpenPgpServiceConnection(this, "org.sufficientlysecure.keychain");
mServiceConnection.bindToService();
}
public void onDestroy() {
[...]
if (mServiceConnection != null) {
mServiceConnection.unbindFromService();
}
}
```
Following the sequence diagram, these steps are executed:
1. Define an ``Intent`` containing the actual PGP instructions which should be done, e.g.
```java
Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_ENCRYPT);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{"dominik@dominikschuermann.de"});
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
```
Define an ``InputStream`` currently holding the plaintext, and an ``OutputStream`` where you want the ciphertext to be written by OpenKeychain's remote service:
```java
InputStream is = new ByteArrayInputStream("Hello world!".getBytes("UTF-8"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
```
Using a helper class from the library, ``is`` and ``os`` are passed via ``ParcelFileDescriptors`` as ``input`` and ``output`` together with ``Intent data``, as depicted in the sequence diagram, from the client to the remote service.
Programmatically, this can be done with:
```java
OpenPgpApi api = new OpenPgpApi(this, mServiceConnection.getService());
Intent result = api.executeApi(data, is, os);
```
2. The PGP operation is executed by OpenKeychain and the produced ciphertext is written into ``os`` which can then be accessed by the client app.
3. A result Intent is returned containing one of these result codes:
* ``OpenPgpApi.RESULT_CODE_ERROR``
* ``OpenPgpApi.RESULT_CODE_SUCCESS``
* ``OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED``
If ``RESULT_CODE_USER_INTERACTION_REQUIRED`` is returned, an additional ``PendingIntent`` is returned to the client, which must be used to get user input required to process the request.
A ``PendingIntent`` is executed with ``startIntentSenderForResult``, which starts an activity, originally belonging to OpenKeychain, on the [task stack](http://developer.android.com/guide/components/tasks-and-back-stack.html) of the client.
Only if ``RESULT_CODE_SUCCESS`` is returned, ``os`` actually contains data.
A nearly complete example looks like this:
```java
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: {
try {
Log.d(OpenPgpApi.TAG, "output: " + os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) {
OpenPgpSignatureResult sigResult
= result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
[...]
}
break;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: {
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
try {
startIntentSenderForResult(pi.getIntentSender(), 42, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
case OpenPgpApi.RESULT_CODE_ERROR: {
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
[...]
break;
}
}
```
4. Results from a ``PendingIntent`` are returned in ``onActivityResult`` of the activity, which executed ``startIntentSenderForResult``.
The returned ``Intent data`` in ``onActivityResult`` contains the original PGP operation definition and new values acquired from the user interaction.
Thus, you can now execute the ``Intent`` again, like done in step 1.
This time it should return with ``RESULT_CODE_SUCCESS`` because all required information has been obtained by the previous user interaction stored in this ``Intent``.
```java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[...]
// try again after user interaction
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 42: {
encrypt(data); // defined like in step 1
break;
}
}
}
}
```
### Tipps
* ``api.executeApi(data, is, os);`` is a blocking call. If you want a convenient asynchronous call, use ``api.executeApiAsync(data, is, os, new MyCallback([... ]));``, where ``MyCallback`` is an private class implementing ``OpenPgpApi.IOpenPgpCallback``.
See [``OpenPgpApiActivity.ja
没有合适的资源?快使用搜索试试~ 我知道了~
Android-K-9Mail–一个开源Android电子邮件客户端
共1336个文件
java:627个
png:326个
xml:243个
需积分: 19 8 下载量 111 浏览量
2019-08-13
03:25:04
上传
评论 1
收藏 4.17MB ZIP 举报
温馨提示
K-9 Mail – 一个开源Android电子邮件客户端
资源推荐
资源详情
资源评论
收起资源包目录
Android-K-9Mail–一个开源Android电子邮件客户端 (1336个子文件)
ic_action_remote_search.ai 1.02MB
ic_button_archive.ai 231KB
IOpenPgpService2.aidl 1014B
IOpenPgpService.aidl 932B
gradlew.bat 2KB
proguard.cfg 1KB
config 3KB
rsa2048-explicit-type.eml 2KB
rsa2048-unknown-critical.eml 2KB
rsa2048-unknown-non-critical.eml 2KB
unknown-type.eml 2KB
rsa2048-simple-to-bot.eml 2KB
rsa2048-simple.eml 2KB
rsa2048-broken-base64.eml 2KB
no_autocrypt.eml 384B
K-9_Mail.eps 43KB
loading.gif 2KB
.gitattributes 37B
.gitignore 575B
.gitignore 298B
.gitignore 298B
.gitignore 278B
.gitmodules 0B
build.gradle 4KB
build.gradle 2KB
findbugs-android.gradle 1016B
build.gradle 917B
build.gradle 826B
build.gradle 423B
checkstyle-android.gradle 384B
settings.gradle 126B
gradlew 5KB
activity_diagram.graphml 45KB
downloading.html 292B
gradle-wrapper.jar 53KB
settings.jar 2KB
MessagingController.java 173KB
MessageListFragment.java 99KB
LocalFolder.java 92KB
Accounts.java 85KB
MessageCompose.java 72KB
Account.java 71KB
MessageList.java 58KB
AccountSettings.java 53KB
ImapFolder.java 53KB
K9.java 53KB
ImapFolderTest.java 50KB
Pop3Store.java 49KB
LocalStore.java 48KB
SettingsImporter.java 47KB
MimeUtility.java 46KB
MigrationTest.java 46KB
FolderList.java 44KB
SmtpTransportTest.java 43KB
ImapConnectionTest.java 40KB
WebDavStore.java 40KB
ImapSync.java 39KB
MessagingControllerTest.java 38KB
RecipientPresenter.java 36KB
MessageProvider.java 35KB
PgpMessageBuilderTest.java 33KB
SmtpTransport.java 33KB
ImapConnection.java 33KB
MigrationTo51.java 32KB
Prefs.java 32KB
CharsetSupport.java 32KB
MessageCryptoHelper.java 31KB
EmailProvider.java 30KB
MessageViewFragment.java 30KB
AccountSetupIncoming.java 29KB
Base64.java 28KB
OpenPgpApi.java 26KB
ImapFolderPusher.java 26KB
WebDavFolder.java 25KB
MessageViewInfoExtractorTest.java 25KB
RecipientSelectView.java 24KB
StorageManager.java 24KB
WebDavFolderTest.java 23KB
AccountSetupOutgoing.java 22KB
MimeMessage.java 22KB
SettingsExporter.java 22KB
MessageViewInfoExtractor.java 22KB
MessageBuilder.java 22KB
GlobalSettings.java 22KB
MessageCryptoStructureDetectorTest.java 21KB
ImapResponseParserTest.java 21KB
MessageContainerView.java 21KB
Settings.java 20KB
AccountSetupBasics.java 20KB
AccountSetupCheckSettings.java 20KB
MessageHeader.java 19KB
TrustManagerFactoryTest.java 19KB
MessageExtractor.java 19KB
MessageLoaderHelper.java 19KB
NewMailNotificationsTest.java 18KB
PgpMessageBuilder.java 18KB
ImapStoreTest.java 18KB
Utility.java 18KB
QuotedMessagePresenter.java 18KB
RecipientLoader.java 17KB
共 1336 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
weixin_39840914
- 粉丝: 435
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功