Crypto日记之Diffie-Hellman算法+变异凯撒

本文介绍了picoCTF中一道关于Diffie-Hellman算法和变异凯撒加密的题目。通过算法解释和解题过程,展示了如何利用Diffie-Hellman密钥交换和凯撒密码的逆向操作来解密信息,从而获取flag。解密过程中发现,数字需要减去5,字母则进行移位,最终成功解出含有'caesar cipher is a bit outdated'的密文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0x01 前言

picoCTF 2022的一道Cryptography题目,好评率只有25%,flag提交正确率只有9%,题目非常简单,主要是考察了Diffie-Hellman算法以及变异凯撒问题,其实不需要解Diffie-Hellman也可以得到flag,但为了加深对Diffie-Hellman的理解,还是简单记录一下。

0x02 解题

题目描述

Description:Alice and Bob wanted to exchange information secretly. The two of them agreed to use the Diffie-Hellman key exchange algorithm, using p = 13 and g = 5. They both chose numbers secretly where Alice chose 7 and Bob chose 3. Then, Alice sent Bob some encoded text (with both letters and digits) using the generated key as the shift amount for a Caesar cipher over the alphabet and the decimal digits. Can you figure out the contents of the message?
Hints1:Diffie-Hellman key exchange is a well known algorithm for generating keys, try looking up how the secret key is generated;
Hints2:For your Caesar shift amount, try forwards and backwards.
下载附件得到加密字符串:H98A9W_H6UM8W_6A_9_D6C_5ZCI9C8I_CB5EJHB6

Diffie-Hellman算法

Diffie-Hellman算法是Whitefield Diffie和Martin Hellman在1976年公布的一种秘钥交换算法
Diffie-Hellman密钥交换方案的安全性都是取决于求离散对数问题的计算复杂性。
这篇文章讲的很清楚,有兴趣可以深入了解一下:

Diffie-Hellman协议是一种非对称密钥交换算法,它允许两个通信方在无需预先共享密钥的情况下建立共享密钥。在C语言中实现Diffie-Hellman的关键步骤包括选择素数、生成模数、选取公钥值和计算共享密钥。以下是简化的伪代码描述: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 定义大素数p和它的指数g #define P 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EE71F79938A0370FA07C0FEBA27B2D2DCB0FB0CA1ACDD6EB5A8A03DD83106AE709E5DB00A9F915FA3CF4F83B560855BF399B00327A809F4C8DB525847C952AFDE48B956469ECAC6FE8CEB53C0E5D06FBE0E50C0F073B48第二部分省略... void generateKeys(int prime, int g) { // Alice and Bob各自随机选择一个整数x srand(time(NULL)); int a = rand() % (prime - 1) + 1; int b = rand() % (prime - 1) + 1; // 计算公开部分:A = g^a mod p 和 B = g^b mod p int A = pow(g, a) % prime; int B = pow(g, b) % prime; printf("Alice's public key is A = %d\n", A); printf("Bob's public key is B = %d\n", B); return; // 假设私钥存储不再这里展示 } int main() { // 初始化并生成公钥 generateKeys(P, g); // Alice和Bob相互发送他们的公钥 // 这里假设通过安全通道交换... // 接下来,双方可以计算共享密钥K // (略去实际计算共享密钥的部分) printf("Shared secret key: K = %d\n", K); // 仅作为示例显示 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值