Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
Information-Flow-Experiments
/
notes
Public
forked from
JavaDevTeam/notes
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
notes
/
Python
/
python-lib-rsa.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
45 lines (36 loc) · 1.63 KB
master
Breadcrumbs
notes
/
Python
/
python-lib-rsa.py
Copy path
Top
File metadata and controls
Code
Blame
45 lines (36 loc) · 1.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
rsa模块
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
# 创建密钥对
(
publicKey
,
privateKey
)
rsa
.
newkeys
(
length
)
*
length
表示长度
# 把key序列化为证书格式的文本
publicKey
.
save_pkcs1
().
decode
()
privateKey
.
save_pkcs1
().
decode
()
*
其实就是序列化为字符串
-
-
-
-
-
BEGIN
RSA
PUBLIC
KEY
-
-
-
-
-
MIIBCgKCAQEA3TQgbv7yA2tmp6Cxp2VO9dz8ByfYSZWVhtpc1kKYwTljjaZv2U4e
...
-
-
-
-
-
END
RSA
PUBLIC
KEY
-
-
-
-
-
-
-
-
-
-
BEGIN
RSA
PRIVATE
KEY
-
-
-
-
-
MIIEqgIBAAKCAQEA3TQgbv7yA2tmp6Cxp2VO9dz8ByfYSZWVhtpc1kKYwTljjaZv
2
U4eKCc4k33z6euoWwEyn5eYYfSC
+
9
I7AJLwJXq7ABy
+
o
/
fBVXZR
/
WsepuX506Ew
...
-
-
-
-
-
END
RSA
PRIVATE
KEY
-
-
-
-
-
# 导入密钥
*
导入文本格式的公钥和密钥
(
格式见上
)
rsa
.
PublicKey
.
load_pkcs1
(
publicKeyStr
)
rsa
.
PrivateKey
.
load_pkcs1
(
privateKeyStr
)
# 私钥签名,公钥验签
signature
=
rsa
.
sign
(
content
,
privateKey
,
'SHA-1'
)
# 使用私钥计算出前面,算法使用 SHA-1
rsa
.
verify
(
content
,
signature
,
publicKey
)
# 使用公钥对签名进行验证
# 公钥加密,私钥解密
import
rsa
publicKey
,
privateKey
=
rsa
.
newkeys
(
512
)
# 初始化密钥对
result
=
rsa
.
encrypt
(
"你好啊"
.
encode
(
'UTF_8'
),
publicKey
)
# 公钥加密,计算出密文
content
=
rsa
.
decrypt
(
result
,
privateKey
);
# 私钥根据密文解密出原始数据
print
(
content
.
decode
(
'UTF_8'
))
# 私钥加密公钥解密
*
虽然rsa算法理论上支持对称的公钥加密私钥解密
/
私钥加密公钥解密
,
但大部分平台的rsa
api都设计成只提供public
key
encrypt
/
private
key
decrypt的接口
*
这是由于私钥加密会带来私钥泄露的风险
,
一般私钥加密过程只用于签名sign
*
因为sign的过程是加密之前对消息进行hash
,
然后der
,
然后加密
,
验证的过程是逆向的
,
对比解密和der解码之后的hash做对比
,
因此不会泄露private
key
You can’t perform that action at this time.