惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
The Hacker News
The Hacker News
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
I
Intezer
aimingoo的专栏
aimingoo的专栏
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
P
Proofpoint News Feed
B
Blog
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
H
Help Net Security
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
GRAHAM CLULEY

mephisto.cc

修复rime-ice无法弹出候选框的问题 开机滚动字体显示优化 个人网站安全防护 树莓派上部署Vaultwarden 使用goaccess实时分析Caddy日志 个人网站监控 Hugo全站AVIF记 Linux下尝试使用Godot开发小游戏 Arch linux dae 透明代理 Airflow接管galler-dl下载任务 如何使用gallery-dl批量下载图像 Arch核显下如何愉快玩Dota2和CS2 Arch/labwc 环境网络相关设置 Rime添加dota2词库 Arch 重装记录 Supertuxkart iOS版终于发布了 打造自己的流媒体音乐服务 剪切板管理工具clipcat推荐 MangoHud性能监控 微信小程序开发记 Rofi试用 解决Arch下VSCode无法输入中文的问题 独立窗口管理器下无法录屏问题处理 上海市二手房成交数据监控 OpenLDAP监控 Arch linux如何顺畅连接蓝牙设备 使用Git和Ansible管理配置文件 Arch Linux SSL VPN 客户端配置 简单获取celery任务实时结果 Arch linux下iNode客户端的安装和使用方法 券商费率调整记 迷你主机组装记 Labwc更换定制皮肤 树莓派跑分流代理 实用自动代理配置示例 九年老鼠标复活记 Openvpn示例 Mac下如何旋转webp图片 如何隐藏Vscode中Markdown PDF插件的头部内容 解决Linux下网络配置无法变更保存的问题 labwc环境启用wlogout Ubuntu下新官方微信尝鲜 简单检测用户是否开启广告屏蔽 Atuin ZFS下卡顿问题解决 Wine安装64位微信 Fish 和 Atuin使用记 Fastapi简单实现临时下载链接 Fastapi微信公众号开发简要 Hugo根据语种展示不同内容 如何修复Waybar微信图标错误 Linux环境下维护公众号记录 快速隐藏和呼出终端 Wayland环境自动切换壁纸 SuperTuxKart 试玩 简单抓取雪球股票快讯 为什么你的kill命令不能列出信号表 Wine安装微信保姆教程 Hysteria科学上网简要 Ubuntu 23.10 钉钉无法启动解决方法 网站导航栏防止插入Adsense自动广告 Linux环境按键检测 Hugo文末添加最后修改时间 Wayland环境ksnip无法复制问题解决 我眼中的股票市场 Caddy简单图片防盗链 greetd和regreet使用教程 Wayland环境下截图加后期修改 Wayland 环境下gif录屏 Firefox的一些有趣功能 Ubuntu 切换系统语言 Firefox 标题栏高度调整 网站添加回到顶部功能 Hugo自定义字体 Labwc 便捷配置 Wofi使用教程 窗口管理器labwc使用记 文件共享软件Dufs推荐 Hugo静态站点接入Adsense广告 Fcitx5配置详解 Ubuntu安装chrome的方法总结 一种Xterm.js的全屏方案 Google网址收录api Python示例 手机听离线音乐 Snipe it资产导入 应用启动器yofi使用配置 Snipe it资产管理系统安装使用 Ubuntu开机启动加速 Vue3 vite TypeScript跨域等相关设置 Ubuntu手动升级Libreoffice Ubuntu设置alacritty为默认终端 简体文章批量转换为繁体 使用Inkscape调整svg图片大小 实用命令 使用mitmproxy给手机app抓包 Python中rstrip方法细解 Github同主机多仓库部署deploy key问题处理 Linux安装最新版本Python KeePassXC使用教程 Ubuntu 22.10连接蓝牙耳机报错br-connection-profile-unavailable解决方法 Caddy日志配置轮转和格式化
信封加密简要
2023-12-21 · via mephisto.cc

信封加密(Envelope Encryption)是一种应对海量数据的高性能加解密方案,国内的各种公有云都有密钥管理系统,简称 KMS(Key Management Service),该系统中就包含信封加密方案。

一图胜万言(图来自腾讯云文档):

enveope encrypt decrypt

名词解释:

  • DEK = data encryption keys
  • KMS = Key Management Service
  • AES = Advanced Encryption Standard

加密核心流程:

    1. 用户向 KMS 请求的时候,系统会给你返回一对密钥(明文 key+密文 key), 二者作用不同,明文 key 用来给本地文件加密,密文 key 用来从 KMS 系统获取明文 key。至于 KMS 系统如何维护二者的对应关系的,这个读者大可不必担心。反正 2 个 key 关系非同一般,各司其职,要是真不能理解,可以想想婚姻登记所,传统婚姻一个主内做家务,一个主外搞钱。
    1. 用明文 key 加密数据,比如你要加密一个 test.txt 文件什么的。心急的可能要问,有了明文 key,到底怎么加密呢?不要慌,我给你看简单 Python 示例:
    1from Crypto.Cipher import AES
    2key = b'Sixteen byte key'         # 这里就是明文key(here is the plainkey)
    3cipher = AES.new(key, AES.MODE_EAX)
    4nonce = cipher.nonce
    5ciphertext, tag = cipher.encrypt_and_digest(data) # 加密 data,返回 ciphertext(先忽略nonce和tag等)
    
    1. 假设上面的 text.txt 加密后命名为 secret.bin,本地加密完成后总要放到一个地方存起来,大陆新闻里面的贪官,钱财存墙体、保险柜、小区空房什么的,如今数字时代,文件一般存到云存储中,存起来的时候你不光要存加密文件本身,你还得存密文 key,不然没法解密,那玩笑就开大了,白忙活。一般加密后明文 key 就删除了,不留痕迹。加密后的文件和密文 key 一起存起来,好比用信封给包起来?(所以叫信封加密?这是我猜的...)

