a.k.a進階版凱薩密碼
加密原理 :
明文、密鑰:由字母組成的字串
把 A 到 Z 編號成 0 到 25
明文中第一個字母的位移量就是密鑰第一個字母的編號、
第二個字母的位移量就是密鑰第二個字母的編號、……
如果密鑰長度比明文字母數短
就再從第一個字母開始用
明文:HSNU CRC
密鑰:CRSC
明文 H S N U C R C
密鑰 C R S C C R S
密文 J J F W E I U
因為每一個字母的位移量都不盡相同
所以不知道密鑰的情況下
要還原出明文是很困難的事
幾乎不可能
要怎麼把字母位移?
我們利用ASCII(美國資訊交換標準代碼)
找到字元 c 對應的數字:ord(c)
找到數字 i 對應的字元:chr(i)
純字母版
def encryption(key, password):
cipher = ""
now = 0
for char in password:
t = ord(char) - ord('A')
k = ord( key[now] ) - ord('A')
print(t+k)
cipher += chr( ord('A') + (t+k)%26 )
now = (now + 1) % len(key)
return cipher
def decryption(key, password):
cipher = ""
now = 0
for char in password:
t = ord(char) - ord('A')
k = ord( key[now] ) - ord('A')
print(t-k)
cipher += chr( ord('A') + (t-k)%26)
now = (now + 1) % len(key)
return cipher
廣泛版(不局限於字母)
def encryption(key, password):
cipher = ""
now = 0
for char in password:
k = ord( key[now] ) - ord('A')
cipher += chr( (ord(char) + k)%126 )
now = (now + 1) % len(key)
return cipher
def decryption(key, password):
cipher = ""
now = 0
for char in password:
k = ord( key[now] ) - ord('A')
cipher += chr( (ord(char) - k)%126 )
now = (now + 1) % len(key)
return cipher
我們將用它來加密/解密我們的密碼
有時候在很多網站創建很多帳號
其中每種帳號的密碼限制都不一樣
時間久了很容易忘記
這種時候就很需要Password Manager的幫忙
Process:
如果master password輸入錯誤,就會無法看到既有的密碼
key = input("What is the master password?")
while True:
mode= input("Would you like to add a new password or view existing ones (view, add), press q to quit? ").lower()
if mode == 'q':
break
if mode == "view":
view(key)
elif mode == "add":
add(key)
else:
print("Valid mode.")
continue
加解密函式
def encryption(key, password):
cipher = ""
now = 0
for char in password:
k = ord( key[now] ) - ord('A')
cipher += chr( (ord(char) + k)%126 )
now = (now + 1) % len(key)
return cipher
def decryption(key, password):
cipher = ""
now = 0
for char in password:
k = ord( key[now] ) - ord('A')
cipher += chr( (ord(char) - k)%126 )
now = (now + 1) % len(key)
return cipher
add()函式
在password.txt
中新加一條密碼,包含用戶名稱跟密碼
with open("password.txt", 'a') as file:
password.txt
就會自動被關掉,很方便輸入完會請使用者再確認一次
def add(key):
print('')
Done = False
while not Done:
name = input("Account name : ")
password = input("Password : ")
print('')
print("Please double check the account name and password you\'ve typed in :")
print("Account name :", name)
print("Password :", password)
correct = input("Type \"yes\" if they're correct, \"no\" if they're not : ").lower()
print('')
if correct == "yes":
encrypted_password = encryption(key, password)
# like a function. Once it's done, the file will be closed automatically.
# a stands for "pen". Add sth to an existing file or create a new file if the file doesn't exist.
# w stands for "write". Create a file or overwrite the file
# r stands for "read". Just read.
with open("password.txt", 'a') as file:
file.write(name + '|' + encrypted_password + '\n') # 寫入密碼
Done = True
elif correct == "no":
print("Alright. Please type again.")
continue
else:
print("Valid reaction.")
continue
view()
函式
把目前所有密碼都依照輸入的key解密後秀出來
file.readlines(len)
: 取得文字檔file的所有文字,len默認是-1,代表所有行數line.split('|')
: 把line依照'|'切割成n個不同字元,大多數密碼不會包含'|',所以假設只利用原先用來分割的'|'切割出兩個字串line.rstrip()
: 去除line最後面的指定部分,默認是空白、換行等等
def view(key):
print('')
with open("password.txt", 'r') as file:
for line in file.readlines():
data = line.rstrip() # delete the '\n' behind ecery line
user, password = data.split('|') # "FB|123456789" -> ["FB", "123456789"]
password = decryption(key, password)
print("Account name :", user, "| Password :", password)
print('')
恭喜各位完成Password Manager,
可以自己多加一些有趣的功能
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing