Stanford - Cryptography I - Week 1-2 Programming Assignment

本文详细介绍了如何证明一个使用56位种子的PRG(伪随机数生成器)是不安全的,并提供了Python实现的PRG以及将其转换为Java代码的过程。通过分析PRG的算法,作者展示了如何预测下一个输出,并揭示了安全算法中种子长度的重要性。最终,计算得出PRG的第10个输出为231886864。
摘要由CSDN通过智能技术生成

题注

在整理我自己Cryptography I答案的时候,我发现以前旧博客里面很多的代码都是不能用的… 这实在是对不起广大朋友们啊!而且旧答案真正的源代码已经基本都找不到了,因此我有的代码就干脆重新整理甚至重写,以保证答案的正确性。这次应该是没问题了~

题目

Weak PRG

The PRG described below uses a 56-bit secret seed. Running the program generates the following first nine outputs of the PRG:
output #1: 210205973
output #2: 22795300
output #3: 58776750
output #4: 121262470
output #5: 264731963
output #6: 140842553
output #7: 242590528
output #8: 195244728
output #9: 86752752

Show that this PRG is insecure by computing the next output. What is the next output (output #10) of the PRG? Note that you are not given the seed.

Hint: there is an algorithm that takes time approximately 2^28 to predict the next output.

Here is the Python script that implements the PRG:

import random

P = 295075153L   # about 2^28

class WeakPrng(object):
    def __init__(self, p):   # generate seed with 56 bits of entropy
        self.p = p
        self.x = random.randint(0, p)
        self.y = random.randint(0, p)
   
    def next(self):
        # x_{i+1} = 2*x_{i}+5  (mod p)
        self.x = (2*self.x + 5) % self.p

        # y_{i+1} = 3*y_{i}+7 (mod p)
        self.y = (3*self.y + 7) % self.p

        # z_{i+1} = x_{i+1} xor y_{i+1}
        return (self.x ^ self.y) 


prng = WeakPrng(P)
for i in range(1, 10):
  print "output #%d: %d" % (i, prng.next())

分析

PRG

PRG(伪随机数生成器)是密码学中非常重要的一个原型函数。实际上,当今的计算机是没有办法获得真正的随机数的,计算机产生随机数的方法无非两种:(1)用一些可以预估,但是几乎

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值