解密核心流程:

    1. 若干年后,你要用数据了。先把信封从存储系统中拿出来,信里面有加密数据 + 密文 key,你用密文 key 向 KMS 系统请求获得明文 key,然后你想起上面雪封很久的加密程序,心理想太好了。当初是我亲手加密的,现在亲手解密,一切就像回到从前,存封许久的密密文件由你来解码,感觉自己像军统特务,神秘且居功至伟,这是熟悉的感觉!
    1. 回到现实,用 KMS 返回的明文 key 解密数据,也搞个简单示例:
    1  from Crypto.Cipher import AES
    2  key = b'Sixteen byte key'          # 还是那个熟悉的明文key(the same plainkey)
    3  cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
    4  plaintext = cipher.decrypt(ciphertext)   # 解密得到plantext
    5  try:
    6      cipher.verify(tag)
    7      print("The message is authentic:", plaintext)
    8  except ValueError:
    9      print("Key incorrect or message corrupted")
    
    1. 拿到数据明文后,阅后即焚毁,白天兴奋不安、夜晚难以入眠,几十年来,这个星球上只有你一个人知道这个机密,太不可思议了 😅。

好了,虚拟故事讲完了,现实中,你不是戴笠,也没有委员长给你撑腰。

苦哈哈写代码才是日常.....

所谓帮人帮到底,下面我给一个比较完整的伪代码示例:

 1
 2import requests
 3import base64
 4from Crypto.Cipher import AES    #pip install pycryptodome
 5
 6
 7url = "your_kms_url"
 8
 9
10def gen_signature(method, params, secret_key):
11
12    '''
13    signature
14    '''
15    pass
16
17
18def generate_data_key():
19    '''
20    get planinkey and cipherkey
21    '''
22    params = {
23        'xxx1': 'yyy1',
24        'xxx2': 'yyy2',
25    }
26    params['signature'] = gen_signature()
27    r = requests.get(url, params=params)
28    rdata = r.json()["data"]
29    plainkey, cipherkey = rdata["plaintext"], rdata["ciphertext"]
30    return plainkey,cipherkey
31
32
33def get_plainkey(cipherkey):
34    '''
35    use cipherkey get plainkey"
36    '''
37    params = {
38        'zzz1': 'mmm1'
39        'foo': 'bar'
40    }
41    params["ciphertext"] = cipherkey
42    params['signature'] = gen_signature()
43
44    r = requests.get(url, params=params)
45    rdata = r.json()["data"]
46    plainkey = rdata["plaintext"]
47    return plainkeypt
48
49
50def envelope_encrypt(plainkey):
51    '''encrypt'''
52    key = base64.b64decode(plainkey)
53    with open(to_be_encrypt_file, "rb") as f:
54        data = f.read()
55    cipher = AES.new(key, AES.MODE_SIV)  # chose your aes mode
56    ciphertext, tag = cipher.encrypt_and_digest(data)
57
58    with open(encrypted_file,"wb") as file_out:
59        [ file_out.write(x) for x in (tag, ciphertext) ]
60
61def envelope_decrypt():
62    '''decrypt'''
63    with open(cipherkey_file,'r') as f:
64        cipherkey = f.read()
65    key = getDatakeyPlaintext(cipherkey)
66    key = base64.b64decode(key)
67    if key:
68        with open(encrypted_file,"rb") as file_in:
69            tag, ciphertext = [ file_in.read(x) for x in (16, -1) ]
70        cipher = AES.new(key, AES.MODE_SIV)
71        data = cipher.decrypt_and_verify(ciphertext,tag)
72        with open(decrypt_file,"wb") as file_origin:
73            file_origin.write(data)
74
75def upload_to_cloud(upload_file):
76    '''
77    upload cipherkey and secret file to cloud like aws s3
78    '''
79    pass
80
81
82if __name__ == "__main__":
83
84    to_be_encrypt_file = 'text.txt'          # 待加密文件
85    encrypted_file = "secret.bin"            # 加密后的文件
86    decrypt_file = "origin"                  # 解密后的文件
87    cipherkey_file = "cipherkey"
88
89    plainkey, cipherkey = generate_data_key()
90
91    # encryt
92    envelope_encrypt(plainkey)
93
94    #upload
95    upload_to_cloud(upload_file)
96
97    # decrpyt at someday
98    #envelope_decrypt()

上面的伪代码只是反应了核心流程,还需要读者根据自己的业务状况修改。