> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/web_advance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/web_advance/cryptojia_mi.md).

# Crypto加密

## Crypto加密

## 簡單字串加密

```
let cipher = crypto.createCipher('aes-256-cbc','testkey');
let crypted = cipher.update('要加密的東西','utf8','hex');
crypted += cipher.final('hex');
```

解密

```
let decipher = crypto.createDecipher('aes-256-cbc','testkey');
let dec = decipher.update(crypted,'hex','utf8');
dec += decipher.final('utf8');
console.log('解密的文本：'+dec);
```

注意：如果產生錯誤

> TypeError: error:0606506D:digital envelope routines:EVP\_DecryptFinal\_ex:wrong final block length

可把`'hex'`改為`'binary'`試試

## JWT Token產生

<https://github.com/auth0/node-jsonwebtoken>

Server

> 以下為預設的HMAC加密，第二個參數放入secret，可以放入客戶password

```javascript
// Generate JWT token
const jwt = require('jsonwebtoken');
const token = jwt.sign(result.rows[0], result.rows[0].password);
console.log(token)
```

Client

> 登入時驗證password正確，token從localstorage等地方取出。

```javascript
const decoded = jwt.verify(token, result.rows[0].password);
console.log(decoded) // bar
```

如果客戶端已經有Token則直接使用密碼與verify解密即可驗證，不用再次去DB query。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://easonwang.gitbook.io/web_advance/cryptojia_mi.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.
