
前言
- 不知道你平常如何保存助記詞?也許用截圖、或 plain text。
這種明碼的方式如果放在本地端應該沒什麼問題,不過像我想備份在雲端的話,就必須要加密。
- 本篇介紹用
ccrypt
工具加密字串或文字檔案的方法!!
說明
安裝 ccrypt
我是用 Mac,直接
如果是 linux 環境可以
1
2
3
4
|
apt install ccrypt
# CentOS
# yum install ccrypt
|
更多 OS 安裝方式參考 REF [2]
加密與解密
假如我有以下助記詞 my-phrase.txt
:
1
2
3
4
|
labellum proximal shanghai pedate
vesica feat estreat matrass
smug sawmill spitfire milord
trend twill jillion picayune
|
(產自 REF [1])
加密:
1
2
3
4
5
6
7
|
password="my-secret-password"
ccrypt -e -K "${password}" <my-phrase.txt >my-phrase-encrypted.cpt
# 也可以在互動模式下輸入密碼
ccrypt -e <my-phrase.txt >my-phrase-encrypted.cpt
|
流程:
- 把
my-phrase.txt
作為 stdin 傳入給 ccrypt
- 將加密結果存成
my-phrase-encrypted.cpt
參數:
-e
代表 encrypt
-K
代表傳入密碼;不放 -K
的話就是用互動式的方式輸入密碼
解密:
解密流程更單純,把 .cpt
作為 stdin 傳入給 ccrypt
即可
1
2
3
4
5
6
7
|
password="my-secret-password"
ccrypt -d -K "${password}" <my-phrase-encrypted.cpt
# 同樣也可以在互動模式下輸入密碼
ccrypt -d <my-phrase-encrypted.cpt
|
流程:
- 把
my-phrase-encrypted.cpt
作為 stdin 傳入給 ccrypt
解密
參數:
-d
代表 decrypt
-K
代表傳入密碼;不放 -K
的話就是用互動式的方式輸入密碼
執行就會得到原本的助記詞
1
2
3
4
|
labellum proximal shanghai pedate
vesica feat estreat matrass
smug sawmill spitfire milord
trend twill jillion picayune
|
這樣只要保存好密碼,各種加密後的 .cpt
檔都可以放在雲端了!
如果只想處理字串
如果只是要加密一段字串,懶得處理檔案,可以使用 xxd
來轉成 16 進制的字串傳送:
1
2
3
|
# 假設密碼是 123
ccrypt -f -e -K 123 <<<"我的秘密字串" | xxd -p
|
會得到
1
2
|
948e516db4644d01d478cf4c4ce12589aa22115a7591017382a049964fb3
452b8af6ccf3943137b071bd7efd55a27a33505aab
|
此時可以把這段字串傳給其他人。
接收到的人可以使用
1
2
3
4
5
6
|
# 假設密碼是 123
cypher="948e516db4644d01d478cf4c4ce12589aa22115a7591017382a049964fb3
452b8af6ccf3943137b071bd7efd55a27a33505aab"
echo "$cypher" | xxd -r -p - | ccrypt -d -K 123
|
就會得到
Note
xxd
的目的是把二進制的資料轉換成 16 進制的字串,方便傳遞。
- 原始明文的字串也可以多行,用引號包起來即可
REF
- https://www.fourmilab.ch/javascrypt/pass_phrase.html
- https://command-not-found.com/ccrypt
- https://stackoverflow.com/questions/6292645/convert-binary-data-to-hexadecimal-in-a-shell-script