首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python实现Diffie-Hellman密钥交换算法

python实现Diffie-Hellman密钥交换算法

作者头像
timerring
发布2022-07-20 14:41:20
发布2022-07-20 14:41:20
1.4K0
举报
文章被收录于专栏:TechBlogTechBlog

Program : Diffie–Hellman key exchange (on group)

In this part, you are required to implement the Diffie–Hellman key exchange algorithm in

\mathbb{Z}_p

from scratch. (Hint: review the procedure of ElGamal algorithm). As the Setup procedure is the same as ElGamal algorithm, it is assumed that the public parameters of

p

and

\alpha

are both set to constants in this part.

代码语言:javascript
复制
p: 11483166658585481347156601461652228747628274304826764495442296421425015253161813634115028572768478982068325434874240950329795338367115426954714853905429627
alpha: 9312361210673900259563710385567927129060681135208816314239276128613236057152973946513124497622387244317947113336161405537229616593187205949777328006346729

In this program, two parties, Alice and Bob, want to get a symmetric key for future symmetric encryption via Diffie–Hellman key exchange, and hope that adversaries learn nothing about the shared symmetric key. In this program, it is assumed that only Honest-but-Curious adversaries exist.

Your program will output the following items:

  • All the transmission data on the communicate channel (in correct order, if necessary), e.g. Alice to Bob: blah blah blah.
  • The symmetric key that Alice gets, that is the result of Diffie–Hellman key exchange.
  • The symmetric key that Bob gets, that is the result of Diffie–Hellman key exchange.

Example Output

代码语言:javascript
复制
Alice to Bob: 8940959903919892646369383076988236263414149283589789417534093823879702643730138301746710316972043367005133179322397075568692734123174632487566957931486431
Bob to Alice: 4384683352873557635562368964248068727038294529254597987180258684651520296204501642442796366842225710992904070529210926322430373646688781391733323295711438
Result (Alice view): 11340178546045069617516325240966622435238310460224925781433563012664618800006804703149537436309299281476260328893892580363729101975655852342449233799172983
Result (Bob view): 11340178546045069617516325240966622435238310460224925781433563012664618800006804703149537436309299281476260328893892580363729101975655852342449233799172983
solution code
代码语言:javascript
复制
import random

p: int = 11483166658585481347156601461652228747628274304826764495442296421425015253161813634115028572768478982068325434874240950329795338367115426954714853905429627
alpha: int = 9312361210673900259563710385567927129060681135208816314239276128613236057152973946513124497622387244317947113336161405537229616593187205949777328006346729


# 快速模幂函数:
def quick_mod(in_a: int, in_b: int, in_p: int) -> int:
    in_a %= in_p  # 预处理
    ans = 1  # 记录结果
    while in_b != 0:
        if in_b & 1:
            ans = (ans * in_a) % in_p
        in_b >>= 1  # 移位操作
        in_a = (in_a * in_a) % in_p
    return ans


da: int = random.randint(1, p - 1)
pa: int = quick_mod(alpha, da, p)
print('Alice to Bob:', pa)

db: int = random.randint(1, p - 1)
pb: int = quick_mod(alpha, db, p)
print('Bob to Alice:', pb)

k_Alice: int = quick_mod(pb, da, p)
print('Result (Alice view):', k_Alice)

k_Bob: int = quick_mod(pa, db, p)
print('Result (Bob view):', k_Bob)
output
代码语言:javascript
复制
Alice to Bob: 5401715652811798129297331183196252248450609218063188670304313143373208976703187421383700793089900140600880484160655782279227609131431659730556049081414488
Bob to Alice: 10381154167090192760107089673416704448415008747056849721982037743379509557693524847560794289113004966742132559903754830205621033822324882161216780933705183
Result (Alice view): 8104765534365036719551043733060004916489593108004314113023589709245844912143607805211963817440317208929064111677451038766659888032127613276974880121512400
Result (Bob view): 8104765534365036719551043733060004916489593108004314113023589709245844912143607805211963817440317208929064111677451038766659888032127613276974880121512400

进程已结束,退出代码为 0

受于文本篇幅原因,本文相关算法实现工程例如环境及相关库,无法展示出来,现已将资源上传,可自行点击下方链接下载。

python实现Diffie-Hellman密钥交换算法工程文件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Program : Diffie–Hellman key exchange (on group)
    • solution code
    • output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档