# Data Encrypt & Decrypt

今天把資料的加解密整理了一下也丟到 [Github](https://github.com/alanmoment/rsa-util) 了，不過只有 Server 的，雖然也可以玩玩看加解密，但也蠻奇怪的，加密端與解密端應該是要不同的平台，還要再找時間把 Android 的加密也整理一下。不曉得又要拖到什麼時候...哈。

只要在自己的 `composer.json` 加上設定檔就可以囉！

```php
{
  "repositories": [
      {
          "type": "git",
          "url": "https://github.com/alanmoment/rsa-util.git"
      }
  ],
  "require": {
      "alanmoment/rsa-util": "dev-master"
  },
  "minimum-stability": "dev",
  "autoload": {
      "classmap": [
          "vendor/alanmoment"
      ]
  }
}
```

範例程式碼

index.php

```php
require __DIR__ . '/vendor/autoload.php';

use Rsa\RsaUtil;

$RsaUtil = new RsaUtil();
$RsaUtil->setKeyStorePath("./keystores/");
$encrypt = $RsaUtil->generate()->encrypt("I am test data.");
echo $encrypt;

$decrypted = $RsaUtil->decrypt($encrypt);
echo $decrypted;
```

> May 5th, 2015 11:59:25pm


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alanmoment.gitbook.io/moment/php/data_encrypt_and_decrypt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
