```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. ...
{"title":"wk13","description":"凱薩密碼","contributors":"[{\"id\":\"dddf0523-8116-4e33-81ae-4e51d5181be6\",\"add\":6307,\"del\":0}]"}