bujiyao
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## AES 加密原理 AES(Advanced Encryption Standard,進階加密標準)是一種**對稱式加密演算法**,也就是說,**加密和解密都使用相同的金鑰**。根據金鑰長度不同,AES 提供 **128、192 或 256 位元**的安全性。以下是 AES 的核心概念與運作流程: ### 基本概念 1. **對稱式加密** - 加密和解密都使用**相同的金鑰**。 - 這種加密方式的安全性取決於金鑰是否保密。 2. **區塊加密** - AES 以**固定大小的區塊(128 位元 = 16 位元組)**進行加密。 - 如果輸入資料超過區塊大小,則會**補齊(Padding)**,或將資料分割成多個區塊分別加密。 3. **多輪加密** - AES 根據金鑰長度不同,會執行 **10、12 或 14 輪**的運算,每輪都包含多個步驟。 ### 加密流程 ![image](https://hackmd.io/_uploads/H1_12XzvJx.png) 每個區塊會依照以下步驟逐一加密,直到所有區塊加密完成後,再將它們組合成最終的密文: ## AES 加密流程 ### **1. 初始運算:AddRoundKey** - **AddRoundKey**:將**明文與第一組輪密鑰(Round Key)進行 XOR 運算**。 ![image](https://hackmd.io/_uploads/HyNYC7YUkx.png) ### **2. 重複運算(根據金鑰長度,執行 10、12 或 14 輪)** 每輪加密包含以下 **四個步驟**: #### **步驟 1:SubBytes** - **使用 S-Box(替換表)對狀態矩陣的每個字節進行替換**。 - S-Box 是透過**非線性轉換與有限域(GF)反演**計算出的,可增強 AES 對攻擊的抵抗力。 ![image](https://hackmd.io/_uploads/ByhIGEF8yx.png) #### **步驟 2:ShiftRows** - **狀態矩陣的每一列進行位移**,以打散明文的字節排列,提高擴散性: - **第 0 列**:不變。 - **第 1 列**:左移 1 位元組。 - **第 2 列**:左移 2 位元組。 - **第 3 列**:左移 3 位元組。 ![image](https://hackmd.io/_uploads/H1szQNFUyg.png) #### **步驟 3:MixColumns** - **每一欄的數據視為多項式,並與固定多項式相乘**,讓單一字節的變化影響整欄數據,增強擴散效果。 ![image](https://hackmd.io/_uploads/BkNifNYL1x.png) #### **步驟 4:AddRoundKey** - **將當前狀態矩陣與該輪對應的密鑰進行 XOR 運算**。 ![image](https://hackmd.io/_uploads/BkcCG4tUyg.png) ### **3. 最終輪運算(Final Round)** - **重複執行**「SubBytes、ShiftRows 和 AddRoundKey」**(省略 MixColumns)**。 - 最終輸出即為**密文**。 ## **AES 解密流程** ![image](https://hackmd.io/_uploads/Hk0ajXzvkg.png) AES 的解密過程是加密的反向操作,主要步驟如下: 1. **InvShiftRows** 2. **InvSubBytes** 3. **AddRoundKey** 4. **InvMixColumns** 與加密不同,**解密時的使用順序是反向的**。 ## **AES 兩種加密模式** 區塊加密(Block Cipher)是一種**固定區塊大小**的加密方式,每個明文區塊會對應到相同大小的密文區塊。**AES 的區塊大小固定為 128 位元**(16 位元組),但金鑰長度可以是 **128、192 或 256 位元**。 當明文長度不足 128 位元時,需要**補齊(Padding)**,常見的填充方式包括 **PKCS5 或 PKCS7**。 ### **1. ECB 模式(Electronic Code Book,電子密碼本模式)** ![image](https://hackmd.io/_uploads/rJn44EMD1l.png) **ECB 模式的特點**: - **每個明文區塊**都是**獨立加密**的,彼此之間沒有關聯。 - 這種模式的優勢是**可以並行處理**,加密速度快。 - **缺點**:相同的明文區塊會產生相同的密文區塊,導致安全性較低,容易被模式分析攻擊(Pattern Attack)破解。 - **適用場景**:僅適合小量、不敏感的數據加密。 ### **2. CBC 模式(Cipher Block Chaining,加密區塊鏈模式)** ![image](https://hackmd.io/_uploads/Sy8lEVfvke.png) **CBC 模式的特點**: - 每個**明文區塊**在加密前**都會與前一個密文區塊進行 XOR 運算**,因此**每個區塊的加密結果都不同**。 - 第一個區塊需要一個 **初始化向量(IV,Initialization Vector)** 來進行 XOR 運算,以確保每次加密輸出都不同,即使明文相同。 - **缺點**:由於每個區塊依賴於前一個區塊,無法並行加密,導致效能較低。 - **適用場景**:適合需要更高安全性的應用,例如**傳輸機密資料**或**檔案加密**。 **並行處理應用**: 儘管 CBC 模式無法針對單一任務並行加密,但可以**針對多個獨立任務並行運作**,提升硬體資源利用率。例如: - **不同用戶的 CBC 加密**可以獨立處理,提高整體系統效能。 ## 實驗結果 我在使用C語言實現AES加解密結果,完整的專案程式碼可以參考 [bujiyao/AES-implementation-c](https://github.com/bujiyao/AES-implementation-c)這個專案。這個實作完整涵蓋了 AES-128、AES-192 和 AES-256,並包含 加密、解密、密鑰擴展(Key Expansion)等核心功能。 單元測試結果如下: ```bash Running AES unit tests: [PASS] test_add_round_key [PASS] test_sub_bytes [PASS] test_shift_rows [PASS] test_inv_shift_rows [PASS] test_mix_columns [PASS] test_inv_mix_columns [PASS] test_aes_encrypt_decrypt All tests completed. ``` ## 參考資料 - [AES Encryption: Secure Data with Advanced Encryption Standard](https://www.simplilearn.com/tutorials/cryptography-tutorial/aes-encryption) - [The difference in five modes in the AES encryption algorithm](https://www.highgo.ca/2019/08/08/the-difference-in-five-modes-in-the-aes-encryption-algorithm/) - [A Pipelined AES and SM4 Hardware Implementation for Multi-Tasking Virtualized Environments](https://easychair.org/publications/preprint/BNHg)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully