fred ko
    • 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
    • Make a copy
    • 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 Make a copy 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
    ```python text='i love python' reverse_text=text[::-1] print(reverse_text) ``` nohtyp evol i ```python #【重要函式】 chr(122) ord('z') ``` 122 ```python i='p' shift=3 encryptext_char='' if i.supper()(): encryptext_char = chr(( ord(i) - ord('A') + shift ) % 26 + ord('A')) else: encryptext_char = chr(( ord(i) - ord('a') + shift ) % 26 + ord('a')) print(encryptext_char) ``` ## wk13_0516_CEASARcipher (練習單) ### {案例觀摩} 1. 凱薩密碼 2. 凱撒密碼挑戰遊戲 3. 凱薩密碼破解器 --- ### 1. 凱薩密碼 #### 函式說明 <pre> 1. range(start, stop, step): 生成一個整數範圍的序列。 start 是起始值(包含在序列中)。 stop 是結束值(不包含在序列中)。 step 是步長,表示每個元素之間的間隔。如果沒有提供,默認值是 1。 例如,range(1, 10, 2) 將生成包含 1, 3, 5, 7, 9 的序列。 2. len(sequence):返回序列(列表、元組、字符串等)的長度。 sequence 是要測量長度的對象。 例如,len("hello") 返回 5,因為字符串 "hello" 有 5 個字符。 3. ord(character):返回給定字符的Unicode碼點。 character 是要獲取Unicode碼點的字符。 例如,ord('A') 返回 65,因為大寫字母 'A' 的Unicode碼點是 65。 4. str(object):將給定的對象轉換為字符串。 object 是要轉換的對象,可以是數字、列表、元組等。 例如,str(123) 將數字 123 轉換為字符串 "123"。 </pre> #### 方法說明 <pre> 1. isupper() 是 Python 字符串方法之一,用於檢查字符串中的字符是否都是大寫字母。 語法:string.isupper() 返回值:如果字符串中的所有字符都是大寫字母,則返回 True;否則返回 False。 2. </pre> ```python def encrypt(text,s): result = "" #轉換內容 for i in range(len(text)): char = text[i] # 加密純文字中的大寫字符 if (char.isupper()): result += chr((ord(char) + s-65) % 26 + 65) # 加密純文字中的小寫字符 else: result += chr((ord(char) + s - 97) % 26 + 97) return result #確認功能 text = "CEASER CIPHER DEMO" s = 4 print("Plain Text : " , text) print("Shift pattern : " , str(s)) print("Cipher: " , encrypt(text,s)) ``` Plain Text : CEASER CIPHER DEMO Shift pattern : 4 Cipher: GIEWIVrGMTLIVrHIQS ---- #### 程式說明 <pre> 這段程式碼實現了凱薩密碼(Caesar Cipher)的加密功能。凱薩密碼是一種古老的加密技術,通過將每個字母替換為字母表中固定偏移量的另一個字母來加密文本。 這個 encrypt() 函式接受兩個參數:text 和 s。text 是要加密的文本,而 s 是加密時使用的偏移量(也稱為密鑰)。 函式首先創建了一個空字符串 result 來存儲加密後的結果。然後,它遍歷 text 中的每個字符,並根據該字符是大寫還是小寫字母來進行加密。如果字符是大寫字母,則將其移位後對應到字母表中的另一個大寫字母;如果字符是小寫字母,則將其移位後對應到字母表中的另一個小寫字母。加密的過程使用了ASCII碼來進行字符與數字的轉換,具體來說,ord(char) 函式返回字符 char 的ASCII碼值,chr() 函式則將ASCII碼值轉換回對應的字符。 加密完成後,將加密後的字符添加到 result 中,最後返回加密後的文本。 在程式碼的下半部分,我們提供了一個示例用法來檢查這個函式的運行情況。我們將一個字符串 text 和一個偏移量 s 傳遞給 encrypt() 函式,並打印出原始文本、偏移量和加密後的結果。 </pre> --- ### 2. 凱撒密碼挑戰:加密與解密遊戲 #### 函式說明 1.def caesar_cipher_encrypt(message, shift): 定義了一個名為 caesar_cipher_encrypt 的函式,它有兩個參數:message(要加密的明文訊息)和 shift(整數,表示每個字母在密文中要移動的位數) 2.if char.isalpha(): 檢查當前字元 char 是否為字母。 3.ord(char): ord() 函式返回指定字元的Unicode編碼。 char 是要加密的字元。 #### 方法說明 #### 程式說明 這個程式使用凱撒加密法(Caesar Cipher)來加密和解密訊息。這個加密法是一種替換式加密方法,它將每個字母替換成位於字母表中一定數量位置後的字母。在這個程式中,使用者可以選擇加密或解密一條訊息,並且需要提供一個稱為"shift"的值,用來指定加密或解密時每個字母需要向後或向前移動的數量。 #### 逐行解說 ```python def caesar_cipher_encrypt(message, shift): encrypted_message = "" for char in message: if char.isalpha(): shifted_char = chr((ord(char) - 65 + shift) % 26 + 65) if char.isupper() else chr((ord(char) - 97 + shift) % 26 + 97) encrypted_message += shifted_char else: encrypted_message += char return encrypted_message def caesar_cipher_decrypt(encrypted_message, shift): decrypted_message = "" for char in encrypted_message: if char.isalpha(): shifted_char = chr((ord(char) - 65 - shift) % 26 + 65) if char.isupper() else chr((ord(char) - 97 - shift) % 26 + 97) decrypted_message += shifted_char else: decrypted_message += char return decrypted_message def main(): print("Welcome to the Caesar Cipher Challenge!") print("1. Encrypt a message") print("2. Decrypt a message") choice = input("Enter your choice (1 or 2): ") if choice == "1": message = input("Enter the message to encrypt: ") shift = int(input("Enter the shift value: ")) encrypted_message = caesar_cipher_encrypt(message, shift) print("Encrypted message:", encrypted_message) elif choice == "2": encrypted_message = input("Enter the message to decrypt: ") shift = int(input("Enter the shift value: ")) decrypted_message = caesar_cipher_decrypt(encrypted_message, shift) print("Decrypted message:", decrypted_message) else: print("Invalid choice. Please try again.") if __name__ == "__main__": main() ``` Welcome to the Caesar Cipher Challenge! 1. Encrypt a message 2. Decrypt a message --- ### 3. 凱薩密碼破解器:解密加密消息並尋找可能的解密鑰匙 #### 函式說明 1. 2. ... #### 方法說明 3. 4. ... ```python message = 'GIEWIVrGMTLIVrHIQS' #encrypted message LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for key in range(len(LETTERS)): translated = '' for symbol in message: if symbol in LETTERS: num = LETTERS.find(symbol) num = num - key if num < 0: num = num + len(LETTERS) translated = translated + LETTERS[num] else: translated = translated + symbol print('Hacking key #%s: %s' % (key, translated)) ``` Hacking key #25: HJFXJWrHNUMJWrIJRT #### 程式說明 <pre> 這段程式碼的目的是對一個加密的消息進行解密,並嘗試尋找可能的解密鑰匙。程式會使用凱薩密碼的破解方法,嘗試將密文依次使用所有可能的解密鑰匙進行解密,並輸出每個可能的解密結果。 </pre> #### 逐行解說: 1. message = 'GIEWIVrGMTLIVrHIQS': 定義了一個加密的消息,我們的目標是對這個消息進行解密。 2. LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': 定義了一個包含所有大寫英文字母的字符串,這將用於解密過程中。 3. for key in range(len(LETTERS)):: 這是一個嵌套的 for 迴圈,外層迴圈用於遍歷所有可能的解密鑰匙(即 0 到 25)。 4. translated = '': 定義一個空字符串,用於存儲解密後的消息。 5. for symbol in message:: 內層迴圈用於遍歷密文中的每個字符。 6. if symbol in LETTERS:: 檢查字符是否在 LETTERS 中,如果是,表示它是一個需要解密的字母。 7. num = LETTERS.find(symbol): 使用 find() 方法獲取字符在 LETTERS 中的索引,這將作為解密時的位置。 8. num = num - key: 根據解密鑰匙對字符的位置進行位移,即將密文的位置向左移動 key 個位置。 9. if num < 0: num = num + len(LETTERS): 如果位移後的位置小於 0,則將它加上 LETTERS 的長度,以保證它在 0 到 25 的範圍內。 10. translated = translated + LETTERS[num]: 將解密後的字符添加到解密結果字符串中。 11. else: translated = translated + symbol: 如果字符不在 LETTERS 中,則保持不變,直接添加到解密結果字符串中。 12. print('Hacking key #%s: %s' % (key, translated)): 打印每個可能的解密結果,並指示使用的解密鑰匙。 ### 【參考資料】 1. [密碼學分類&凱薩解碼器實作](https://ithelp.ithome.com.tw/articles/10328286) 2. [編碼跟加密-凱薩密碼](https://ithelp.ithome.com.tw/m/articles/10261660) 3. [秘密訊息](https://projects.raspberrypi.org/zh-TW/projects/secret-messages/3) ### 【討論】 1. 字母之外 2. 字串倒著寫 3. 部分改部分不改 4. ...

    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