Z2kDH

abyk 2024-09-01 23:31:00 169 0


Diffie-Hellman密钥交换协议,称为Z2kDH(可能是某种特定场景下的变体)。

模块定义和初始设置
modulus = 1 << 258
这行代码定义了一个大数modulus,其值等于 2^258这个modulus用于计算模幂操作。由于1 << 258是用位移操作符表示的,所以这实际上是一个非常大的数(1 后面跟2580)。
Alice 和 Bob 的私有指数
alice_private_exponent = int(open('alice_private_exponent.txt').read(), 16)
bob_private_exponent = int(open('bob_private_exponent.txt').read(), 16)
这两行代码从文件中读取Alice和Bob的私有指数,并将其从十六进制字符串转换为整数。这两个文件中的内容应该是私有密钥。

初始化公共结果
def Z2kDH_init(private_exponent):
    """
    Computes the public result by taking the generator 5 to the private exponent, then removing the last 2 bits
    private_exponent must be a positive integer less than 2^256
    """
    return pow(5, private_exponent, modulus) // 4
这个函数实现了Z2kDH协议的初始化步骤:

计算公共结果:使用基数5和私有指数进行模幂运算 pow(5, private_exponent, modulus)。这里使用了Python的内置pow函数,它计算 (5^private_exponent) % modulus。
移除最后两位:然后将结果除以4,即移除最后两位。这是因为在计算中有两位被用作控制位或调整值。
交换共享秘密
def Z2kDH_exchange(public_result, private_exponent):
    """
    Computes the shared secret by taking the sender's public result to the receiver's private exponent, then removing the last 2 bits
    public_result must be a non-negative integer less than 2^256
    private_exponent must be a positive integer less than 2^256
    """
    return pow(public_result * 4 + 1, private_exponent, modulus) // 4
这个函数实现了共享秘密的交换:

计算共享秘密:使用接收方的私有指数对发送方的公共结果进行计算。首先将公共结果乘以4并加1,然后对结果进行模幂运算 pow(public_result * 4 + 1, private_exponent, modulus)。
移除最后两位:然后将结果除以4,同样是为了去除调整位或控制位。
执行密钥交换
alice_public_result = Z2kDH_init(alice_private_exponent)
bob_public_result = Z2kDH_init(bob_private_exponent)

# These are sent over the public channel:
print(f'{alice_public_result:064x}') # Alice sent to Bob
print(f'{bob_public_result:064x}')   # Bob sent to Alice

alice_shared_secret = Z2kDH_exchange(bob_public_result, alice_private_exponent)
bob_shared_secret = Z2kDH_exchange(alice_public_result, bob_private_exponent)

assert alice_shared_secret == bob_shared_secret # the math works out!
生成公共结果:Alice和Bob分别生成他们的公共结果,并打印出来。这里的结果是以十六进制格式输出的,长度为64个字符(填充了零)。

计算共享秘密:Alice和Bob分别计算共享秘密。Alice使用Bob的公共结果和她自己的私有指数,Bob使用Alice的公共结果和他的私有指数。

验证:通过assert语句,验证Alice和Bob计算出的共享秘密是否相同。如果相同,说明算法实现正确,双方获得了相同的共享秘密。


import math
from sympy.ntheory import discrete_log

# 定义常量
modulus = 1 << 258

def Z2kDH_init(private_exponent):
    """计算 Diffie-Hellman 初始公钥"""
    return pow(5, private_exponent, modulus) // 4

def Z2kDH_exchange(public_result, private_exponent):
    """计算 Diffie-Hellman 共享秘密"""
    return pow(public_result * 4 + 1, private_exponent, modulus) // 4

# Alice 和 Bob 的公钥(从外部接收到的值)
alice_public_result = int('99edb8ed8892c664350acbd5d35346b9b77dedfae758190cd0544f2ea7312e81', 16)
bob_public_result = int('40716941a673bbda0cc8f67fdf89cd1cfcf22a92fe509411d5fd37d4cb926afd', 16)

# 计算 Alice 和 Bob 的私钥
alice_private_exponent = discrete_log(modulus, (alice_public_result * 4 + 1), 5)
bob_private_exponent = discrete_log(modulus, (bob_public_result * 4 + 1), 5)

print("alice_private_key =", alice_private_exponent)
print("bob_private_key =", bob_private_exponent)

# 打印公钥
print(f'{alice_public_result:064x}')  # Alice 发送给 Bob
print(f'{bob_public_result:064x}')    # Bob 发送给 Alice

# 计算 Alice 和 Bob 的共享秘密
alice_shared_secret = Z2kDH_exchange(bob_public_result, alice_private_exponent)
bob_shared_secret = Z2kDH_exchange(alice_public_result, bob_private_exponent)

print("alice_shared_secret =", alice_shared_secret)
print("bob_shared_secret =", bob_shared_secret)

# 确保共享秘密一致
assert alice_shared_secret == bob_shared_secret

# 输出共享秘密的标志(64字节)
print("flag =", int.to_bytes(int(alice_shared_secret), length=64, byteorder='big', signed=False))

https://ctftime.org/writeup/36405

分类:Crypto
image
作者:abyk

1

提交

0

收入

相关WriteUP